With PHP, sending form data is easy. You use two files; a send.php:
- Code: Select all
<html>
<body>
<form action="receive.php" method="get">
<input name="txt_example" type="text" /><br />
<input name="submit" type="submit" />
</form>
</body>
</html>
And a receive.php:
- Code: Select all
<html>
<body>
<? echo $_REQUEST["txt_example"]; ?>
</body>
</html>
Obviously you can process the sent information any way you like once you receive it with the receiving file (in other words you don't just have to echo it out).
After you click the Submit button it redirects you to receive.php, but it appends a query to the end. So it would say in your address bar: http://localhost/receive.php?txt_example=hello.
Now here is the only way I know how to do it with AJAX:
- Code: Select all
<html>
<head>
<script language="javascript" type="text/javascript">
var XMLHttpRequestObject_test = false;
var strSend;
if (window.XMLHttpRequest) {
XMLHttpRequestObject_test = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject_test = new ActiveXObject("Microsoft.XMLHTTP");
}
function test()
{
if (XMLHttpRequestObject_test) {
strSend = "receive.php?txt_example=" + document.getElementById('txt_example').value;
XMLHttpRequestObject_test.open("GET", strSend);
XMLHttpRequestObject_test.onreadystatechange = function ()
{
if (XMLHttpRequestObject_test.readyState == 4 && XMLHttpRequestObject_test.status == 200) {
document.getElementById('here').innerHTML = XMLHttpRequestObject_test.responseText;
}
}
XMLHttpRequestObject_test.send(null)
}
}
</script>
<body>
<input id="txt_example" type="text" /><br />
<input id="submit" type="button" onclick="test()" />
<br /><br />
<div id="here"></div>
</body>
</html>
Since I tend to use a lot of AJAX, I thought it would be a good idea to automate the process with a class, which works just fine - until you consider that each AJAX needs to be able to send unique text boxes.
So is it possible to somehow send form through AJAX, so I can just use generic script to send the data instead having to custom write each AJAX script?

