Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to get http response code
    primarykey
    data
    text
    <p>I have a form which sends the values to the variables $_POST['person_id'] , $_POST['accident_loc'], $_POST['message'] and $_POST['name']. </p> <p>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. </p> <p>I mean when i submit a form without any values. so i made an if statement <code>if(isset($_POST['person_id']) &amp;&amp; isset($_POST['accident_loc']) &amp;&amp; isset($_POST['message']) &amp;&amp; isset($_POST['name'])){}</code> if this statement fails call the function "sendResponse()". </p> <p>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 <code>request.status</code> it gave me 0 as its values </p> <p>Below is my php file.. can you tell me how to send the status code ??</p> <pre><code> &lt;?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']) &amp;&amp; isset($_POST['accident_loc']) &amp;&amp; isset($_POST['message']) &amp;&amp; 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()."&lt;/br&gt;"); //selecting the db $select_db= mysql_select_db($database_name); if(!$select_db) die(mysql_error()."&lt;/br&gt;"); //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().'&lt;/br&gt;'; } else sendResponse(); ?&gt; </code></pre> <p><strong>UPDATE:</strong> here is my javascript </p> <pre><code>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); } </code></pre> <p>if you find any problem in my javascript pls correct me</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload