Im having a problem with my php/jquery script. The PHP script is suppose to get an array of fields from a MySQL database, then parse it to JSON and echo it so that i can grab it in my Jquery script.
The problem is that the Jquery script is not grabbing the data correctly, or it is not handling the JSON correctly – or the last possibility that I am doing something wrong (which might be the case because Jquery is a new field of work for me).
I have tried a lot of different Jquery scripts i found on the internet, but here is my current code:
PHP:
<?php
include("../../config.php");
$roomId = $_POST['roomId'];
$data = mysql_query("SELECT field FROM fields WHERE room = '$roomId'
AND value = 1 AND TYPE = ''") or die(mysql_error());
while ($users = mysql_fetch_array($data)) {
echo json_encode($users);
}
?>
The Jquery script:
function UpdateRoom() {
var data = 'roomId='+roomId;
$.ajax({
type: "POST",
url: "chatfunctions/getplacementfield.php",
dataType: 'json',
data: data,
success: function(data){
var arrayValues = $.parseJSON(data);
$.each(arrayValues, function() {
$('#f' + parseInt(arrayValues.field).append('<div id="user" />');
});
}
});
}
Anyone who can tell me what I am doing wrong. If this is bad Jquery or PHP then tell me what’s wrong as I mentioned earlier I am new to Jquery and not an expert in PHP.
PS: I am not getting any erros in the firebug console
Try this, i hope you will get an idea from this eg. –
PHP:
Jquery: