【问题标题】:UPDATE database after select option change选择选项更改后更新数据库
【发布时间】:2014-04-27 09:23:43
【问题描述】:

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

<?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 上的一些功能?我不太确定如何使用它。我试图在网上搜索,但无法了解它...对不起,我是新人...

【问题讨论】:

  • api.jquery.com/change 试试这个
  • 你有没有尝试谷歌“在选择更改时调用 php”或者搜索 ajax select php

标签: php jquery html


【解决方案1】:

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

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

简单来说,AJAX 的作用是当用户在页面上执行任何操作(例如单击按钮)时,使用 eventsevent 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.

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

/*
   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 的概念,但功能较少。

【讨论】:

    【解决方案2】:
    $(document).on('change', '#status', function(event) {
        event.preventDefault();
        // make ajax call for update field in db 
        // or submit form (put select in form)
    });
    

    【讨论】:

      【解决方案3】:

      尝试像这样在 jquery 中使用 ajax

      $(this).on('change', function() {
          var id = $(this).html();
          alert(id);
          $.ajax({
              type:'POST',
              //url: 'updateurl
              success: function(data) {
                   //open dialog box and fill it with data
              }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-19
        • 1970-01-01
        • 2012-12-10
        • 1970-01-01
        • 2020-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多