我如何使用ajax单独从数据库返回数据(整数)

时间:2018-02-19 10:32:41

标签: javascript jquery ajax

我想使用AJAX从数据库返回数据 这里是用于检索数据的AJAX代码,但它不能给它带来数据。

  $(document).ready(function(){
    $('#bathroom-select').change(function(){
        var bathroom_option1 =$(this).val();
        console.log(bathroom_option1);
        $.ajax({
            type:'POST',
            data:({bathroom_option1}),
            success:function(data){
                price1=parseInt(data);
                console.log(price1);
                var rows;
                $.each(data, function (key, name) {    //this will not work 
                    console.log(item[i]);
                    });
                  }
             });
        });
    });

这里是存储数据的数据库图像。

enter image description here

任何人plz解释我在stackoverflow上是新的,所以如果有任何错误,那么抱歉。 谢谢你回答我的答案。

这是使用php

处理的服务器站点
 $con=mysqli_connect("localhost","root","","price");
   if (isset($_POST['bathroom_option1'])) {
    $query=mysqli_query($con,"select * from bathroom where number_of_bathrooms ='$_POST[bathroom_option1]'");
   while ($row = mysqli_fetch_array($query)) {
    echo json_encode($row['price'],JSON_NUMERIC_CHECK);
    echo json_encode($row['hours'],JSON_NUMERIC_CHECK);
    echo json_encode($row['minutes'],JSON_NUMERIC_CHECK);
  }
  }

2 个答案:

答案 0 :(得分:0)

这可能是因为没有定义项目试试这个

$.each(data, function () {    
   console.log($(this));
});

答案 1 :(得分:0)

好的,试试吧:

1 /你的ajax电话:

$(document).ready(function(){
    $('#bathroom-select').change(function(){
        var bathroom_option1 =$(this).val();
        console.log(bathroom_option1);
        $.ajax({
            type:'POST', // you send data with POST method
            data:{ "bo1" : bathroom_option1}, //the data you send
            dataType : "JSON", // what you will get as anwser data type
            url : yourphp.php, // your .php where you send your data
            success:function(response){
                var data = response.data; // your anwser array with one row = one item with price, hours and minutes

                // See why in my PHP, but here you can get the message and the type you send with "response.message" and "response.type", can be usefull if you want to do something different according to what happen in your .php

                $.each(data, function (key, name) {   
                    console.log(this); // one row 
                });
              }
         });
    });
});

2 /你的.php:

$result = array();    

$con=mysqli_connect("localhost","root","","price");
if (isset($_POST['bo1'])) { // you get your data with post method here, with the key you used in your call, here 'bo1'
    $query=mysqli_query($con,"select * from bathroom where number_of_bathrooms ='$_POST['bo1']'");
    while ($row = mysqli_fetch_array($query)) {
       // You add each row in your $result['data'] array
       $result['data'][] = array(
           "price" => $row['price'],
           "hours" => $row['hours'],
           "minutes" => $row['minutes']
       );
      } // end while
  } // end if

  $result['message'] = "All is ok !";
  $result['type'] = "success";

  echo json_encode($result);

 // You can send back what you want as message or type for example, if you have some test to do in your php and it fails send back $result['type'] = "warning" for example

这是你在找什么?