Dear Freinds,
I am having a problem in Remote Scripting once i am doing it from Javascript and PHP.On change of the select box i want to fetch the data and create an XML file.but really don't know how to start with.Though i have parsed upto first level but here after things are becoming difficult.
Kindly help me.Below is the sitename i want to fetch the data from and then its the code i have been using.
Instant help of any kind will be deeply appreciated.
Thanking you
Pratiush Dayal
URL is http://www.campusstores.com/ucf/index.asp
and the code snippet is as follows:-
// the html file:
<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 = 'http://www.campusstores.com/ucf/index.asp';
// 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("GET", 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" method='get' onsubmit="send_post(this.name); display_response(); return
false;">
<p>What is your favorite color? <input type="text" name="cboSectProf">
<input type="submit" value="go"></p>
</form>
<div id="responseContainer"></div>
</body>
</html>
// the php script (process.php)
<?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!";
?>