Reputation: 783
I try to use jQuery ajax call to retrive some data via php script. Script take from mysql table all values of column 'rezhour' what also based on $_GET variable.
Then he print all finded values via json_encode() function
PHP:
<?php
header('Content-type: application/json');
$fromdate = $_GET['fromdate'];
$getrezhiredh = mysql_query("
SELECT rezhour FROM rezhiredhours
WHERE rezdate = '".$fromdate."' ORDER BY rezhour
");
$i=0;
$data = array();
while($row = mysql_fetch_array($getrezhiredh)){
$data['hours'][$i] = $row['rezhour'];
$i++;
}
$result = array(
$data
);
print json_encode($result);
?>
When script is called from browser adress bar all is fine and in we have correct result as below:
[{"hours":["2","9","13","14"]}]
Each digit in result above corresponds selected table row.
The problem is when i want to do the same via ajax.
jQuery:
<script>
var date = jQuery('input').val()
jQuery.ajax({
type: "GET",
dataType: "html",
data: "fromdate="+date,
url: "script.php",
success: function (data) {
console.log(data);
},
});
</script>
Now all is fine only when mysql query return result with one row and this looks in console like this:
[{"hours":["1"]}]
When mysql query return result with more than one row console show this:
[[]]
For me is really weird that jquery ajax call have problem to print correct result when we have more than one rows with selected data.
What i must do to have the same result in AJAX?
EDIT:
I found an important clue. The problem is WHERE clause in MYSQL query. When u remove from query this clause, php script return data from entire table and now ajax can print into console all selected values from specifed column. The problem is i must use WHERE clause to load only data from before selected date at the website. Any idea how to do this?
Upvotes: 2
Views: 517
Reputation: 4584
If you want to return json data ,must use dataType:json. Valid data format is data :{key:value},...
var date = jQuery('input').val();
jQuery.ajax({
type: "GET",
dataType: "json",
data: {fromdate : date },
url: "script.php",
success: function (data) {
console.log(data);
},
});
Upvotes: -1