$.ajax({
type: "GET",
url: 'http://localhost/abc/all-data.php',
data: {
data1: "1"},
success: function(response)
{
alert(response);
}
});
return false;
I want to display each element of array one by one in success function of the ajax currently i get all elements to gether
this is my php code
$i=0;
while($row = mysqli_fetch_assoc( $qry )){
$temp[$i]['c_n'] = $row['c_name'];
$temp[$i]['j_t'] = $row['Job_Title'];
$temp[$i]['des'] = $row['description'];
$temp[$i]['req'] = $row['requirments'];
$temp[$i]['dat'] = $row['posted'];
$i++;
}
$data = array('temp'=> $temp);
echo JSON_encode($temp);
I do appreciate your helps
you probably use something like this in your success function :
response.temp.forEach(function(element){
console.log(element.c_n) ;
console.log(element.j_t) ;
console.log(element.des) ;
console.log(element.req) ;
console.log(element.dat) ;
});
In your success function, you need to json parse your response
var data = JSON.parse(response);
You can access to your data:
data['temp']
If you want your response parsed to json automaticallym you can setup your ajax settings like this:
$.ajaxSetup ({
contentType: "application/json",
dataType: 'json'
});
Then you don't need to call JSON.parse anymore.
Your code:
$i=0; while($row = mysqli_fetch_assoc($qry)){
$temp[$i]['c_n'] = $row['c_name'];
$temp[$i]['j_t'] = $row['Job_Title'];
$temp[$i]['des'] = $row['description'];
$temp[$i]['req'] = $row['requirments'];
$temp[$i]['dat'] = $row['posted'];
$i++;
} $data = array('temp'=> $temp); echo JSON_encode($temp);
Please change the last line as
return JSON_encode($data);
Hope this helps you :)
I send data via json. It is working well:
$.ajax({
url: "json.php",
type: "POST",
dataType: "json",
encode: true,
data: {},
success: function (data) {
$(".blue").html(data.blue);
$(".red").html(data.red);
}
});
json.php
$array['blue'] = "blue array";
$array['red'] = "red content";
echo json_encode($array);
My problem is now, that instead of..
blue array
...I want to send:
$pdo = $db->query('SELECT * FROM data;');
while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
echo "<li>".$row['name']."</li>";
}
is this possible?
Here's an extremely basic example:
<?php
$json = array(
'blue' => '',
'red' => 'Empty red content or whatever'
);
$pdo = $db->query('SELECT * FROM data;');
while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
$json['blue'].= "<li>".$row['name']."</li>";
}
echo json_encode($json);
Not sure what logic you would use to actually populate red but you can work it into the while loop or whatever.
I want to get the return multiple value from my php to ajax, but the problem is I always get a Undefined response from my page. I tried different ways but still getting the same problem
here is my code...
addSizeandPrice.php
if($selectResult){
while($data = mysqli_fetch_assoc($selectResult)){
$dataprice = $data['Price'];
$datasize = $data['CoffeeSize'];
echo json_encode (array(
'size' => $datasize,
'price' => $dataprice,
));
}
}
billingCoffee.php
$.ajax({
url: "addSizeandPrice.php",
type: "POST",
data: {coffeename: txtCoffeeName, sizes: cmbSizes, price: txtPrice},
datatype: "json",
success: function (result){
alert(result.size);
alert(result.price);
}
});
Any ideas and suggestions would be greatly appreciated. . :)
Your json_encode is incorrect. If your DB query returns MULTIPLE rows, then you're sending out MULTIPLE separate json-encoded strings, which is a syntax error. e.g.
{"size":"val1","price":"val1"}{"size":"val2","price":"val2"}
^^--illegal/invalid javascript
You should have:
$arr = array();
while($data = mysqli_fetch_assoc($selectResult)){
$dataprice = $data['Price'];
$datasize = $data['CoffeeSize'];
$arr[] = array(
'size' => $datasize,
'price' => $dataprice,
)
}
echo json_encode($arr);
which would produce:
[{"size":"val1","price":"val1"},{"size":"val2","price":"val2"}]
which is valid javascript/json, and then
success: function(response) { console.log(response[0].size); }
i know how to validate form using it for 1 field. But i would like enter code and take back multiple values from my db. exmp: price, quantity etc.
It is posible to do using ajac?
php file:
$sql = "SELECT * FROM database WHERE code='$field_code'";
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$query=sqlsrv_query($conn, $sql, $params, $options);
$row = sqlsrv_fetch_array($query);
if ($row == true)
{
$code = ($row['code']);
$life = ($row['life']);
$agree = ($row['agree']);
}
echo $code;
echo $life;
echo $agree;
?>
And script is:
$("#field_code").change(function() {
$("#message").html("<img src='pictures/ajax_loader.gif' width='26px' height='26px' /> checking...");
var data1 = $("#field_code").val();
$.ajax({
type: 'POST',
url: 'validation.php',
data: $('form').serialize(),
success: function validate(code) {
if (data == ? ) {
to do something
} else {
to do something
}
How to receive all 3 values from php file?
}
})
you need to use json encode for this
$values[]= array('code'=>$row['code'],
'life'=>$row['life'],
'agree'=>$row['agree']);
echo json_encode($values);
and in ajax
var data = jQuery.parseJSON(data);
and access values like data.life,data.code and data.agree
I have a json_encode in my php script which seems to fail the Jquery side of things if the json_encode is called inside a while loop.
When i view the result in a browser window other than the page i need the result in i can see that it does actually does work.
For example:
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$data = array("id" => $id,
"title" => $title,
"success" => true
);
echo json_encode($data); // Inside the While Loop
}
and the output in the browser is:
{"id":"15","title":"Advanced Booking Examples","success":true}
{"id":"14","title":"Advanced Booking Fields","success":true}
{"id":"11","title":"Add New Driver","success":true}
{"id":"10","title":"Registering a Driver \/ Add New Vehicle","success":true}
{"id":"8","title":"Dispatch Controls","success":true}
{"id":"7","title":"Troubleshooting Driver Device Checklist","success":true}
{"id":"6","title":"Requirements Checklist","success":true}
In the jQuery response this is actually blank but if I leave the echo json_encode($data); just outside the while loop the jQuery picks up the response, however only 1 record - being the last record in the loop.
I need the jQuery to pick up all results from the loop.
Here's the JS
$.ajax({ // Send it off for prcessing
type: "POST",
dataType: 'json',
url: "includes/auto_suggest.php",
data: {
q: inputString
},
success: function (result) {
if (result.success) {
var id = result.id;
var title = result.title;
$('#auto_id').append("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");
$('#auto_title').prepend("<p>" + title + "</p>");
}
}
});
Any ideas?
Thanks in advance
JSON needs to all be on one array or object. You need to append each row to an array, then json_encode that.
$data = array();
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$data[] = array(
"id" => $id,
"title" => $title,
"success" => true
);
}
echo json_encode($data);
Then in JavaScript, you will have an array of objects, that you can loop through.
$.ajax({ // Send it off for prcessing
type: "POST",
dataType: 'json',
url: "includes/auto_suggest.php",
data: {
q: inputString
},
success: function (result) {
$.each(result, function(){
if (this.success) {
var id = this.id;
var title = this.title;
$('#auto_id').append("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");
$('#auto_title').prepend("<p>" + title + "</p>");
}
});
}
});
Well, you are not sending valid JSON.
Store the objects in an array and then json_encode that array after the loop.
$data = array();
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$data[] = array("id" => $id,
"title" => $title,
"success" => true
);
}
echo json_encode($data);
You then need to change your JavaScript code to properly handle an array instead of an object. Since nobody here knows what exactly you intend to do you'll have to do that on your own or provide more information.
You need to add the resulting data to an array and call json_encode once, like so:
$data = array();
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$data[] = array("id" => $id,
"title" => $title,
"success" => true
);
}
echo json_encode($data);
This will change your outputted JSON (since currently your JSON is invalid), which you now have to loop over in your jQuery callback.
The JSON that gets returned when that line is inside the loop isn't valid as a whole. It's multiple JSON strings together. You would need to split them up.
I would make an array and inside the loop add each $data to that array. Then, outside of the loop, json_encode it and echo it. This will create one JSON string to return to your javascript.
Perhaps...
$data = array();
while( WHATEVER ){
// other code
$data[] = array('id' => $id, 'title' => $title, 'success' => true);
}
echo json_encode($data);
Store all your information in one array, then encode that array.
You can't pass multiple objects like that to jQuery. Put the in array and print the array of objects when the while loop is done.
You need to encode json outside the while loop. it will return single json, which is actually correct
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$data[] = array("id" => $id,
"title" => $title,
"success" => true
);
}
echo json_encode($data);
Your JSON output is not valid. Try this complete code (EDIT):
$result = Array();
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$data = array("id" => $id,
"title" => $title,
);
$result[] = $data;
}
echo json_encode(Array('data'=>$result,'success'=>true)); // Outside the While Loop
And in your javascript code
$.ajax({ // Send it off for prcessing
type: "POST",
dataType: 'json',
url: "includes/auto_suggest.php",
data: {
q: inputString
},
success: function (results) {
if (results.success) {
for(var i in results.data) {
var result = results.data[i];
var id = result.id;
var title = result.title;
var $newfield = $("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");
$newfield.prepend("<p>" + title + "</p>");
$('#auto_title').append($newfield);
}
}
}
});