Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with xmlHTTPRequest and form submission.

    Code:
    <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

  • #2
    Regular Coder godofreality's Avatar
    Join Date
    Jan 2009
    Posts
    234
    Thanks
    1
    Thanked 15 Times in 15 Posts
    well this is how i would do it....

    html
    Code:
    <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
    PHP Code:
    $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
    Code:
    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);
    		}
    }
    Last edited by godofreality; 06-04-2009 at 07:13 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •