|
|
|
|
|
|
| |
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 html file:
| [code]
<html>
<head>
<title>Remote Scripting Example</title>
<script type="text/javascript">
// load the appropriate xmlHttpRequest for IE or Mozilla
// this sniffer code can be found at
// http://jibbering.com/2002/4/httprequest.html
var xmlHttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP")
} catch (e) {
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
} catch (E) {
xmlHttp=false
}
}
@else
xmlHttp=false
@end @*/
if (!xmlHttp) {
try {
xmlHttp = new XMLHttpRequest();
}
catch (e) {
xmlHttp=false
}
}
// end jibbering.com code
// 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.
function get_data($var_name) {
global $$var_name;
$raw = $GLOBALS['HTTP_RAW_POST_DATA'];
$pairs = explode('&',$raw);
for($i=0;$i<sizeof($pairs);$i++) {
$unencoded = urldecode($pairs[$i]);
$pattern = "/($var_name)=([a-zA-Z0-9\, ]+)/";
if(preg_match($pattern,$unencoded,$matches)) {
$$matches[1]=$matches[2];
}
}
}
get_data('color');
// 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.
print "I agree, $color is my favorite, too!";
?> | | |
|
| Dynamic Calender in PHP, Javascript and HTML. Categories : PHP, Java Script, HTML and PHP, Calendar | | | Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | | | The Ajax Tree view class fetches data from a db for the requested parent category id. The data is then stored in an array and converted into JSON (Javascript Object Notation) format. This format is then used by JavaScript for populating tree view. Categories : PHP, PHP Classes, Java Script, AJAX, Databases | | | Zephyr: AJAX Based Framework for PHP5 Developers Categories : PHP, AJAX, Frameworks, Java Script, Web Applications | | | Dynamic generation of textboxes, select items etc in a table for use with databases applications, matrimonials and for job sites Categories : PHP, HTML and PHP, Java Script | | | Tree Menu Dynamic (+Static) with Loading in Progress.. Categories : PHP, Java Script, HTML and PHP | | | OverEasy - PHP generated JavaScript to do mouseovers on
your pages. Modify one file and one function does it all
for you! Categories : PHP, Java Script, HTML and PHP, MySQL | | | This PHP function creates dropdown select lists for time and date that you can change, outputs a 14 char MySQL timestamp in a text field Categories : PHP, MySQL, Java Script, HTML and PHP | | | Function that allows a Javascript cookie to be set after HTML has been outputted to the page.
Categories : PHP, Java Script, Cookies, HTML and PHP | | | Local-to-user date and time display regardless of time zone or where the website's server is located Categories : PHP, Date Time, HTML and PHP, Java Script | | | PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use! Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP | | | ezRemoteScripter - A little remote scripting (AJAX) helper Categories : PHP, Java Script, AJAX | | | PHP4 session helper HTML file.
Categories : PHP, Java Script, HTML and PHP, Sessions | | | Newbie Notes #9 - Hyperlinking a post Categories : PHP, Java Script, HTML and PHP, Beginner Guides | | | Validator 98 - a PHP-script to generate form-validation-code in JavaScript. Categories : Complete Programs, Java Script, PHP, HTML and PHP | |
| | | | Simon Speich wrote : 988
to access $_post[] variables you just need to set the content type differently:
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
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?
regards,
DESPERATE
| |
|
|
|