I have a form with validation which will display error in <span class="error" id="rcomment_error"></span>
, somehow I cannot get my errors display if validation failed.
Here I shorthen all the codes.
ajax response if validation failed:
$.ajax({
url: 'request.php',
type: 'POST',
data: params,
dataType: 'json',
success: function(response){
if(response.success == 'success'){
alert('done');
}else{
//if validation failed
$('[id$="_error"]').html('');
$.each(response.error, function(key, value){
if(value){
$('#' + key + '_error').html(value);
}
});
}
},
request.php
// validate input fields
if(empty($rname) || strlen($rname) < 2){
$message['error']['rname'] = 'Enter your name';
}
if(empty($rcomment)){
$message['error']['rcomment'] = 'Your comment is too short';
}
echo json_encode($message);
validation error will display between <span>
literally, below two line of codes are included as part of a file called loadData.php
, when I go to page product.php, loadData.php
will request by ajax and fetched all necessary data and load into <div id="product_list"></div>
, I was speculated this is the reason why the error message cannot be output with callback data.
<input type="text" name="rname"><span class="error" id="rname_error"></span>
<textarea rows="3" name="rcomment"></textarea><span class="error" id="rcomment_error"></span>
I tried to put <span class="error" id="rname_error"></span><span class="error" id="rcomment_error">
in product.php and validate again, this time the errors are shown properly, why is it so?