在我的AJAX success
函数中,有一个if
/ else
语句。来自服务器端的响应是可以的,但是当我在这里检查时,if
逻辑与else
部分逻辑一起运行。两者都在同一时间运行,这对我来说有点混乱。
$.ajax({
url: 'centre-upload-process2.php',
type: 'POST',
data: formData,
async: false,
success: function (response) {
if(response == 'success') {
$('#centreformupload').hide();
$('#centreformpayment').show();
console.log(typeof response);
} else {
alert("Already present");
console.log(typeof response);
}
},
contentType: false,
processData: false
});
PHP
<?php require_once '../core/class_init.php'; ?>
<?php
$centre_uploads = new dbhandler();
$flag = false;
$client_id = $_POST['client_id'];
$r_id = $_POST['r_id'];
$path = new uploads();
$payment_data = $centre_uploads->all_data('payment_dtls');
$i = 0;
foreach($payment_data as $pd) {
if($payment_data[$i]->fy == input::get('fy')) {
$flag = true;
}
}
if(isset($_POST) && isset($_FILES)) {
//$r_id = $_POST['r_id'];
$inputtext2 = $_POST['inputtext2'];
$inputfile2 = $path->upload_image('inputfile2');
$inputtext3 = $_POST['inputtext3'];
$inputfile3 = $path->upload_image('inputfile3');
$inputtext4 = $_POST['inputtext4'];
$inputfile4 = $path->upload_image('inputfile4');
if(empty($inputtext2) && empty($inputfile2)) {
$inputtext2 = 'NA';
$inputfile2 = 'NA';
}
if(empty($inputtext3) && empty($inputfile3)) {
$inputtext3 = 'NA';
$inputfile3 = 'NA';
}
if(empty($inputtext4) && empty($inputfile4)) {
$inputtext4 = 'NA';
$inputfile4 = 'NA';
}
if(!$flag) {
try{
$centre_uploads->create('payment_dtls' , array(
'r_id' => $r_id,
'client_id' => $client_id,
'fy' => input::get('fy'),
'ay' => input::get('ay')
));
$centre_uploads->create('upload_dtls' , array(
'r_id' => $r_id,
'client_id' => $client_id,
'pan_file' => $path->upload_image('pan_file'),
'bank_file' => $path->upload_image('bank_file'),
'insurance_file' => $path->upload_image('insurance_file'),
'title1' => $inputtext2,
'document1_file' => $inputfile2,
'title2' => $inputtext3,
'document2_file' => $inputfile3,
'title3' => $inputtext4,
'document3_file' => $inputfile4,
'purpose' => input::get('purpose')
));
echo 'success';
}catch(Exception $e){
die($e->getMessage());
}
} else {
echo 'denied';
}
}
如果响应为1,那么数据将被插入到数据库中但我也得到alert
并且在此处写入响应1的逻辑不起作用(from不隐藏)而是我得到警报。< / p>
答案 0 :(得分:0)
听起来你的回答是整数1,而不是字符串&#39; 1&#39;。您可以通过添加以下内容来仔细检查返回的内容:
console.log(response, typeof response)
你的考试:
if (response === '1') {
要求response
是字符串&#39; 1&#39;,数字1将不匹配。因此,centre-upload-process2.php
中的处理工作正常,记录添加到数据库中,并返回整数1.这不是Javascript所期望的字符串,因此它被解释为错误,并显示您的警报。
假设这是真的并且你得到一个整数,请更新你的JS以期望一个整数:
if (response === 1) {