更改时选择提交表单但忽略结果 - jquery / php

发布于 2024-10-21 06:45:19 字数 2953 浏览 1 评论 0原文

我正在为客户构建发票系统,并希望他们能够通过从选择下拉列表中进行选择来更改订单状态和订单付款状态。

表单

    <form action="changestatus.php" method="post">
    <input type="hidden" name="status" value="order"/>
    <input type="hidden" name="orderid" value="33"/>
    <select name="orderstatus">
        <option value="Complete">Complete</option>
        <option value="Incomplete">Incomplete</option>
    </select>
    </form>
    <form action="changestatus.php" method="post">
    <input type="hidden" name="status" value="payment"/>
    <input type="hidden" name="orderid" value="33"/>
    <select name="paymentstatus">
        <option value="Complete">Complete</option>
        <option value="Incomplete">Incomplete</option>
    </select>
    </form>

changestatus.php

<?php
switch($_POST['status'])
{
case"payment":  
        mysql_query("UPDATE orders SET payment_status = '$_POST[paymentstatus]' WHERE ID = '$_POST[orderid]'")or die(mysql_error());
break;
case"order":    
        mysql_query("UPDATE orders SET order_status = '$_POST[orderstatus]' WHERE ID = '$_POST[orderid]'")or die(mysql_error());
break;
}

?>

