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