
09-04-2009, 22:16
|
|
|
|
חבר מתאריך: 08.07.05
הודעות: 144
|
|
|
שליחת טפסים ב AJAX
היי לכולם, החלטתי ללמוד ולהתנסות ב AJAX עכשיו זה טכנולוגיה מאוד גאונית ואני מאוד אוהב את זה, רק שהבעיה שלי שאני מצליח להעביר רק פרמטר 1 :S
נסיתי לקרוא פעמיים לפעולה שלי makeRequest, אבל זה עדין העביר רק את הפרמטר הראשון, אז נסיתי ליצור אובייקט נוסף שינסה לשמש אותי והנה מה שיצא, אבל עדין מעביר רק את הראשון
קוד PHP:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1255" />
<script language="javascript" type="text/javascript"> function getXmlHttpRequestObject() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); //Mozilla, Safari ... } else if (window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); //IE } else { //Display our error message alert("Your browser doesn't support the XmlHttpRequest object."); } }
//Our XmlHttpRequest object var receiveReq = getXmlHttpRequestObject(); var receiveReq2 = getXmlHttpRequestObject();
//Initiate the AJAX request function makeRequest(url, param, param2) { //If our readystate is either not started or finished, initiate a new request if (receiveReq.readyState == 4 || receiveReq.readyState == 0) { //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) receiveReq.open("POST", url, true); //Set the function that will be called when the XmlHttpRequest objects state changes receiveReq.onreadystatechange = updatePage;
receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); receiveReq.setRequestHeader("Content-length", param.length); receiveReq.setRequestHeader("Connection", "close"); //Make the request receiveReq.send(param); }
if (receiveReq2.readyState == 4 || receiveReq2.readyState == 0) { //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) receiveReq2.open("POST", url, true); //Set the function that will be called when the XmlHttpRequest objects state changes receiveReq2.onreadystatechange = updatePage;
receiveReq2.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); receiveReq2.setRequestHeader("Content-length", param2.length); receiveReq2.setRequestHeader("Connection", "close"); //Make the request receiveReq2.send(param2); } }
//Called every time our XmlHttpRequest objects state changes function updatePage() { //Check if our response is ready if (receiveReq.readyState == 4) { //Set the content of the DIV element with the response text document.getElementById('result').innerHTML = receiveReq.responseText; }
if (receiveReq2.readyState == 4) { //Set the content of the DIV element with the response text document.getElementById('result2').innerHTML = receiveReq2.responseText; }
}
//Called every time when form is perfomed function getParam(theForm,url) { //Set up the parameters of our AJAX call var postStr = theForm.text.name + "=" + encodeURIComponent( theForm.text.value ); var postStr2 = theForm.b.name + "=" + encodeURIComponent( theForm.b.value ); //Call the function that initiate the AJAX request makeRequest(url, postStr); makeRequest(url, postStr2); } </script> </head> <body>
<form id="frmCaptcha" name="frmCaptcha"> <input id="text" type="text" name="text" value="" /> <input id="b" type="text" name="b" value="" /> <input type="button" value="בדוק שם משתמש" onclick="getParam(this.form,'try.php')" /> </form>
<div id="result"> </div>
<div id="result2"> </div>
</body> </html>
אם יש דרך להעביר טופס שלם בדרך יותר חכמה/יעילה אשמח לשמוע, אני מגגל את זה כבר כמה שעות ואני לא מוצא אנשים שמעבירים יותר מפרמטר אחד
_____________________________________
|