2011-10-11 112 views
0

我有一个将值发送到变量$ _POST ['person_id'],$ _POST ['accident_loc'],$ _POST ['message']和$ _POST ['name' ]。如何获得http响应代码

该php文件插入值作为记录在数据库中,我没有任何问题。但是,当表单不向变量发送任何值时,我想返回一个错误。

我的意思是当我提交一个没有任何值的表单。所以我做了一个if语句if(isset($_POST['person_id']) && isset($_POST['accident_loc']) && isset($_POST['message']) && isset($_POST['name'])){},如果这个语句失败,调用函数“sendResponse()”。

现在我想要这个文件返回状态代码400到我的JavaScript文件,这样我可以写一些错误条件。但是当我试图显示状态码request.status的值,它给了我0作为它的值

下面是我的PHP文件..你能告诉我如何发送状态码?

<?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: 这里是我的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); 

} 

,如果你在我的javascript发现有什么问题请指正

+0

了解如何在PHP中启用错误/警告报告,然后使用Fiddler或Firebug检查PHP的响应。我非常怀疑它会给你一个警告,说明输出写入后不能更改标题 - 在设置响应标题之前,你正在回应你的MySQL错误(本身可能是一个坏主意)。 – tomfumb

+0

为什么你总是执行'sendResponse()'?你不想在那里有一个'else',所以只有当'if'条件失败时才会调用它。 – pimvdb

+0

你是对的我很抱歉粘贴它,我做了一个错误将现在纠正它 – indr

回答

2

你这样做是差不多吧,这里是我是怎么了能够在我身边做到这一点

<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> 

而在第二个文件:

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

所有这一切都可以在jQuery的网站上,只需将它适应您的需求,使之在你的系统中工作:http://api.jquery.com/jQuery.get/

好运

+0

请注意,我认为你的PHP部分会发送400错误。如果是这样,那么你的问题不在PHP部分,而是在JavaScript部分。这就是我在这里给你的,如何在JavaScript中... –

+0

嗨,感谢您的链接..但我不认为这个问题是在我的javacript。我已更新我的JavaScript代码,如果你发现任何错误请让我知道..我也通过你的链接 – indr

1

相反 SendResponse(); 只是发送适当的HTTP错误代码。

线

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

会为你做的。有关所有有效状态代码的列表,请参阅 http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

+0

我想使用sendReponse()函数,以便我可以发送多个http错误代码。我刚刚尝试了一个,但它没有工作:( – indr

相关问题