below is the code that I use to retrieve an array from a php files using a json ajax jquery method (refer below)
$('.edituser').click(function(e) {
$.ajax({
type: 'POST',
url: 'processor.php',
data: { jkey: 'jedituser', post_id: this.id },
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
success: function(result) {
$('.ename').val(result[0]);
$('.ename').val(result[1]);
$('.edisplay_name').val(result[2]);
$('.eusername').val(result[3]);
$('.epassword').val(result[4]);
$('.eemail').val(result[5]);
},
});
// prevent the link's default behavior
e.preventDefault()
});
and this is the content of my php file which bind with the json function (refer below
jkey = $_REQUEST['jkey'];
switch ($jkey)
{
case "jedituser":
$post_id = $_POST['post_id'];
$sql = "SELECT * FROM user_meta WHERE id='$post_id'";
$result = mysqli_query($con, $sql);
// Mysql_num_row is counting table row
$count = mysqli_num_rows($result);
// If there is a result then
if($count==1){
$value = mysqli_fetch_object($result);
$id=$value->id;
$name=$value->name;
$display_name=$value->display_name;
$username=$value->username;
$email=$value->email;
// Register $myusername, $mypassword and redirect to file "login_success.php"
$array = array($id,$name,$display_name,$username,"Fill password if you want to change it..",$email);
echo json_encode($array);
}else {
echo "No match files. try different query and try again";
}
break;
default:
echo "Shall not waste time, creature!";
}
and this is the element that trigger the json function
<a href="processor.php" id="1" class="edituser">Get the data</a>
and assume that I have those elements where the result array from the processor.php will be put on if json is success. Now the problem is, nothing is return or seems no data has been thrown back to the json data receive function yet when i tried to change the success data function to alert(result) while remove the dataType: 'json' and then changing what inside the case jedituser to echo "saasad", its seems working and also I tried to switch to plain ajax and its also working. I guess there something wrong with the json function i use. What suppose be the problem why this is not working? any ideas, recommendations and suggestions is greatly appreciated. Thanks in advance.
dataType
and do aconsole.log(result);
in the success function? Perhaps you have additional output, causing the result to be non-valid json. – jeroen Mar 26 at 21:35