I am having difficulty implementing an ajax example from the W3C website. The example pulls back results from a database and displays them in a table based on which option has been selected from a Dropdown menu.
http://www.w3schools.com/php/php_ajax_database.asp
I have tried configuring this example to meet my requirements. This is to update a field in the database based on the option that is selected. I think i'm close I just wanted to make sure I am on the right track. When an option is selected an ajax call is made which triggers my php script to make the changes. It looks like this:
<?php
$q = mysql_real_escape_string($_POST['q']);
print_r($_GET["q"]);
$con = mysql_connect('localhost', '', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Orders", $con);
$sql= "UPDATE Orders SET status='.$_POST[q]['.$i.'].' WHERE ID='.$_POST[order_no] ['.$i.'].'";
however this data is not entered into the database. I can retrieve the value of $q which is either 'approved' 'pending' or 'disapproved' but not the specific order number to update that individual row. My select box looks like this:
echo '<select name="order_status[]" id="id" onchange="showUser(this.value)">';
echo '<option value = "Pending" name="order_status['.$i.']" class = "pending"' . ($row['status'] == 'Pending' ? ' selected=selected' : '') . '>Pending</option>';
echo '<option value = "Approved" name="order_status['.$i.']" class = "approved"' . ($row['status'] == 'Approved' ? ' selected=selected' : '') . '>Approved</option>';
echo '<option value = "Disapproved" name="order_status['.$i.']" class ="disapproved"' . ($row['status'] == 'Disapproved' ? ' selected=selected' : '') . '>Disapproved</option>';
this is wrapped in form which also sends an email based on which checkbox is selected. This functionality works and I can send an email to an individual row:
<form method ="post" action="sendemail.php">
echo '<td><input type="checkbox" name="order_selected['.$i.']"/></td>';
//in send mail.php:
if(isset($_POST['order_selected']))
{
$keys = array_keys($_POST['order_selected']);
foreach($keys as $key)
//send email
do I need to implement the update code in the same php script? or use the same approach to loop through an array?
Many Thanks
From your description I am not exactly sure of what your problem is but from looking at your code you have a few syntax errors in your SQL statement.
$sql= "UPDATE Orders SET status='.$_POST[q]['.$i.'].' WHERE ID='.$_POST[order_no] ['.$i.'].'";
should be
$sql= 'UPDATE Orders SET status="' . $_POST['q'][$i] . '" WHERE ID="' . $_POST['order_no'][$i] . '"';
Also its not clear from your code where your $i variable is coming from and your also not doing any validation on $_POST['order_no'] before using it in the SQL statement which which will leave you vulnerable to SQL injection.
Also this example of ajax is a very old way of doing things. I would always include the jQuery library and use there jQuery.ajax() function http://api.jquery.com/jQuery.ajax/ which does all the cross browser stuff for you. It also makes it very easy to select data from DOM elements and parse them into a $_POST or $_GET. Then by using a callback function you can continue with the results from your php script.
$('select.foo').change(function() {
var post = {
status: $('select.foo option:selected').val(); // get the value from a dropdown
}
$.ajax({
type: "POST",
url: "some.php",
data: post,
success: function(returnData){
if(returnData == 1){
alert('updated');
}
}
});
});