2016-07-11 35 views
0

我正在嘗試做一個ajax調用,然後顯示一條成功消息。 我有一個PHP窗體,它張貼一些數據。 這是我的代碼。顯示一條Ajax成功消息

<script> 
$(document).ready(function() { 
    $('#valuesubmit').click(function(event) { 
     var formData = new FormData($('form#devAdd')[0]); 
     $.ajax({ 
      url: 'php/addDevice.php', //Server script to process data 
      type: 'POST', 
      // Form data 
      data: formData, 
      contentType: false, 
      processData: false, 
      dataType: 'json', 
      success: function(response){ 
       $('#settingsMessage').html(response["msg"]); 
      } 
     }); 
    }); 
}); 
</script> 

然後有一個PHP文件,這使得相應的檢查之前提交。

這個文件生成一個消息

if(trim($Name) == '') { 
    echo json_encode(array('msg' => 'Name, is required.')); 
} else if(trim($DevID) == '') { 
    echo json_encode(array('msg' => 'State, is required')); 
} else if(trim($IconID) == '') { 
    echo json_encode(array('msg' => 'Town, is required')); 
} 

所以最後一部分應顯示settingsMessage DIV裏面這條消息。

錯誤在哪裏?

回答

0

控制檯中是否有任何錯誤?試試這個:

<script> 
$(document).ready(function() { 
    $('#valuesubmit').click(function(event) { 
    var formData = new FormData($('form#devAdd')[0]); 
    $.ajax({ 
     url: 'php/addDevice.php', //Server script to process data 
     type: 'POST', 
     // Form data 
     data: formData, 
     dataType: 'json', 
     success: function(response){ 
      $('#settingsMessage').html(response.msg); 
     } 
    }); 
    }); 
}); 
</script> 

你有空:success: function(response){

+0

它沒有工作...... – user3464267

+0

你用id'settingsMessage' HTML元素?檢查控制檯。在'$('#settingsMessage')。html(response.msg)之前做一個'console.log(response);'但是首先檢查PHP是否返回了你想要的答案(用postman或者從POST GET並在URL上設置參數)。並用HTML編輯問題 – Mikel

+0

它沒有工作... – user3464267