数据不会使用AJAX发布在$ _POST变量中

时间:2012-06-01 01:37:03

标签: php ajax

我的脚本中有一个问题。服务器在php中运行,我使用AJAX发布数据。这是我的剧本。

PHP脚本:

<?php

include './connConf.php';

if (isset($_POST['pStr'])){
    $preStr=$_POST['pStr'];
    $query="SELECT * FROM advisory WHERE projname in (SELECT projname FROM advisorydata WHERE prefixStr LIKE '%$preStr')";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    if ($num > 0){
        echo "<font style=\"font-size:12px\" color=\"#FF6820\" face=\"Century Gothic\"><b>Search Result :</b></font><br><br>";
        for ($x=0;$x<$num;$x+=1){
            echo "<font style=\"font-size:12px\" color=\"#FFFFFF\" face=\"Century Gothic\">Project Name:&nbsp&nbsp&nbsp</font><font style=\"font-size:11px\" color=\"#C0FFFF\" face=\"Century Gothic\"><b>".mysql_result($result,$x,"projname")."</b></font><br>";
            echo "<font style=\"font-size:12px\" color=\"#FFFFFF\" face=\"Century Gothic\">APMS ID:&nbsp&nbsp&nbsp</font><font style=\"font-size:11px\" color=\"#C0FFFF\" face=\"Century Gothic\"><b>".mysql_result($result,$x,"apmsid")."</b></font><br>";
            echo "<font style=\"font-size:12px\" color=\"#FFFFFF\" face=\"Century Gothic\">Prefix/es:&nbsp&nbsp&nbsp</font><font style=\"font-size:11px\" color=\"#C0FFFF\" face=\"Century Gothic\"><b>".mysql_result($result,$x,"projprefix")."</b></font><br>";
            echo "<font style=\"font-size:12px\" color=\"#FFFFFF\" face=\"Century Gothic\">Usage Type:&nbsp&nbsp&nbsp</font><font style=\"font-size:11px\" color=\"#C0FFFF\" face=\"Century Gothic\"><b>".mysql_result($result,$x,"usagetype")."</b></font><br>";
            echo "<font style=\"font-size:12px\" color=\"#FFFFFF\" face=\"Century Gothic\">Rate:&nbsp&nbsp&nbsp</font><font style=\"font-size:11px\" color=\"#C0FFFF\" face=\"Century Gothic\"><b>".mysql_result($result,$x,"projrate")."</b></font><br>";
            echo "<font style=\"font-size:12px\" color=\"#FFFFFF\" face=\"Century Gothic\">Offer Details:&nbsp&nbsp&nbsp</font><font style=\"font-size:11px\" color=\"#C0FFFF\" face=\"Century Gothic\"><b>".mysql_result($result,$x,"offerdetails")."</b></font><br><br>";
        }
    }else{
        echo "<font style=\"font-size:12px\" color=\"#FFFFFF\" face=\"Century Gothic\">No results found ...</font>";
    }
}else{
    echo "<font style=\"font-size:12px\" color=\"#FFFFFF\" face=\"Century Gothic\">Problems encountered while processing the data ...</font>";
}
?>

JS脚本:

function QueryPrefix()
{
    var xmlhttp;
    var pStr = document.getElementById('Editbox2');
    var htmlHolder = document.getElementById('Html1');
    var butStr = document.getElementById('Button1');

    if (pStr.value.length == 0){
        alert("Please enter a value on the box provided!");
        return; 
    }
    pStr.value="";

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4)
            {
                htmlHolder.innerHTML=xmlhttp.responseText;
                butStr.disabled=false;
            }
    }
    butStr.disabled=true;

    xmlhttp.open("POST","searchutype.php",false);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("pStr=" + pStr.value);
}

1 个答案:

答案 0 :(得分:0)

我尝试重现这个问题,它似乎对我有用,使用Chrome作为客户端和服务器上的PHP 5.2。但我确实注意到你没有将Content-Length标头作为标题发送,这可能是问题所在。尝试添加:

var post = "pStr=" + pStr.value;
xmlhttp.setRequestHeader("Content-length",post.length);

当然,您必须在发布之前对URL编码pStr.value进行编码。正如nickb先前所说,为什么不使用jquery等框架库?