php - 选择选项更改后更新数据库

标签 php jquery html

Database

Result

“引用编号,状态”均来自数据库。以下代码在顶部链接中显示了结果。 这是我的问题:在将选择选项从“待定”更改为“已交付”后,如何立即将我的数据库从“待定”更新为“交付”?

<?php
echo '<tr>
    <td>Reference No</td>
    <td>Status</td>
</tr>';
$result = mysql_query("select * FROM orderhistory"); 
while($row=mysql_fetch_array($result)){
    $shopReference = $row['reference'];
    $status = $row['status'];
    if($status == 'pending')
    {$status = 'Pending';}
    elseif($status == 'delivered')
    {$status = 'Delivered';}
    if($q==0) continue;
?>
    <tr>
    <td><?php echo $shopReference?></td>
    <td>
        <select id="status" name="status" size="1" required>
            <option value="" style="display:none">Status</option>
                <option value="pending" <?php if($status == 'Pending') echo "selected"; ?>>Pending</option>
                <option value="delivered" <?php if($status == 'Delivered') echo "selected"; ?>>Delivered</option>
            </optgroup>
        </select>
    </td>

<?php                   
}
?>

更新数据库的SQL最有可能是这个

$sql = "UPDATE orderhistory SET status= '$status' WHERE reference = '$reference'";

但是我应该使用什么 jQuery 或其他东西呢?

我猜应该是 onChange 上的某个函数?我不太确定如何使用它。我试图在网上搜索,但对此一无所知……抱歉,我是新手……

最佳答案

我经常看到这个问题,所以我根据我对这个概念的理解写了一个概括性的答案,以便将以后类似类型的问题重定向到这里。

作为新手,您应该知道的第一件事是,当您打开一个 PHP 页面时,PHP 代码是第一个被服务器执行的代码,然后是 HTML 和 JavaScript 由服务器执行浏览器。现在,当您与 HTML 元素交互时,例如更改输入框的内容或从下拉列表中选择一个选项,甚至单击按钮等,这些 Action /事件可以被 JavaScript 检测到,但是 < em>不是 PHP。因此,您需要一种方法让客户端 (JavaScript) 与服务器端 (PHP) 交互。这种方式叫做 AJAX

简单来说,AJAX 所做的就是当用户在页面上执行任何操作(例如单击按钮)时,使用 events (event handlers )您可以捕获用户输入并通过 AJAX它传递给 PHP。

AJAX 框架预览:

$.ajax({ // ajax block starts
   url: 'send.php', // The PHP file that you want to send your user input to
   type: 'POST', /*
                    by default the values will be sent as GET - 
                    through address but if it's sensitive data,
                    use POST 
                 */
   data: {data1: $('#textbox').val(), data2: $('#select').val()}, // The data to be sent
   dataType: 'text', /*
                        Over here you set the type of response that will be sent
                        back by the PHP file. It could be 'text','html' or
                        if you have to send back values in array format 
                        you can use'json' type
                     */
   success: function(data) 
   {
    /* 
       This block will be called once the PHP file is executed and 
       the 'data' parameter here holds
       the values returned from the PHP file
    */
   }
});

如前所述,您可以在加载页面或与 HTML 元素交互时使用事件和事件处理程序调用 AJAX。

例如,我们有一个 button作为:

<input type="button" id="button" value="Click" />

我们可以通过以下方式检测点击事件:

$('#button').click(function(){
  // AJAX here will run on click of button 
}

或者如果我们有一个 select下拉列表:

<select id="dropdown">
  <option value=1>1</option>
  <option value=2>2</option>
</select>

change选择一个选项时将触发方法

$('#dropdown').change(function(){
   // AJAX here will run on change of select
}

哈希 #这里表示id属性。你不能有相同的 id多次,如果遇到这种情况,你应该使用class属性,然后您将使用点 .类名如下:

<input type="button" class="button" value="Click">
<input type="button" class="button" value="Click Again">

$('.button').click(function(){ 
    //clicking any of the above button will trigger this code
}

既然您有多个具有相同类的按钮,该函数如何知道识别哪个按钮被单击了?为此,您使用 $(this) . $(this)是一个 jQuery 元素对象,它引用调用该方法的当前对象。

另一个需要注意的重点是,如果你加载了动态 HTML 元素,那么你需要添加一个 on()事件处理程序。更多信息 here.

现在关键的部分是在我们的 PHP 中访问我们从 AJAX 传递的值:

/*
   This if block here detects if the request is sent through AJAX,
   it's not necessary but use this if you want to prevent direct access
   to this PHP file and allow it ONLY through AJAX.
*/
if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' )
{
    /*
       Now depending on the 'type' we set in our AJAX code, 
       we would have to call it by $_GET if the type is 'GET' 
       and $_POST if the type is 'POST', in the above we have 
       set it to POST so the following code demonstrates the same
    */

    # So the data can be accessed as:
    echo $_POST['data1'];
    echo $_POST['data2'];
}

data1 , data2这是我们引用 AJAX 中传递的值的标识符。

AJAX 中还有很多有用的功能,例如定期访问 PHP 文件 (timeout)、以数组格式返回数据 (json) 等。

或者,您也可以使用 $.get$.post它们再次基于 AJAX 的概念,但功能较少。

关于php - 选择选项更改后更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23321594/

相关文章:

php - 无法 Eloquent 自动创建连接

javascript - 添加类时单击时停止 jquery 循环函数

javascript - angularjs-无法使用jquery提交文本框中的数据集

html - jQuery 或 HTML5 的世界地图框架

javascript - 这个错误 "Uncaught TypeError: Cannot read property ' style' of undefined” 是什么意思?

html - 容器的高度不随内容改变

php - 如何更改 Joomla 3.2.1 中的默认 block /部分?

php - ASP.net 调用 javascript,然后将值返回给后面的代码

php - 在同一个表上组合多个选择计数查询

html - 如何删除内联 CSS 元素样式(不使用 JavaScript)?