更改时选择提交表单但忽略结果 - jquery / php
我正在为客户构建发票系统,并希望他们能够通过从选择下拉列表中进行选择来更改订单状态和订单付款状态。
表单
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅 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.
首先提供您的表单并选择 id,如下所示:
然后在您的脚本中添加一个更改观察者:
这应该可以帮助您开始,然后阅读更多有关 jQuery AJAX 函数的信息,以便您可以处理来自服务器的响应。
First give your forms and selects ids like so:
Then add a change observer in your script:
That should get you started, then read a bit more about the jQuery AJAX functions so you can handle the response from the server.
当然,像这样使用 change() :
并添加
id="orderStatus"
前往订单状态下拉列表sure, use change() like this:
and add
id="orderStatus"
to the order status dropdown