我想我可以使用 jquery.post() (http://api.jquery.com/jQuery.post/) 但我不知道如何实施。

/////编辑

好的我找到了解决方案。

我更改了“changestatus.php”以接受 GET 变量

   <?php
include"../inc/config.php";
switch($_GET['status'])
{
case"payment":  
        $query = mysql_query("UPDATE orders SET payment_status = '$_GET[paymentstatus]' WHERE ID = '$_GET[ID]'")or die(mysql_error());
        if($query){echo"Saved";}else{echo"Not Saved";};
break;
case"order":    
        $query = mysql_query("UPDATE orders SET orderStatus = '$_GET[orderstatus]' wHERE ID = '$_GET[ID]'")or die(mysql_error());
        if($query){echo"Saved";}else{echo"Not Saved";};
break;
}

?>

然后将表单更改为

    <select name="orderstatus">
        <option value="Complete">Complete</option>
        <option value="Incomplete">Incomplete</option>
    </select>


    <select name="paymentstatus">
        <option value="Complete">Complete</option>
        <option value="Incomplete">Incomplete</option>
    </select>

最后是 javascript

<script>
$(document).ready(function() {


$('#paymentstatus').change(function() {
    url = "changestatus.php?ID=<?php echo $_GET['ORDERID'];?>&status=payment&paymentstatus="+$('#paymentstatus').val();
  $("#payment_status_result").load(url)
});
$('#orderstatus').change(function() {
    url = "changestatus.php?ID=<?php echo $_GET['ORDERID'];?>&status=order&orderstatus="+$('#orderstatus').val();
  $("#order_status_result").load(url)
});
});
</script>

I am building an invoice system for a client and would like to give them the ability to change the order status and payment status of the order by selecting them from a select dropdown.

The forms

    <form action="changestatus.php" method="post">
    <input type="hidden" name="status" value="order"/>
    <input type="hidden" name="orderid" value="33"/>
    <select name="orderstatus">
        <option value="Complete">Complete</option>
        <option value="Incomplete">Incomplete</option>
    </select>
    </form>
    <form action="changestatus.php" method="post">
    <input type="hidden" name="status" value="payment"/>
    <input type="hidden" name="orderid" value="33"/>
    <select name="paymentstatus">
        <option value="Complete">Complete</option>
        <option value="Incomplete">Incomplete</option>
    </select>
    </form>

changestatus.php

<?php
switch($_POST['status'])
{
case"payment":  
        mysql_query("UPDATE orders SET payment_status = '$_POST[paymentstatus]' WHERE ID = '$_POST[orderid]'")or die(mysql_error());
break;
case"order":    
        mysql_query("UPDATE orders SET order_status = '$_POST[orderstatus]' WHERE ID = '$_POST[orderid]'")or die(mysql_error());
break;
}

?>

I think I can use jquery.post() (http://api.jquery.com/jQuery.post/) but I have no idea how to implement it.

/////EDIT

OK i found a solution to this.

I changed "changestatus.php" to accept GET variables

   <?php
include"../inc/config.php";
switch($_GET['status'])
{
case"payment":  
        $query = mysql_query("UPDATE orders SET payment_status = '$_GET[paymentstatus]' WHERE ID = '$_GET[ID]'")or die(mysql_error());
        if($query){echo"Saved";}else{echo"Not Saved";};
break;
case"order":    
        $query = mysql_query("UPDATE orders SET orderStatus = '$_GET[orderstatus]' wHERE ID = '$_GET[ID]'")or die(mysql_error());
        if($query){echo"Saved";}else{echo"Not Saved";};
break;
}

?>

Then changed the forms to

    <select name="orderstatus">
        <option value="Complete">Complete</option>
        <option value="Incomplete">Incomplete</option>
    </select>


    <select name="paymentstatus">
        <option value="Complete">Complete</option>
        <option value="Incomplete">Incomplete</option>
    </select>

and finally the javascript

<script>
$(document).ready(function() {


$('#paymentstatus').change(function() {
    url = "changestatus.php?ID=<?php echo $_GET['ORDERID'];?>&status=payment&paymentstatus="+$('#paymentstatus').val();
  $("#payment_status_result").load(url)
});
$('#orderstatus').change(function() {
    url = "changestatus.php?ID=<?php echo $_GET['ORDERID'];?>&status=order&orderstatus="+$('#orderstatus').val();
  $("#order_status_result").load(url)
});
});
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手心的温暖 2024-10-28 06:45:19

请参阅 http://api.jquery.com/change/ 了解如何在下拉菜单已更改。

至于 jquery 帖子,使用它的主要原因是进行 AJAX 调用。无论你做什么,你都不想忽视结果。您的脚本应始终返回错误或成功消息。

See http://api.jquery.com/change/ to find out how to trigger javascript when a drop-down menu is changed.

As for the jquery post, the primary reason for using this is for making an AJAX call. Whatever you do, you don't want to ever ignore the result. Your script should always return an error or success message.

昔日梦未散 2024-10-28 06:45:19

首先提供您的表单并选择 id,如下所示:

 <form action="changestatus.php" method="post" id="orderStatusForm">
 <form action="changestatus.php" method="post" id="paymentStatusForm">
 <select name="orderstatus" id="orderstatus">
 <select name="paymentstatus" id="paymentstatus">

然后在您的脚本中添加一个更改观察者:

var changeOrderStatus = function(){
     $.post("changestatus.php", $("#orderStatusForm").serialize());
}

var changePaymentStatus = function(){
     $.post("changestatus.php", $("#paymentStatusForm").serialize());
}

$('#orderstatus').change(changeOrderStatus);
$('#paymentstatus').change(changePaymentStatus);

这应该可以帮助您开始,然后阅读更多有关 jQuery AJAX 函数的信息,以便您可以处理来自服务器的响应。

First give your forms and selects ids like so:

 <form action="changestatus.php" method="post" id="orderStatusForm">
 <form action="changestatus.php" method="post" id="paymentStatusForm">
 <select name="orderstatus" id="orderstatus">
 <select name="paymentstatus" id="paymentstatus">

Then add a change observer in your script:

var changeOrderStatus = function(){
     $.post("changestatus.php", $("#orderStatusForm").serialize());
}

var changePaymentStatus = function(){
     $.post("changestatus.php", $("#paymentStatusForm").serialize());
}

$('#orderstatus').change(changeOrderStatus);
$('#paymentstatus').change(changePaymentStatus);

That should get you started, then read a bit more about the jQuery AJAX functions so you can handle the response from the server.

挽清梦 2024-10-28 06:45:19

当然,像这样使用 change()

$("#orderStatus").change(function() { /* post() your form here */ });

并添加 id="orderStatus" 前往订单状态下拉列表

sure, use change() like this:

$("#orderStatus").change(function() { /* post() your form here */ });

and add id="orderStatus" to the order status dropdown

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文