2011-11-08 25 views
0

我怎麼得到這個工作過程PHP不離開頁面功能的onsubmit = 「loadXMLDoc()把」

的onsubmit

,而不是

一個按鈕

的onclick?

我有一個表格:

<form id="form"> 
    <label>Email:</label> 
    <input type="email" name="email" id="email" class="iput" placeholder="[email protected]" 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,我不能去工作提交併不知道爲什麼..

回答

2

onsubmit<form>元素事件。

你可以很簡單地刪除的按鈕onclick屬性和執行改變你的形式

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

return false阻止默認的事件處理程序(表單提交)。

+0

感謝您解釋返回false! –

1

使用的onsubmit你也應該返回false:

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