When I make an AJAX call from a form, it shows it is successful, but the data object is null. The code is as follows:
$.ajax(
{
url: 'shipprocess.php',
dataType: 'json',
type: 'POST',
success: function(data)
{
alert('Data is: ' + data);
alert('The AJAX request was a success.');
},
'error': function()
{
alert('An unexpected error occurred.');
}
});
return false;
The form looks like this:
<div class="processedDate">
<form action="shipprocess.php" method="POST" id="shipProcess2" name="shipProcess2">
<input type="hidden" name="empID" value="1" />
<input type="hidden" name="thisOrderID" id="thisOrderID2" value="2" />
<label>Date Shipped </label>
<input type="text" name="shipDate" id="shipDate2" class="shipDate" size="20" value="" />
<input type="submit" name="shipped" id="shipped2" class="shipped" value="Shipped!" />
</form>
</div>
After the AJAX call is made, Firebug shows the status as 200 and content length as 0. The script that processes the form is called shipprocess.php. It echos the following data when the return: false; line is commented out:
[ { "sd": "2012-09-17", "eid": "1", "oid": "2", "efn": "Johnathan", "eln": "Smith" } ]
For some reason, the script keeps alerting that data is null. A complete example can be found at http://www.yellowcas.com/ship/shipexample.php. This example shows the alert message that the data object is null when you submit the form. I also have a complete example of the data that the shipprocess.php script returns at http://www.yellowcas.com/ship/shipexample1.php. I have used AJAX before to populate the city and state input fields for a zip code that is entered by the user. The jQuery script is almost identical except for the fact that I use GET instead of POST for the zip code form.
I have tried declaring the header in PHP as JSON data, but that doesn’t help either. Firebug doesn’t seem to be giving me any helpful information either. I have tested the script using a different file called testjson.html. In that file, I put valid JSON data as the only line in the file with no headers at all, and it returns the data variable as an object. That example is at http://www.yellowcas.com/ship/shipexample2.php. I couldn’t post more than 2 hyperlinks. If you would like to see the code for shipprocess.php, I will gladly post it. I just don’t want to make this post too long. Any ideas would be greatly appreciated. Thank you.
I decided to post the shipprocess.php code to be sure you can see what I have done.It is as follows:
<?php
require_once('dblogin.php');
require_once('dbconnect.php');
require_once('funcs.php');
$err = array();
$datePattern = "!^(\\d\\d)[-/](\\d\\d)[-/](\\d\\d(?:\\d\\d)?)$!";
$psErr = "Shipping date is required.";
$emErr = "Employee ID is missing.";
$orErr = "Order ID is missing.";
if(isset($_POST['shipped']))
{
$postEID = clean($_POST['empID'],$emErr,$n);
$postOID = clean($_POST['thisOrderID'],$orErr,$n);
$postShipDate = clean($_POST['shipDate'],$psErr,$n);
$now = date("Y-m-d H:i:s");
if($postEID == $emErr)
{
$err[] = $postEID;
}
else
{
$query = "SELECT FK_UserID,FirstName,LastName FROM employees WHERE EmployeeID = '$postEID'";
$res = mysql_query($query);
if(mysql_num_rows($res) < 1)
{
$err[] = "Employee does not exist.";
}
else
{
while($row = mysql_fetch_assoc($res))
{
$retUserID = $row['FK_UserID'];
$retFirstName = $row['FirstName'];
$retLastName = $row['LastName'];
}
}
}
if($postOID == $orErr)
{
$err[] = $postOID;
}
if($postShipDate == $psErr)
{
$err[] = $postShipDate;
}
else
{
if (preg_match($datePattern,$postShipDate,$sMatches))
{
$sMonth = $sMatches[1];
$sDay = $sMatches[2];
$sYear = $sMatches[3];
if(checkdate($sMonth,$sDay,$sYear))
{
$shipDate = "$sYear-$sMonth-$sDay";
}
else
{
$err[] = "Invalid shipping date.";
}
}
else
{
$err[] = "Invalid Shipping Date";
}
}
if(empty($err))
// Keep processing the information if there are no errors.
{
$data[] = "$postEID,$shipDate,$postOID,$now,$retFirstName,$retLastName";
}
else
// Return the errors to the user so corrections can be made.
{
$data[] = implode(",",$err);
}
for ($i=0;$i<sizeof($data);$i++)
{
$info = explode(",",$data[$i]);
$data[$i] = $info;
}
$result = array();
for ($y=0;$y<sizeof($data);$y++)
{
if (($data[$y][0]) !== false)
{
array_push($result, array("sd"=>$data[$y][1], "eid"=>$data[$y][0], "oid" => $data[$y][2], "efn"=>$data[$y][4], "eln"=>$data[$y][5]));
}
if (count($result) > 2)
{
break;
}
}
}
echo array_to_json($result);
?>
Please try out the three example pages I have provided to see what the different results are. Thank you.
Your code lacks at least 2 things:
1: When posting with Ajax, you have to send post data. So you have to tell what data you want to send. Most of the time it will be the serialized form data, but it could be anything.
2: You expect a return value, but you do not send a return value. In
shipprocess.phpafter processing you should echo somethning like this: