如果数据为空,我想返回 true,如果数据不为空,我想返回 false。如果数据为空,则继续执行表单操作,否则留在当前页面。
代码是:
<script type="text/javascript">
function error()
{
var response = false;
$.ajax({
url: BASE_URL + 'auth/validateForm',
type: 'POST',
data: {
surname: $("#r-surname").val()
},
dataType: "json",
success: function(data) {
if(data.surname)
{
$('#error_surname').html(data.surname);
response = false;
}else{
$('#error_surname').html('');
response = true;
}
},
error: function(data) {
return false;
}
});
alert(response);
return response;
}
</script>
<form action="{$BASE_URL}contul-meu" method="post" name="registration" onsubmit="return error()">
<table>
<tr>
<td>
<label for="r-surname">Prenume*</label>
</td>
<td>
<input type="text" class="text" id="r-surname" name="surname" value="{$user['surname']}"/>
</td>
<td><small id="error_surname" class="err"></small></td>
</tr>
</table>
</form>
php:
public function validateForm()
{
$surname = $this->input->post('surname');
$data = array();
if(strip_tags($surname) == '')
{
$data['surname'] = 'Prenumele este invalid!';
}
echo json_encode($data);
}
var response 只是 false。如果没有错误,如何使响应为 true?