通过AJAX、PHP和MYSQL传递参数时返回多个值

时间:2021-10-25 07:28:48

I am developing ajax Based Search , This is demo of how it will be. I am faceing Problem in returning result. I need to show the Result 2 times. But its only showing once. Below is my HTML code

我正在开发基于ajax的搜索,这是它的演示。我正在面临返回结果的问题。我需要显示结果2次。但它只显示一次。下面是我的HTML代码

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

I Using The Below Ajax Script to Retrieve Data :-

我使用下面的Ajax脚本检索数据:-。

$(".product").change(function(){
            $.ajax({
                type : 'POST',
                url : 'post.php',
                dataType : 'json',
                data: {
                    product : $(".product option:selected").text(),
                },
                success : function(data){
                    $('#result').removeClass().addClass((data.error === true) ? 'error' : 'success')
                        .html(data.msg).show();
                    if (data.error === true)
                        $('#demoForm').show();
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    $('#result').removeClass().addClass('error')
                        .text('There was an error.').show(500);
                    $('#demoForm').show();
                }
            });
        });

The post.php file has the following code :-

这个职位。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.
}
?>

I need to show the result Two times as "CRICKET CRICKET", but its only showing once. What should i do to get both the result.

我需要显示两次结果作为“板球板球”,但它只显示一次。我该怎么做才能得到两个结果。

2 个解决方案

#1


0  

Is it possible that this line is confusing php:

这条线可能会让php感到困惑:

while($i<2) {
return['msg'] = $get_product;
}

而($i<2) {return['msg'] = $get_product;}

Should it be $return? Using a reserved word like 'return' is a tad iffy too.

它应该返回美元吗?使用“return”这样的保留字也有点不确定。

#2


0  

Please change the following code:

请更改以下代码:

else {
  $i=0;
  $messageToReturn = "";
  while($i<2) {
    $messageToReturn .= $get_product; //Append to your variable
  }
  return json_encode($messageToReturn); //Returns the result
}

I would suggest to change the while to a for loop. In that case you will get this:

我建议把时间改为for循环。在这种情况下,你会得到:

else {
 $messageToReturn = "";
 for($i = 0; $i < 2; $i++)
 {
    $messageToReturn .= $get_product; //Append to your variable
 }
 return json_encode($messageToReturn);

If you know the times you need to repeat, use a for loop. The while is never ending. So you can get a possible stack overflow...

如果您知道需要重复的时间,请使用for循环。这段时间永远不会结束。所以你可以得到一个可能的堆栈溢出……

#1


0  

Is it possible that this line is confusing php:

这条线可能会让php感到困惑:

while($i<2) {
return['msg'] = $get_product;
}

而($i<2) {return['msg'] = $get_product;}

Should it be $return? Using a reserved word like 'return' is a tad iffy too.

它应该返回美元吗?使用“return”这样的保留字也有点不确定。

#2


0  

Please change the following code:

请更改以下代码:

else {
  $i=0;
  $messageToReturn = "";
  while($i<2) {
    $messageToReturn .= $get_product; //Append to your variable
  }
  return json_encode($messageToReturn); //Returns the result
}

I would suggest to change the while to a for loop. In that case you will get this:

我建议把时间改为for循环。在这种情况下,你会得到:

else {
 $messageToReturn = "";
 for($i = 0; $i < 2; $i++)
 {
    $messageToReturn .= $get_product; //Append to your variable
 }
 return json_encode($messageToReturn);

If you know the times you need to repeat, use a for loop. The while is never ending. So you can get a possible stack overflow...

如果您知道需要重复的时间,请使用for循环。这段时间永远不会结束。所以你可以得到一个可能的堆栈溢出……