Previously, I was using AJAX post with the normal dataType 'html'. However, I have now switched to using dataType 'json'.
Before implementing json, I used to split response errors and display them next to the input fields using spans.
This is the old AJAX success code for splitting errors when using 'html' as the data type:
success:
function(data){
var data_array = split('*');
for(var i=0; i<data_array.length-1; i++)
{
messgae_array = data_array[i].split(':');
$("#"+messgae_array[0]+"_error").html(messgae_array[1]);
$("#"+messgae_array[0]).css({"border":"1px solid red"});
}
}
This is an example of an input field in the form:
<input type="text" id="uname" name="uname" value="" class="inplaceError" />
<span id="uname_error"></span>
I have more inputs but have just shown an example of how they look.
For each input, there is a corresponding span element holding the errors from the AJAX response.
Now, the challenge lies in updating the code that previously worked with HTML responses to work with JSON responses. As someone new to JavaScript/AJAX/JSON, I am unsure of how to modify the split code to handle JSON responses. Any assistance would be greatly appreciated.
Edit:
The error messages are generated like this:
if(isset($_POST['p']) && !empty($_POST['p']))
{
if($password == $retype)
{
$random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));
// Create salted password (Careful with the chilli)
$password = hash('sha512', $password.$random_salt);
}
else
{
$message['password']=' password not match';
}
and so on .
$message['email']=' wrong email';
And here is the foreach loop generating JSON array for the errors:
foreach($message as $key => $val) {
$return = array('error' => $key, 'message' => $val );
echo json_encode($return);
}
Something appears to be incorrect in the foreach loop for the errors, resulting in incorrect handling in the AJAX response code. Any insights on what might be going wrong?