[英]How To Getting error when submitting a form by ajax
how to Getting error when submitting a form by ajax to page msg.php 如何通过ajax向页面msg.php提交表单时出现错误
file 1 = msg.php 文件1 = msg.php
<?php
$id = $_SESSION['id'];
$touser = htmlspecialchars($_GET['iduser']);
$postid = htmlspecialchars($_GET['post']);
?>
<div id="send">
<div id="title">صندوق المحادثة</div>
<form id="my-form" method="post" enctype="multipart/form-data">
<textarea id="_text" name="text" required=""></textarea>
<input id="_from" name="from" type="hidden" value="<?php echo $id; ?>"/>
<input name="to" type="hidden" value="<?php echo $touser; ?>"/>
<input name="post" type="hidden" value="<?php echo $postid ?>" />
<div class="file">
<li>ملفات .zip فقط</li>
<input class="up" type="file" name="up" />
</div>
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token_madmoun']; ?>" />
<button class="submit">ارسال الان</button>
</form>
<script>
$( '#my-form' )
.submit( function( e ) {
$.ajax({
url: 'chat_a.php',
type: 'POST',
data: new FormData( this ),
processData: false,
contentType: false
} );
e.preventDefault();
document.getElementById("my-form").reset();
});
</script>
</div>
chat_a.php chat_a.php
<?php
include "config.php";
if(!$user->is_logged_in()){
header('Location: unregistered.php');
exit();
}
if (isset($_POST['csrf_token']) && $_POST['csrf_token'] === $_SESSION['csrf_token_madmoun']) {
$id = $_SESSION['id'];
$data = date('Y-m-d');
$time = time();
$post = htmlspecialchars($_POST['post']);
$to = htmlspecialchars($_POST['to']);
$file_name = $_FILES['up']['name'];
$file_size = $_FILES['up']['size'];
$FileType = pathinfo($file_name,PATHINFO_EXTENSION);
if(!empty($_POST['text'])){
if(empty($FileType)) {
$sqladdcontent = $db->prepare("INSERT INTO chat_a SET _from = :_from, _to = :_to, _post = :_post, _data = :_data, _time = :_time, _text = :_text");
$sqladdcontent->bindParam(':_from', $id);
$sqladdcontent->bindParam(':_to', $to);
$sqladdcontent->bindParam(':_post', $post);
$sqladdcontent->bindParam(':_data', $data);
$sqladdcontent->bindParam(':_time', $time);
$sqladdcontent->bindParam(':_text', htmlspecialchars($_POST['text']));
$sqladdcontent->execute();
}else {
if($FileType != "zip" && $FileType != "ZIP") {
$error = "<center><div id='no-ok'>قم برفع ملفات بصيغة .zip فقط</div></center>";
}else {
if ($file_size > 104857600) {
$error = "<div id='no'>ممنوع حجم الملف اكبر من 100 ميجا</div>";
}else {
$time_digit = time() . '_';
$new_file_name = $time_digit.'.zip';
move_uploaded_file($_FILES['up']['tmp_name'], "upload-msg/".$new_file_name);
$sqladdcontent = $db->prepare("INSERT INTO chat_a SET _from = :_from, _to = :_to, _post = :_post, _data = :_data, _time = :_time, _text = :_text, _file = :_file");
$sqladdcontent->bindParam(':_from', $id);
$sqladdcontent->bindParam(':_to', $to);
$sqladdcontent->bindParam(':_post', $post);
$sqladdcontent->bindParam(':_data', $data);
$sqladdcontent->bindParam(':_time', $time);
$sqladdcontent->bindParam(':_text', htmlspecialchars($_POST['text']));
$sqladdcontent->bindParam(':_file', $new_file_name);
$sqladdcontent->execute();
}
}
}
}
}
?>
how to Getting error when submitting a form by ajax to page msg.php The name of the variable is the error = $error 如何通过ajax向页面msg.php提交表单时出现错误变量名称为error = $ error
collect errors in php script as $errors array, then finaly ouput it as JSON: 将PHP脚本中的错误收集为$ errors数组,然后最终将其输出为JSON:
print json_encode($errors);
and process it in success event handler 并在成功事件处理程序中对其进行处理
<script>
$( '#my-form' )
.submit( function( e ) {
$.ajax({
url: 'chat_a.php',
type: 'POST',
data: new FormData( this ),
processData: false,
contentType: false,
success: function(msg)
{
json = $.parseJSON(msg);
if (json.error[1] != "") //
$("#div-error-1").html(json.error[1]);
if (json.error[2] != "") //
$("#div-error-2").html(json.error[2]);
// and so on. Or you can use foreach construction. it will make code more universal
}
} );
e.preventDefault();
document.getElementById("my-form").reset();
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.