Php 如何通过ajax提交表单时出错
如何通过ajax将表单提交到页面msg.php时出错 文件1=msg.phpPhp 如何通过ajax提交表单时出错,php,ajax,Php,Ajax,如何通过ajax将表单提交到页面msg.php时出错 文件1=msg.php <?php $id = $_SESSION['id']; $touser = htmlspecialchars($_GET['iduser']); $postid = htmlspecialchars($_GET['post']); ?> <div id="send"> <div id="title">صندوق المحادثة</div> &
<?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>
صندوق المحادثة
将php脚本中的错误收集为$errors数组,然后最终输出为JSON:
print json_encode($errors);
并在成功事件处理程序中处理它
<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>
$(“#我的表格”)
.提交(职能(e){
$.ajax({
url:“chat_a.php”,
键入:“POST”,
数据:新表单数据(本),
processData:false,
contentType:false,
成功:功能(msg)
{
json=$.parseJSON(msg);
如果(json.error[1]!=“”)/
$(“#div-error-1”).html(json.error[1]);
如果(json.error[2]!=“”)/
$(“#div-error-2”).html(json.error[2]);
//等等。或者您可以使用foreach构造。它将使代码更通用
}
} );
e、 预防默认值();
document.getElementById(“我的表单”).reset();
});
如果文件中有两条错误消息,并且我想在Div$error=“قمبكمب㶕㶕㶕㶕㶕㶡.zipف”中显示错误,则可能重复“谢谢”;打印json_编码($error)$error=“مبفعملتتبيغة.zipفط”;打印json_编码(数组(“错误”=>array($error));不要与我一起工作,你可以在msg.php和chat_a.php页面中为我编写完整的代码
<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>