Skip to content Skip to sidebar Skip to footer

Possible To Run Db Update Function When Dropdown Is Selected

I would like to know if it would be possible to run an update query when an item is selected from a drop down list. The user makes a choice, a function is then called to update a p

Solution 1:

Yes, it's possible. If you want the query to be run seamlessly (that is, without a submit button being pressed and the page refreshing), then you'll need to use Ajax to send the request asynchronously to your PHP script.

EDIT: The easiest thing to do is simply use jQuery's $.get() functionality in your onchange event. That way, each time someone chooses an option, jQuery will send the request to your PHP script with that option's value. The PHP script will run, and return the new data back to your jQuery, which will then use its DOM functionality to insert that data into your page.

You can do the same thing with a button. Just stick $.get() in the button's onclick event rather than in the select element's onchange event.

The jQuery site's documentation will give you relevant code examples.

EDIT 2: Okay, here's a very canned example.

First, let's think about the actual process you want to have happen on the back end. In the simplest terms, you want to take an id from user input and use that to run in a query. Pretty straight forward (using PDO for the database work):

// in a real app, you'd need to sanitize and validate the incoming id$id = $_GET['id'];

$stmt = $dbh->prepare("SELECT * FROM table_name WHERE id = :id");
$stmt->bindParam(":id", $id);
$stmt->execute();

$row = $stmt->fetch(PDO::FETCH_ASSOC);

echo json_encode($row); // makes it jQuery friendly

Okay, so the back end is pretty simple. Now, for the font end, where the magic happens. Here's how I'd approach using both a select element and a button in order to pass the id back to the PHP script, and then handle the results:

<!-- Your HTML up to where the select and button are on your page --><selectid="id"name="id"><optionvalue="1">Something</option><optionvalue="2">Something Else</option><optionvalue="3">Yet Another Thing</option></select><buttonid="btn" /><!-- In your jQuery -->

$("#btn").click(function() {
    $.get("path/to/your/back/end/script.php", { "id" : $("#id").value }, function(data) {
        /* the data will be the json_encode($row) that was echoed from the PHP script.
         * so, you'll need to drill into it, take the data you want, and use 
         * jQuery's/JavaScript's DOM manipulation tools to insert the data on your 
         * page
         */
    }) // close $.get()
}); // close .click()

None of this is tested, and it's admittedly incomplete, but that should be more than enough to get started. Really, all you'll need to figure out is how to drill into the returned data (it's a JSON object, so it shouldn't be too bad... use a browser's web development tools to see how the data is actually formed) and insert it where you want. That, and any dumb errors I may have made above.

Hope this helps!

Post a Comment for "Possible To Run Db Update Function When Dropdown Is Selected"