Javascript 无法使用ajax从post请求中检索值
我刚刚编写了我的第一个Ajax请求,但它不起作用。当选择下拉列表中的新值时,它需要更新 这是我的javascript代码:Javascript 无法使用ajax从post请求中检索值,javascript,php,ajax,Javascript,Php,Ajax,我刚刚编写了我的第一个Ajax请求,但它不起作用。当选择下拉列表中的新值时,它需要更新 这是我的javascript代码: $(document).ready(function() { function ajaxLoaded(response) { $('#performanceResults').html(response); } function doRequest() { $.ajax({ url: "results.php", type:
$(document).ready(function() {
function ajaxLoaded(response) {
$('#performanceResults').html(response);
}
function doRequest() {
$.ajax({
url: "results.php",
type: 'POST',
success: ajaxLoaded
});
}
$('#performance').change(doRequest);
});
这就是我如何检索不起作用的q部分:
public function getResults() {
$intCase = intval ( $_POST ['q'] );
var_dump ( $intCase );
if ($intCase == 1 or $intCase == 2 ) {
if ($intCase == 1) {
$strSql = 'select bidder_id, won, lost, fillrate, costs, cost_auction from result_bidder where tagload = ( select max(tagload) from result_bidder) order by cost_auction asc limit 1';
}
if ($intCase == 2) {
$strSql = 'select bidder_id, won, lost, fillrate, costs, cost_auction from result_bidder where tagload = ( select max( tagload ) from result_bidder ) order by fillrate asc limit 1';
}
$arrBestPerformer = $objDatabase->queryresult ( $strSql );
echo "<table border='1'>
<tr>
<th>bidder_id</th>
<th>won</th>
<th>lost</th>
<th>fillrate</th>
<th>costs</th>
<th>cost_auction</th>
</tr>";
while ( $row = mysqli_fetch_array ( $arrBestPerformer ) ) {
echo "<tr>";
echo "<td>" . $row ['bidder_id'] . "</td>";
echo "<td>" . $row ['won'] . "</td>";
echo "<td>" . $row ['lost'] . "</td>";
echo "<td>" . $row ['fillrate'] . "</td>";
echo "<td>" . $row ['costs'] . "</td>";
echo "<td>" . $row ['cost_auction'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
}
我的表格:
公共功能选择性能指示器{
$this->getResults ();
$str = '<form >';
$str .= 'Select your performance indicator<br>';
$str .= '<select id = "performance">';
$str .= '<option value = "">Select Performance Indicator</option>';
$str .= '<option value = "1">Cost per auction </option>';
$str .= '<option value = "2">Fillrate </option>';
$str .= '</select>';
$str .= '</form>';
$str .= '<br>';
$str .= '<div id="performanceResults">';
return $str;
}
您的问题是POST数据字符串无效
xmlhttp.send("q ="+ str);
应该是
xmlhttp.send("q="+ str);
没有q和=
您没有发送头来告诉PHP它应该将随请求发送的数据映射到$\u POST var
因此,正确的功能是:
function showUser(str) {
if (str=="") {
document.getElementById("performance").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("performance").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","results.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("q="+ str);
}
但是,同样:用于使您的。如果您使用jQuery,您就不必再与类似的错误作斗争。您在查询字符串中添加了空格,q=这不是$\u POST变量中的q键, 删除键与= 到 更新回答:当请求的内容类型为application/x-www-form-urlencoded头时,将使用AJax帖子。 请在发送方法之前添加标题 或
您可以通过GET方法发送请求,而不需要头只需echo$\u POST['q'];您现在没有返回任何内容。这是完整的PHP脚本吗?@GaijinJim我知道。我如何使用Ajax返回值?@TimesPliner不,不是。这之后只有一个form和mysql语句。没有什么特别的。或者尝试像jQuery这样的JS框架,它将帮助您编写兼容交叉浏览器的JS脚本,并将Ajax请求抽象到v非常高的级别。所以你可以确保你的AJAX请求工作。当你发送它时,JS中的var可能只是空的?修复了这个问题,但它仍然不工作。感谢你发现了这个错误!再次:使用来处理你的AJAX请求。它更简单,更兼容交叉浏览器。感谢你更新的答案。我在标题部分修复了它,但它是空的till不起作用。如果我发布php函数会有帮助吗?如果你使用jQuery做AJAX的事情会有帮助!使用过jQuery。但是现在它调用我的前端两次,而不仅仅是显示表。修复了这个问题,但它仍然不起作用。不过感谢你发现了这个错误!
function showUser(str) {
if (str=="") {
document.getElementById("performance").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("performance").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","results.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("q="+ str);
}
xmlhttp.send("q ="+ str);
xmlhttp.send("q="+ str);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");