1

我的一个脚本有问题。服务器在 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);
}
4

1 回答 1

0

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

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

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

于 2012-06-01T02:56:06.000 回答