选择选项更改后的UPDATE数据库 [英] UPDATE database after select option change

查看:68
本文介绍了选择选项更改后的UPDATE数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<?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上的某些功能?我不太确定如何使用它.我试图在线搜索,但对此一无所知...对不起,我是新来的...

解决方案

我经常看到这个问题,因此我根据对概念的理解写了一个概括性的答案,将以后类似类型的问题重定向到这里. /p>

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

简单地说,AJAX的作用是用户使用事件处理程序 ),您就可以捕获用户输入并将其通过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()事件处理程序. 此处.

现在最关键的部分是访问我们从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'];
}

data1data2是这里的标识符,通过它我们可以引用AJAX中传递的值.

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

或者,您也可以使用 $ .get Here's my question: How do I update my Database from "pending" to "deliver" straight away after I change the select Option from "Pending" to "Delivered"?

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

The SQL to update the database most probably will be this

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

But what jQuery or any other thing should I use?

I guess should be some function on onChange? I'm not very sure how to use it. I tried to search online, but can't get any idea about it... Sorry, I am new...

解决方案

I see this question quite often, so I have written a generalized answer based on my understanding of the concept to redirect the future questions of similar type to here.

First thing you should know as a newbie is, when you open a PHP page, the PHP code is the first one getting executed by the server and then the HTML and JavaScript by the browser. Now when you interact with the HTML elements such as changing the content of an input box or selecting an option from dropdown or even clicking on a button etc., these actions/events can be detected by JavaScript but not PHP. Thus, you need a way to have the client side (JavaScript) interacting with the server side (PHP). This way is called AJAX.

In simple terms of what AJAX does is when the user performs any action on the page such as a button click, using events (and event handlers) you get to capture the user input and pass it to PHP via AJAX.

A skeleton preview of 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
    */
   }
});

Like mentioned before, you can call AJAX using events and event handlers either on load of page or on interaction with HTML elements.

Let's say for example, we have a button as:

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

We can detect the click event by:

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

Or if we have a select dropdown:

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

The change method would get triggered when you select an option

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

The hash # here denotes id attribute. You cannot have same id multiple times, if such a case arrives, you should use the class attribute and then you would use dot . with the class name like the following:

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

Now since you have several buttons with the same class, how would the function know to identify which button has been clicked? For that you use $(this). $(this) is an jQuery element object that refers to the current object on which the method was invoked.

Another important point to note is, if you have dynamic HTML elements loaded, then you need to add a on() event handler. More on it here.

Now the crucial part that is to access the values in our PHP that we have passed from 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 here is the identifier through which we refer to the values passed in the AJAX.

There's a lot more to useful functions in AJAX such as accessing the PHP file at regular intervals (timeout), returning data in array format(json) etc.

Alternatively, you can also use $.get and $.post which are based again on the concept of AJAX but have lesser functionalities.

这篇关于选择选项更改后的UPDATE数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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