PDA

View Full Version : Need help with xmlHTTPRequest and form submission.


syntheticstar
06-04-2009, 01:52 AM
<form name="contact" action="http://www.sureazure.com/gdform.php" method="post">
<input type="hidden" name="subject" value="SureAzure.com Information Request" />
First Name <input name="01_First_Name" type="text" size="25" /><br />
Last Name <input name="02_Last_Name" type="text" size="25" /><br />
<a href="javascript:loadXMLDoc('home.txt');document.contact.submit();">Submit </a><br />
<a href="javascript:document.contact.reset()">Reset</a>
</form>


What am I doing wrong guys? I've tried it a dozen different ways and I can't seem to get the syntax right. This form is in a text file inserted into a div. Once the form is submitted, it is supposed to load a different text file into the div (ex: "Thanks for submitting the form! Now go away and do something else mindless on the interweb!") If I only use one of the functions (submit() or loadXMLDoc()), it works the way it is supposed to. How do I get both functions to work properly together? Any help in this matter is greatly appreciated!

- Anthony

godofreality
06-04-2009, 08:07 AM
well this is how i would do it....

html

<div id="display">
First Name <input id="First_Name_01" type="text" size="25" /><br />
Last Name <input id="Last_Name_02" type="text" size="25" /><br />
<input type="button" value="Reset" onclick="reset()" />
<input type="button" value="Submit" onclick="submit()" />
</div>


action.php

$First_Name_01 = $_POST['First_Name_01'];
$Last_Name_02 = $_POST['Last_Name_02'];
/*
place chunk of code to do what it is suppose to do with the posted data after this
*/
echo "New text to be placed into the page";


now for the javascript

function reset()
{
document.getElementById('First_Name_01').value = "";
document.getElementById('Last_Name_02').value = "";
}

function submit()
{
var trick = Math.floor(Math.random()*100000);
var First_Name_01 = document.getElementById('First_Name_01').value;
var Last_Name_02 = document.getElementById('Last_Name_02').value ;
var XMLHttp = false;
if (window.XMLHttpRequest)
{
XMLHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (XMLHttp)
{
XMLHttp.open("POST", "action.php?"+trick);
XMLHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
XMLHttp.onreadystatechange = function()
{
if (XMLHttp.readyState==4)
{ document.getElementById('display').innerHTML = XMLHttp.responseText;
}
}
XMLHttp.send("First_Name_01="+First_Name_01+"&Last_Name_02="+Last_Name_02);
}
}