选择下拉菜单时可以运行数据库更新功能 [英] Possible to run DB update function when dropdown is selected

查看:157
本文介绍了选择下拉菜单时可以运行数据库更新功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道当从下拉列表中选择一个项目时是否可以运行更新查询。用户做出选择,然后调用一个函数来更新数据库中的特定字段。这将通过使用选择框来存储选项来实现。谢谢。

  echo'< td>'; 
echo'< select name =order_status []onChange =update()>';
echo'< option value =1class =pending> Pending< / option>';
echo'< option value =2class =approved> Approved< / option>';
echo'< option value =3class =disapproved> Disapproved< / option>';
echo'< / select>';
echo'< / td>';
echo'< / tr>';


解决方案

是的,这是可能的。如果您希望查询能够无缝地运行(也就是说,没有按下提交按钮并刷新页面),那么您将需要使用Ajax将请求异步发送到您的PHP脚本。



编辑:最简单的方法就是在 onchange $。get()功能c $ c>事件。这样,每次有人选择一个选项时,jQuery都会使用该选项的值将请求发送到您的PHP脚本。 PHP脚本将运行,并将新数据返回给您的jQuery,然后使用它的DOM功能将该数据插入到您的页面中。



您可以执行同一件事与一个按钮。在按钮的 onclick 事件中,而不是在select元素的事件中,只需粘贴 $。get onchange 事件。



jQuery网站的文档将为您提供相关的代码示例。



编辑2:好的,这是一个非常简单的例子。

首先,让我们考虑一下你希望在后端发生的实际过程。用最简单的术语来说,你想从用户输入中获取一个id并使用它在查询中运行。非常简单(在数据库工作中使用PDO):

  //在真实应用程序中,您需要清理并验证传入的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); //让jQuery变得友好

好吧,所以后端很简单。现在,对于字体结束,魔术发生的地方。以下是我如何使用 select 元素和按钮来传递id给PHP脚本,然后处理结果:

 <! - 您的HTML直到您的页面上的选择和按钮的位置 - > 

< select id =idname =id>
< option value =1> Something< / option>
< option value =2>其他东西< / option>
< option value =3>另一件事< / option>
< / select>

< button id =btn/>

<! - 在你的jQuery - >点击(函数(){
$ .get(path / to / your / back / end / script.php,{id :$(#id)。value},function(data){
/ *数据将是PHP脚本回显的json_encode($ row)
*你需要钻取它,获取你想要的数据,并使用
* jQuery / JavaScript的DOM操作工具将数据插入到你的
*页面
* /
}中) //关闭$ .get()
}); //关闭.click()

这一切都没有经过测试,并且确实不完整,但应该绰绰有余,开始。真的,你需要弄清楚的是如何钻取返回的数据(这是一个JSON对象,所以它不应该太糟糕......使用浏览器的Web开发工具来查看数据是如何形成的)并将其插入你想要的地方。这和我上面可能犯的任何愚蠢的错误。



希望这有助于!


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 particular field in a database. This will be achieved using a select box to store the options. Thanks.

    echo '<td>';
    echo '<select name="order_status[]" onChange = "update()">';
    echo '<option value = "1" class ="pending">Pending</option>';
    echo '<option value = "2" class = "approved">Approved</option>';
    echo '<option value = "3" class ="disapproved">Disapproved</option>';
    echo '</select>';
    echo '</td>';
    echo '</tr>';

解决方案

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 -->

<select id="id" name="id">
    <option value="1">Something</option>
    <option value="2">Something Else</option>
    <option value="3">Yet Another Thing</option>
</select>

<button id="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!

这篇关于选择下拉菜单时可以运行数据库更新功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