通过AJAX、PHP和MYSQL传递参数时返回多个值
我正在开发基于ajax的搜索,这是它的演示。我在返回结果时遇到了问题。我需要将结果显示两次。但它只放映一次。下面是我的HTML代码通过AJAX、PHP和MYSQL传递参数时返回多个值,php,mysql,html,ajax,jquery,Php,Mysql,Html,Ajax,Jquery,我正在开发基于ajax的搜索,这是它的演示。我在返回结果时遇到了问题。我需要将结果显示两次。但它只放映一次。下面是我的HTML代码 <form action="" method="post" id="demoform"> <select style="width:250px;padding:5px 0px;color:#f1eedb;" name="product" class="product"> <option>TENNIS</option&
<form action="" method="post" id="demoform">
<select style="width:250px;padding:5px 0px;color:#f1eedb;" name="product" class="product">
<option>TENNIS</option>
<option>FOOTBALL</option>
<option>SWIMMING</option>
</select>
</form>
<div id="result">Display Result Here</div>
post.php文件包含以下代码:-
<?php
require('connect.php');
$get_select = $_POST[product];
if($get_product!='FOOTBALL'){
$return['error'] = true;
return['msg'] = 'Incorrect Selection';
echo json_encode(return);
}
else {
$return['error'] = false;
$i=0;
while($i<2) {
return['msg'] = $get_product;
}
echo json_encode(return);//Returns only one result.
}
?>
我需要显示两次板球比赛的结果,但它只显示一次。
我应该怎么做才能得到这两个结果 这一行是否可能让php感到困惑:
虽然$i这一行是否可能让php感到困惑:
而$i请更改以下代码:
else {
$i=0;
$messageToReturn = "";
while($i<2) {
$messageToReturn .= $get_product; //Append to your variable
}
return json_encode($messageToReturn); //Returns the result
}
我建议将while改为for循环。
在这种情况下,您将得到以下结果:
else {
$messageToReturn = "";
for($i = 0; $i < 2; $i++)
{
$messageToReturn .= $get_product; //Append to your variable
}
return json_encode($messageToReturn);
如果您知道需要重复的次数,请使用for循环。这段时间永远不会结束。因此,可能会出现堆栈溢出…请更改以下代码:
else {
$i=0;
$messageToReturn = "";
while($i<2) {
$messageToReturn .= $get_product; //Append to your variable
}
return json_encode($messageToReturn); //Returns the result
}
我建议将while改为for循环。
在这种情况下,您将得到以下结果:
else {
$messageToReturn = "";
for($i = 0; $i < 2; $i++)
{
$messageToReturn .= $get_product; //Append to your variable
}
return json_encode($messageToReturn);
如果您知道需要重复的次数,请使用for循环。这段时间永远不会结束。因此,您可能会遇到堆栈溢出…我对PHP有几个问题:1$get\u产品不存在-您的意思是$get\u select吗?2所有你说‘return’的地方你是说‘return’吗?3为什么你要将$get_product放入$return['msg']中两次?你认为这两个结果都会得到吗?如果你选择“FOOTBALL”,你应该得到一些{'error':'false','msg':'FOOTBALL'}的结果。@adwitya media查看答案并标记正确的答案。请:我有几个关于PHP的问题:1$get_产品不存在-你是说$get_select吗?2所有你说‘return’的地方你是说‘return’吗?3为什么你要将$get_product放入$return['msg']中两次?你认为这两个结果都会得到吗?如果您选择“FOOTBALL”,您应该返回一些具有{'error':'false','msg':'FOOTBALL'}效果的内容。@adwitya media查看答案并标记正确答案,请: