2017-04-10 242 views
0

提交表單時如何得到錯誤阿賈克斯提交表單頁面時,如何獲取錯誤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 變量的名稱是錯誤= $錯誤

提交表單時收到錯誤10
+1

的[可能的複製如何返回正確的JQuery .ajax()使用PHP的成功/錯誤消息?](http://stackoverflow.com/questions/9676084/how-do-i-return-a-proper-success-error-message-for-jquery-ajax -using-php) –

回答

0

收集錯誤的PHP腳本爲$ errors數組,然後finaly輸出中它作爲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> 
+0

謝謝如果文件中有兩條錯誤消息,我想在Div –

+0

中顯示錯誤代碼不起作用 –

+0

sh ow修改html和php – diavolic