0

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

5
  • Look into how to enable error / warning reporting in PHP, then inspect the PHP's response with Fiddler or Firebug. I stongly suspect it will give you a warning that headers can't be altered after output has been written - you are echoing your MySQL error (which in itself might be a bad idea) before setting the response header. Commented Oct 11, 2011 at 19:13
  • Why do you always execute sendResponse()? Don't you want an else there, so that it's only called when the if condition fails? Commented Oct 11, 2011 at 19:14
  • ya you are right i am sorry while pasting it i did a mistake will correct it now Commented Oct 11, 2011 at 19:19
  • @tomfumb did u mean error_reporting(E_ALL); if so i tried it. and i tried running my application with firebug. but it did not give any error Commented Oct 11, 2011 at 22:19
  • @indr yes I did but this was when your code was in its original state - with sendResponse() called on every request, including after you echoed the mysql error. Does Firebug tell you that the response status is 400? Commented Oct 12, 2011 at 1:03

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Note, i assumed your PHP portion DOES sent the 400 error. If it does, then your problem is not in the PHP portion but in the javascript portion. And thats what i gave you here, the how to in javascript...
hi thanks for the link.. but i am dont think the problem is in my javacript. i have updated my javascript code, if u find any fault pls let me know.. and i also went through your link
1

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

1 Comment

i am trying to use sendReponse() function so that i can send more than one http error code. i just tried it out with one but it didnt work out:(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.