如何通过ajax提交表单时出错

时间:2017-04-10 08:31:45

标签: 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>

    <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

<?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();

}
        }
     }
    }

}

?>

如何通过ajax向页面msg.php提交表单时出错 变量的名称是error = $ error

1 个答案:

答案 0 :(得分:0)

将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>
相关问题