indr
indr

Reputation: 13

how to get http response code

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

Upvotes: 0

Views: 1487

Answers (2)

Deckard
Deckard

Reputation: 2450

Instead of SendResponse(); just send the proper HTTP error code.

The line

 header('HTTP/1.1 400 Bad Request');

will do that for you. For a list of all valid status codes have a look at http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Upvotes: 1

Mathieu Dumoulin
Mathieu Dumoulin

Reputation: 12244

You are doing it almost right, here is how i was able to do it on my side

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>

$(function(){

    alert('getting page');
    jqXHR = $.get(
        'index.php', 
        function(data){
            alert(data);
        }
    )
    .success(function(){ alert('second success'); })
    .error(function(){ alert('error'); })
    .complete(function(){ alert('complete'); });
});

</script>

And in the second file:

<?php header('HTTP/1.1 400 Bad Request'); ?>
Invalid request

All of this is available on the jQuery website, just adapt it to your needs to make it work in your system: http://api.jquery.com/jQuery.get/

Good luck

Upvotes: 2

Related Questions