process PHP without leaving page FUNCTION onsubmit="loa


process PHP without leaving page FUNCTION onsubmit="loadXMLdoc()"

如何让它工作

onsubmit

,而不是

onclick with button?

有一个表单:

    <form id="form"> 
    <label>Email:</label>
    <input type="email" name="email" id="email" class="iput" placeholder="jane@doe.com" reguired="required"/>
    <input type="button" id="nbut" onclick="loadXMLDoc()" value="NEXT">
    </form>

按钮可以很好地调用和处理javascript:

function loadXMLDoc() {
 var xmlhttp; 
 if (window.XMLHttpRequest)   {
    // code for IE7+, Firefox, Chrome, Opera, Safari   
    xmlhttp=new XMLHttpRequest();
 } else {
   // code for IE6, IE5   
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 } 
 xmlhttp.onreadystatechange=function()   {   
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {      
       document.getElementById('response').innerHTML=xmlhttp.responseText;
       document.getElementById('step1').style.display = "none";
       document.getElementById('step2').style.display = "block";
     }   
 }
 var em = document.getElementById('email'); 
 var em1 = em.value;
 xmlhttp.open("GET","bin/process.php?email="+em1,true); 
 xmlhttp.send();
 }

这只适用于按钮onclick,我不能让它工作在提交,不知道为什么…

onsubmit<form>元素事件。

你可以很简单地从按钮中删除onclick属性并将窗体更改为

<form id="form" onsubmit="loadXMLDoc(); return false;">

return false阻止默认事件处理程序(表单提交)执行。

使用onsubmit时也应该返回false:

<form id="form" onsubmit="loadXMLDoc(); return false;">