I have a problem with handling json variables in javascript.
The problem appears when I try to post data to a php class and then it should return 3 variables using JSON, but Firebug + FirePHP told me that the JSON variable returned correctly. Then when when I try to store each of them in different variables (3 returned json variables in 3 javascript variables) I just get the first parameter and the other two are NULL.
You can see in next javascript code:
function UpdateContact(ID)
{
// get current Contact data
var full_name,email,mobile;
$.post("/Cards/index.php/Cont/GetContactInfo",{'id' : ID},
function(data){
full_name = data.full_name;
email = data.email;
mobile = data.mobile;
$( "#dialog2-form" ).html(
'<p class="validateTips">All form fields are required.</p>'+
'<form>'+
'<fieldset>'+
'<label for="name">Name</label>'+
'<input type="text" name="Updatefull_name" id="Updatefull_name" class="text ui-widget-content ui-corner-all" value="'+full_name+'" /><br />'+
'<label for="email">Email</label>'+
'<input type="text" name="Updateemail" id="Updateemail" value="" class="text ui-widget-content ui-corner-all" value="'+data.email+'"/><br />'+
'<label for="email">Mobile</label>'+
'<input type="text" name="Updatemobile" id="Updatemobile" value="" class="text ui-widget-content ui-corner-all" value="'+data.mobile+'"/><br />'+
'<input type="hidden" id="UpdateContactID" value="'+ID+'"></fieldset>'+
'</form>'
);
$( "#dialog2-form" ).dialog( "open" );
}, "json");
}
Just the full_name variable is getting a value, but email and mobile variables do not get any value.
The PHP function is:
function GetContactInfo()
{
$Contact = $this->Contacts->GetContacByID($this->input->post('id'));
$data['full_name'] = $Contact->full_name;
$data['email'] = $Contact->email;
$data['mobile'] = $Contact->mobile;
echo json_encode($data);
}
Note: I use CodeIgniter PHP Framework on my XAMPP localhost server
You need to check that the fields are present before using them:
I suggest you use a construct similar to the above for reading your JSON data.