我如何在javascript中提醒来自php的值回显
请注意,我尝试过ALERT(DATA),但由于php中出现了问题,因此无法成功进行验证
我担心的是,当我尝试进行验证时,即使php中的数据或回显为“ Error1”,“ Error2”或“ Error3”,即使想到还是导入成功,也请帮忙。 / p>
这是我的ajax
<script>
$(document).ready(function(){
$('#upload_csv').on("submit", function(e){
e.preventDefault(); //form will not submitted
$.ajax({
url:"import.php",
method:"POST",
data:new FormData(this),
contentType:false, // The content type used when sending data to the server.
cache:false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data){
if(data == 'Error1')
{
alert("Error1");
}
else if(data == 'Error2')
{
alert("Error2");
}
else if(data == 'Error3')
{
alert('Error3');
}
else
{
alert('Import Successfull');
$('#employee_table').html(data);
}
}
})
});
});
</script>
这是php代码
<?php
if(!empty($_FILES["employee_file"]["name"]))
{
$connect = mysqli_connect("localhost", "root", "", "db");
$output = '';
$allowed_ext = array("csv");
$tmp = explode(".", $_FILES["employee_file"]["name"]);
$extension = end($tmp);
if(in_array($extension, $allowed_ext))
{
$file_data = fopen($_FILES["employee_file"]["tmp_name"], 'r');
fgetcsv($file_data);
while($row = fgetcsv($file_data))
{
$try = count($row);
if($try === 6){
$id = mysqli_real_escape_string($connect, $row[0]);
$name = mysqli_real_escape_string($connect, $row[1]);
$address = mysqli_real_escape_string($connect, $row[2]);
$gender = mysqli_real_escape_string($connect, $row[3]);
$designation = mysqli_real_escape_string($connect, $row[4]);
$age = mysqli_real_escape_string($connect, $row[5]);
$query = "
INSERT INTO tbl_employee
(id,name, address, gender, designation, age)
VALUES ('$id','$name', '$address', '$gender', '$designation', '$age')
";
mysqli_query($connect, $query);
}else
{
echo 'Error3';
break;
}
}
echo $output;
}
else
{
echo "Error1";
}
}
else
{
echo "Error2";
}
?>
答案 0 :(得分:0)
alter()
无法输出复杂数据。
尝试使用console.log()
。
数据将出现在浏览器控制台中,而不是直接弹出。 您可以按F12,然后单击控制台以查看
答案 1 :(得分:0)
使用json尝试返回值, 在js上
<script>
$(document).ready(function(){
$('#upload_csv').on("submit", function(e){
e.preventDefault(); //form will not submitted
$.ajax({
url:"import.php",
method:"POST",
dataType: 'JSON',
data:new FormData(this),
contentType:false, // The content type used when sending data to the server.
cache:false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data){
// look into detail json data
console.log(data);
if(data['status'] == false)
{
alert(data['error']);
} else
{
alert('Import Successfull');
$('#employee_table').html(data);
}
}
})
});
});
</script>
在php
<?php
if(!empty($_FILES["employee_file"]["name"]))
{
$connect = mysqli_connect("localhost", "root", "", "db");
$output = '';
$allowed_ext = array("csv");
$tmp = explode(".", $_FILES["employee_file"]["name"]);
$extension = end($tmp);
if(in_array($extension, $allowed_ext))
{
$file_data = fopen($_FILES["employee_file"]["tmp_name"], 'r');
fgetcsv($file_data);
while($row = fgetcsv($file_data))
{
$try = count($row);
if($try === 6){
$id = mysqli_real_escape_string($connect, $row[0]);
$name = mysqli_real_escape_string($connect, $row[1]);
$address = mysqli_real_escape_string($connect, $row[2]);
$gender = mysqli_real_escape_string($connect, $row[3]);
$designation = mysqli_real_escape_string($connect, $row[4]);
$age = mysqli_real_escape_string($connect, $row[5]);
$query = "INSERT INTO tbl_employee
(id,name, address, gender, designation, age)
VALUES ('$id','$name', '$address', '$gender', '$designation', '$age')";
mysqli_query($connect, $query);
}else{
$status = false;
$error = 'Error3';
$output = [];
}
}
$status = true;
$error = [];
$output = $output;
} else {
$status = false;
$error = "Error1";
$output = [];
}
} else {
$status = false;
$error = "Error2";
$output = [];
}
header('Content-Type: application/json');
echo json_encode(array('status'=>$status,'output'=>$output,'error'=>$error), JSON_PRETTY_PRINT);
?>
对不起,我的英语不好
答案 2 :(得分:0)
使用console.log()测试您的数据,警报无法获取很多数据