0

I want to return data from database using AJAX here the AJAX code for retrieving the data but it can't give it 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]);
                    });
                  }
             });
        });
    });

here the database image where the data is stored.

enter image description here

anybody plz explain me i m new on the stackoverflow so if there is any mistake then sorry. And thankyou for replying me the answers.

this is the server site processing using 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);
  }
  }
Puneet
  • 615
  • 2
  • 7
  • 17
  • `console.log(item[i]);` where is the `item[i]` coming from? Also what is the `data` value? – NnN Feb 19 '18 at 10:43
  • you cant give data as like this: `{bathroom_option1}`, Check the parameters and send like that to server. – swaroop pallapothu Feb 19 '18 at 10:43
  • @swarooppallapothu thnks for replying i m new bigner so can you tell how i solve it. – Puneet Feb 19 '18 at 10:45
  • Edit question with your server side controller method signature. – swaroop pallapothu Feb 19 '18 at 10:48
  • Maybe I will say somehting stupid, but you make an Ajax call : where is the "url" part in your call? Can you edit your post and show us what you get in your console.log() pls? If your problem is what you get as callback in your "success", the problem might be in your php script where you get the data and send them back – Mickaël Leger Feb 19 '18 at 10:48
  • @MickaelLeger I edit the question see the server site processing – Puneet Feb 19 '18 at 10:58

2 Answers2

0

This probably because item is not defined try this

$.each(data, function () {    
   console.log($(this));
});
Vermicello
  • 308
  • 1
  • 6
  • 11
  • I tried this but sorry sir it give this error- TypeError: cannot use 'in' operator to search for 'length' in '15020 – Puneet Feb 19 '18 at 10:46
  • It depends by the format of your "data" try to use JSON.parse method like this $.each(JSON.parse(data), ...); – Vermicello Feb 19 '18 at 10:50
  • can u plz explain to me this thing – Puneet Feb 19 '18 at 11:00
  • i tried it like this '$.each(JSON.parse(data),function(){}); ' it gives error **JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 6 of the JSON data** – Puneet Feb 19 '18 at 11:02
  • This because your json is not well formatted, on your php try to create an array of your rows then json_encode once https://stackoverflow.com/questions/9352057/php-cant-encode-json-with-multiple-rows – Vermicello Feb 19 '18 at 11:12
  • thnku :) for giving me the advices have a nice day. – Puneet Feb 20 '18 at 04:54
0

Ok so try this maybe :

1/ Your ajax call :

$(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/ Your .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

Is this what you are looking for?

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
  • its was an error on** ReferenceError: your is not defined** why its is giving this. my file name is also your.php – Puneet Feb 19 '18 at 11:36
  • Oh ok, be sure to put your .php file at the same level of your script + in the url write the right name of you .php file – Mickaël Leger Feb 19 '18 at 11:38
  • i think console was saying that **your is not defined** it links from file. they both are in same folder. – Puneet Feb 19 '18 at 11:39
  • Don't forget to add some "" ! It should look like this : `url : "pathtophpfile.php",` – Mickaël Leger Feb 19 '18 at 11:40
  • ohhhh great fully thanks to you @mickael leger its working – Puneet Feb 19 '18 at 11:41
  • thnku very very much:) hope so we will meet soon. @mickael leger – Puneet Feb 19 '18 at 11:42
  • Nice, I'm glad I could help you :) – Mickaël Leger Feb 19 '18 at 11:44
  • how i will show the data saparately means when i m writing name[0] then it print the first letter of each but i need **name[0]=data, name[1]=data, name[2]= data**. can you help me – Puneet Feb 19 '18 at 11:53
  • I am not sure about what you want to display, what is "name"? You only have "price", "hours" and "minutes" according to your php. But in your `$.each` loop, maybe try this to get each row in your "name" array : `name.push(this);`. Don't forget to declare `var name = [];` before the loop. Is it good or still not what you want? – Mickaël Leger Feb 19 '18 at 12:29
  • i make the array as you tell me when i m using the **name.push(this)**. the there is an error.**name.push is not a function.** – Puneet Feb 19 '18 at 12:40
  • What do you get if you do a `console.log(this);` in the `$.each` loop? – Mickaël Leger Feb 19 '18 at 13:05
  • **this is i got Object { price: "15", hours: "0", minutes: "20" }** – Puneet Feb 19 '18 at 13:06
  • Ok, so what do you need now as output since your know that every loop you will got one Object with this format? – Mickaël Leger Feb 19 '18 at 13:26
  • yes i need price = name[0]; minutes= name[1]; hours= name[2]; – Puneet Feb 19 '18 at 13:29
  • I'm sorry but what is "name"? I don't see any "name" in your question? But maybe this will be close to what you want : still in your `$.each` loop, add `function(key, name)` then you can do : `name['price']` to get the price, `name['hours']` to get the hours and `name['minutes']` to get the minutes. Try to play with data and if it's not what you want change your array ! Maybe change the php array to get what you want, or rebuild it somewhere else to have exactly what you need ! – Mickaël Leger Feb 19 '18 at 13:38
  • yes sir i do it thnx for it. hope so we meet againor not? @MickaelLeger – Puneet Feb 19 '18 at 13:40
  • Try to do it solo now, if you really don't manage to achieve what you want edit your question to add what you tried and what you really want and I could try to look if I can help you :) – Mickaël Leger Feb 19 '18 at 13:43