Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AJAX-type form submission

Status
Not open for further replies.

existenzi

Programmer
Oct 5, 2005
1
CA
I think the problem is with the submit button you are using:
Code:
<input type="submit" name="Submit" value="Submit" onClick="loadXMLDoc('[URL unfurl="true"]http://secure.comodo.net/products/!DecodeCSR')">[/URL]
The onClick is doing all the right things... but the form is then submitted (just after the onClick ends) and the page reloads and your ajax query is lost.

Try using a regular link for test purposes:
Code:
<a href="javascript:loadXMLDoc('[URL unfurl="true"]http://secure.comodo.net/products/!DecodeCSR')">Test</a>[/URL]

And if that works, you could just add return false; to the end of your onclick on the current submit button to replicate the same behaviour.

Hope that helps!
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
You'll need to "open" the request first(before setting header):

Code:
<script type="text/javascript">
var xmlhttp
		  
function loadXMLDoc(url)
{

// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest()

 xmlhttp.open("POST",url,true)
 xmlhttp.setRequestHeader("Content-Type","text/plain")
 
  xmlhttp.onreadystatechange=state_Change
    xmlhttp.send(null)
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp)
    {
    xmlhttp.open("POST",url,true)
 xmlhttp.setRequestHeader("Content-Type","text/plain")
 
    xmlhttp.onreadystatechange=state_Change
    xmlhttp.send(null)

    }
  }
}

function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
  {
  // if "OK"
  if (xmlhttp.status==200)
  {
  alert("XML data OK")
  document.getElementById('A1').innerHTML=xmlhttp.status
  document.getElementById('A2').innerHTML=xmlhttp.statusText
  document.getElementById('A3').innerHTML=xmlhttp.responseText
  }
  else
  {
  alert("Problem retrieving XML data:" + xmlhttp.statusText)
  }
  }
}

function noSubmit(){
return false;
}
</script>
</head>

<body>
<h2>Form</h2>
<form method=post onsubmit="return noSubmit()">
Enter the csr: <textarea name="csr" cols="10" rows="6"></textarea>
<input type="submit" name="Submit" value="Submit" onClick="loadXMLDoc('[URL unfurl="true"]http://secure.comodo.net/products/!DecodeCSR')">[/URL]

<p><b>response:</b>
<br><span id="A1"></span><span id="A2"></span><span id="A3"></span>

</p>
</form>

Thanks,
--Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top