You've made a GUI for your web interface, but everytime you submit a form, the browser redraws
the page. Here's a way to submit the form WITHOUT having to wait for the browser to download the
GUI all over again.
OK, I know we're supposed to be using XML, but why translate it back and forth? This lets you
work with php variables and html as you're used to working with it :)
This example is known to work for IE6 (Win), NN7 (Win) and Mozilla 1.5.1 (Linux). This example
uses nonstandard javascript, so don't expect everybody to be able to use it.
// the php script to process the form. var must be global!
var URLto = 'process.php';
// function to build POST requests
function buildPOST(theFormName) {
theForm = document.forms[theFormName];
var qs = ''
for (e=0;e<theForm.elements.length;e++) {
if (theForm.elements[e].name!='') {
var name = theForm.elements[e].name;
qs+=(qs=='')?'':'&'
qs+= name+'='+escape(theForm.elements[e].value);
}
}
qs+="\n";
return qs
}
// function to communicate with remote script
function send_post(theFormName) {
var xmlMessage = buildPOST(theFormName);
xmlHttp.open("POST", URLto, false)
// for ie compatability
xmlHttp.setRequestHeader('Content-Type','text/html')
xmlHttp.send(xmlMessage)
}
function display_response() {
var optionDiv = document.getElementById("responseContainer");
optionDiv.innerHTML = xmlHttp.responseText;
}
</script>
</head>
<body>
<!-- note: form must be named! -->
<form name="form1" onsubmit="send_post(this.name); display_response(); return false;">
<p>What is your favorite color? <input type="text" name="color">
<input type="submit" value="go"></p>
</form>
<div id="responseContainer"></div>
</body>
</html>
The php script (process.php)
[code]
<?php
// get data. note, $_POST[] does not seem to work.
// output response. you now have the variables in the current scope, and you
// could query a database for information, but for now, just show that we have
// received the variable passed to the script.
instead of using the get_data function, just use the $_post vars!
Tim Morton wrote :989
You know, that`s the first way I tried it, and I swear it didn`t work... But then again, I was using the raw post and not $_POST by that time.
I tried it again using the regular $_POST variable and setRequestHeader(`Content-Type`,`application/x-www-form-urlencoded`) and it worked fine in IE, NN7, and Mozilla
Thanks for the improvement! :)
Ming Chern Foo wrote :1044
Hi, has anyone testing using HTML editors(javascripts) with remote scripting?
I`m currently trying to make it work but to no avail...
Can anyone help me?