• 0

[Javascript] !URGENT! how stop post and get values from form


Question

help me i have this javascript and it puts a page into and div

i need to be able to post into the div

all i need to do is stop the form from submitting and onsubmit get the values of the form e.g. <input type="text" name="post" value="abc"> and get the javascript to be able to put that in the url

which i need to know how to do and place it in that and it should work.. i have page.php to tell me if it works or not xD

function realTimePost(form)
{	
var xmlhttp;

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="page.php";
url=url+"&amp;post="+INPUTVAR-HERE IS WHAT I NEED;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}

thanks in advance :)

Edited by Evil Penguin
Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

We're gonna need more. How is the html structured? You can post to a div, you can't either post to a window, and iframe (or object), or using XmlHttpRequest.

Link to comment
Share on other sites

  • 0

i only have

<div id="myDiv">

and

<form action="" method="GET" onSubmit="realTimePost();">

<input type="text" id="postBox" name="post">

<input type="submit" id="submit">

</form>

i assume thats what you mean?.. of course that html can be changed

Link to comment
Share on other sites

  • 0

I'm assuming you mean, you want to stop it posting normally,and post via XmlHttpRequest instead?

If so, you can probably do something like:

var postForm = function() {
	var xmlHttp;
	var stateChanged = function() {
		if (xmlHttp.readyState == 4) {
			document.getElementById("myDiv").innerHTML = xmlHttp.responseText;
		}
	};

	var url = "page.php&amp;post=";

	url += document.getElementById("postBox").value;

	xmlHttp = GetXmlHttpObject();

	xmlHttp.open("POST", url, true);
	xmlHttp.send("");

	return false;
};

&lt;form onSubmit="return postForm()" ...&gt;

The import part in getting it to work how you expect, is to return false to the onSubmit() method. This stops the browser from posting the form, and allows your XmlHttpRequest to take over.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.