我必须比较我从ajax中的其他php文件获取值的第一个字符串。
AJAX
<script type='text/javascript'>
$(document).ready(function() {
$("#curr_add").click(function(e) {
e.preventDefault();
var curr_code = $("#curr_code").val();
var curr_name = $("#curr_name").val();
var curr_desc = $("#curr_desc").val();
var curr_remark = $("#curr_remark").val();
$.ajax({
type: "POST",
url: "add_employee_currency.php",
data: {
data: curr_code,
Cur_name : curr_name,
Cur_desc : curr_desc,
Cur_remark: curr_remark
},
success: function(msg){
//I have to split my msg string here before compare.
if(msg == '"Error"'){
$("#d1").html(msg); //show validation error in my modal dialogue
}
else
{
$("#mainform").html(msg); //display drop down list to my main form
$('div#currency_modal').dialog('close');
}
},
error:function(){
alert('error');
}
});
});
});
</script>
PHP
<?php
$code = $_POST['data'];
$name = $_POST['Cur_name'];
$desc = $_POST['Cur_desc'];
$remark = $_POST['Cur_remark'];
$checkCurrencyCode = base_checkCurrencyCode($code);
$checkCurrencyCode2 = base_checkLength($code, 3, 3);
$checkCurrencyCode3 = base_generalValidate($code, 'alpha', 3);
if($code == "")
{
$err_code = "<br /><font color=red>".str_replace("##CONTENT##","Currency Code",ERR_BLANK_CONTENT_FIELD)."</font>";
}
elseif($checkCurrencyCode == false)
{
$err_code = "<br /><font color=red>".str_replace("##CONTENT##","Currency Code",ERR_CONTENT_EXISTS)."</font>";
}
elseif($checkCurrencyCode2 == false)
{
$err_code = "<br /><font color=red>".str_replace("##CONTENT##","Currency Code",ERR_CORRECT_FORMAT)."</font>";
}
elseif($checkCurrencyCode3 == false)
{
$err_code = "<br /><font color=red>".str_replace("##CONTENT##","Currency Code",ERR_CORRECT_FORMAT)."</font>";
}
if($name == "")
{
$err_name = "<br /><font color=red>".str_replace("##CONTENT##","Currency Name",ERR_BLANK_CONTENT_FIELD)."</font>";
}
if(($code != "") && ($checkCurrencyCode == true) && ($checkCurrencyCode2 == true) && ($checkCurrencyCode3 == true) && ($name != ""))
{
$err_msg = base_add_Currency($code,$name,$desc,$remark);
$success = "Success";
}
if($success == "Success")
{
$dropdown = base_generateCurrencyDropdown($empCurrency,"estaf_emp_currency");//this query is shows out new currency drop down list in my main form
}
else
{
//I have to split this with only show "ERROR" in order to compare in my AJAX
echo json_encode("Error");
echo "\n";
echo $err_code ."\n".$err_name;
}
?>
我的验证结果看起来像这样:
"Error"
* Please enter Currency Code. OR *Please enter your Currency Code in correct format.
* Please enter Currency Name.
我只想在上面的字符串中输入错误,以便在我的ajax中进行比较
答案 0 :(得分:0)
你能否替换
if(msg == '"Error"'){
$("#d1").html(msg); //show validation error in my modal dialogue
}
与
if(msg.indexOf('"Error"') != -1){
$("#d1").html(msg); //show validation error in my modal dialogue
}
在你的success
函数中,基本上检查你的返回数据是否包含刺痛&#34;错误&#34; (带引号)?
如果你想更具体,并说出&#34;错误&#34; 成为字符串的第一部分:
if (msg.indexOf('"Error"') == 0){
有关.indexOf的更多信息,请访问:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
答案 1 :(得分:0)
可能缺少的内容类型:
$aReturn[ 'error' ] = TRUE;
$aReturn[ 'error_code' ] = $err_code;
$aReturn[ 'error_name' ] = $err_name;
$sJson = json_encode( $aReturn, 1 );
header( 'Content-type', 'application/json' );
echo $sJson;