2015-11-06 64 views
-1

我已創建的HTML形式並且處理經由一個PHP頁中的數據,我想接收/顯示同一頁(索引的「插入/失敗」通知。 html)。我使用的是javascript,ajax和jquery。數據在數據庫中更新,但我在process.php頁面上獲得結果。我想在index.html頁面本身上插入結果通知。形式使用javascript,AJAX PHP,jquery的

的index.html

<html> 
<head><title>Registration form</title></head> 
<body> 
<form id="myForm" action="process.php" method="POST"> 
Username: <input type="text" name="username"> <br /> 
Password: <input type="password" name="pass"> <br /> 
First name: <input type="text" name="fname"> <br /> 
Last name: <input type="text" name="lname"> <br /> 

<button id="submit" name="submit">Register</button> 
</form> 

<div id="ack">Acknowledge</div> 

<script type="text/JavaScript" src="script/jquery-1.11.3.min.js"></script> 
<script type="text/JavaScript" src="script/my_script.js"></script> 
</body> 
</html> 

my_script.js

$("#submit").click(function(){ 


    $.post($("#myForm").attr("action"), 
      $("#myForm :input").serializeArray(), 
      function(info){ 
      $("#ack").empty(); 
      $("#ack").html(info); 
      clear(); 
      }); 
$("#myForm").submit(function(){ 
    return false; 
    }); 
}); 

function clear(){ 

    $("#myForm:input").each(function()){ 
     $(this).val(""); 
    } 
} 
+0

粘貼process.php頁面的代碼。 –

回答

0

試試這個:

$("#submit").on('click',function(e){ 
    e.preventDefault(); 
    $.post(
      $("#myForm").attr("action"), 
      $("#myForm :input").serializeArray(), 
      function(info,status,xhr){ 
       $("#ack").empty(); 
       $("#ack").html(info); 
       clear(); 
      } 
    ); 
}); 

function clear(){ 
    $("#myForm:input").each(function(){ 
     $(this).val(""); 
    }); 
} 

PHP頁面(我在呼應PHP頁面 「成功」,所以相同的內容將反映在HTML頁面中):

<?php echo "success";?> 

輸出:

success 
+0

感謝人,,,它的工作。非常感謝您的幫助。 :) –

+0

不客氣:)只是想知道,爲什麼它還沒有被接受;) – Santhosh