代码之家  ›  专栏  ›  技术社区  ›  Puneet Poonam Navapara

如何使用ajax分别从数据库返回数据(整数)

  •  0
  • Puneet Poonam Navapara  · 技术社区  · 6 年前

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

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

    任何人都可以向我解释我是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 回复  |  直到 6 年前
        1
  •  0
  •   Mickaël Leger    6 年前

    好吧,试试这个:

    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
    

    这就是你要找的吗?

        2
  •  0
  •   Vermicello    6 年前

    这可能是因为未定义项,请尝试以下操作

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