I have a form which sends the values to the variables $_POST['person_id'] , $_POST['accident_loc'], $_POST['message'] and $_POST['name'].
The php file inserts the values as a record in the database, i dont have any problem with that. But i want to return an error when the form doesn't send any values to the variables.
I mean when i submit a form without any values. so i made an if statement if(isset($_POST['person_id']) && isset($_POST['accident_loc']) && isset($_POST['message']) && isset($_POST['name'])){} if this statement fails call the function "sendResponse()".
now i want this file to return the status code 400 to my javascript file so that i can write some error conditions. But when i tried to display the value of the status code request.status it gave me 0 as its values
Below is my php file.. can you tell me how to send the status code ??
<?php
//allowing access to the server which contains the following host-origin
if($_SERVER[HOST_ORIGIN]=="http:\\localhost")
header('Access-Control-Allow-Origin:http:\\http:localhost');
function sendResponse(){
header('HTTP/1.1 400 invalid request');
header('Content-type: text/html');
echo "invalid request";
}
if(isset($_POST['person_id']) && isset($_POST['accident_loc']) && isset($_POST['message']) && isset($_POST['name']))
{
//php mysql database info
require ("phpMysql_dbInfo.php");
//getting the arguments from the url
$person_id=$_POST['person_id'];
$accident_loc=$_POST['accident_loc'];
$message=$_POST['message'];
$name=$_POST['name'];
//connecting to the database
$connection= mysql_connect($local_host,$username,$password);
if(!$connection)
die(mysql_error()."</br>");
//selecting the db
$select_db= mysql_select_db($database_name);
if(!$select_db)
die(mysql_error()."</br>");
//inserting the values into the database
$query= sprintf("insert into messages_to_people(name,accident_location,message,person_id)
values('%s','%s','%s','%s')",
mysql_real_escape_string($name),
mysql_real_escape_string($accident_loc),
mysql_real_escape_string($message),
mysql_real_escape_string($person_id));
$result=mysql_query($query);
if(!$result)
echo mysql_error().'</br>';
}
else
sendResponse();
?>
UPDATE: here is my javascript
function downloadXml (url,params,callback) {
var request= window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject('Microsoft.XMLHTTP');
request.onreadystatechange= function(){
if(request.readyState==4){
//gives me a zero when i try to display it through alert
alert(request.status);
callback(request, request.status);
}
};
request.open('POST',url,true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send(params);
}
if you find any problem in my javascript pls correct me
sendResponse()? Don't you want anelsethere, so that it's only called when theifcondition fails?error_reporting(E_ALL);if so i tried it. and i tried running my application with firebug. but it did not give any error