cannon303
07-29-2008, 05:03 AM
Hi I'm having a bit of a problem getting my javascript to send a variable to my php script and getting the php to then return it back to the webpage without refreshing.
here is the javascript to use the correct HTTPrequest as per the users browser. I don't think there is anything wrong with this bit as i'm following an example in a book. This sits in the head of the document:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
<!--
// Create a boolean variable to check for a valid Internet Explorer instance.
var xmlhttp = false;
// CHeck if we are using IE
try{
// If the javascript version is greater than 5.
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
alert ("You are using Microsoft Internet explorer with javascript greater than 5.");
}catch(e) {
//If not, then use the older active x object.
try{
//If we are using internet explorer.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
alert ("You are using Microsoft Internet Explorer with Javascript pre v5");
}catch (E) {
//Else we must be using a non IE browser.
xmlhttp = false;
}
}
//If we are using a non IE browser, create a javascript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
alert ("You are not using I E");
}
function makerequest(serverPage, objID){
var obj = document.getElementById(objID);
xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function(){
//if (xmlhttp.readyState == 4 && xmlhttp.status == 200{
obj.innerHTML = xmlhttp.responseText;
//}
}
xmlhttp.send(null);
}
//-->
Then below this (still in the head) i create a function:
function makerequestPOST(serverPage, objID){
var obj = document.getElementById(objID);
// call the server page to execute the server side operation
xmlhttp.open("POST", serverPage, true);
xmlhttp.onreadystatechange = function(){
//if (xmlhttp.readyState == 4 && xmlhttp.status == 200{
obj.innerHTML = xmlhttp.responseText;
//}
}
xmlhttp.send("param1=x");
}
</script>
</head>
This works if I use GET instead of POST and i attach the string to the end of the link in the body onload below ie 'content3.php?param1=x'. The exercise in the book uses GET although notice that I have commented out the 'if' paramiter as it doesn't work at all when this is not commented out. Not sure why that is, but anyway I need the POST to work.
Below this is the call to the function
<body onload="makerequestPOST('content3.php','hw')">
<div id="hw"></div>
</body>
And here is the content3.php file
<?
$ccphpVar = $_GET['param1'];
if (!ccphpVar){$ccphpVar = $_POST['param1'];};
if ($ccphpVar){
$new = "<p>Yes it works</p>";
} else {
$new = "<p>No doesn't work</p>";
};
echo $new ?>
Basically if i switch the javascript function to GET then the response i get back is "Yes it works" and if I use POST the response is "No doesn't work".
How do I get it to work?:confused:
Thanks for your help.
cc
here is the javascript to use the correct HTTPrequest as per the users browser. I don't think there is anything wrong with this bit as i'm following an example in a book. This sits in the head of the document:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
<!--
// Create a boolean variable to check for a valid Internet Explorer instance.
var xmlhttp = false;
// CHeck if we are using IE
try{
// If the javascript version is greater than 5.
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
alert ("You are using Microsoft Internet explorer with javascript greater than 5.");
}catch(e) {
//If not, then use the older active x object.
try{
//If we are using internet explorer.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
alert ("You are using Microsoft Internet Explorer with Javascript pre v5");
}catch (E) {
//Else we must be using a non IE browser.
xmlhttp = false;
}
}
//If we are using a non IE browser, create a javascript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
alert ("You are not using I E");
}
function makerequest(serverPage, objID){
var obj = document.getElementById(objID);
xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function(){
//if (xmlhttp.readyState == 4 && xmlhttp.status == 200{
obj.innerHTML = xmlhttp.responseText;
//}
}
xmlhttp.send(null);
}
//-->
Then below this (still in the head) i create a function:
function makerequestPOST(serverPage, objID){
var obj = document.getElementById(objID);
// call the server page to execute the server side operation
xmlhttp.open("POST", serverPage, true);
xmlhttp.onreadystatechange = function(){
//if (xmlhttp.readyState == 4 && xmlhttp.status == 200{
obj.innerHTML = xmlhttp.responseText;
//}
}
xmlhttp.send("param1=x");
}
</script>
</head>
This works if I use GET instead of POST and i attach the string to the end of the link in the body onload below ie 'content3.php?param1=x'. The exercise in the book uses GET although notice that I have commented out the 'if' paramiter as it doesn't work at all when this is not commented out. Not sure why that is, but anyway I need the POST to work.
Below this is the call to the function
<body onload="makerequestPOST('content3.php','hw')">
<div id="hw"></div>
</body>
And here is the content3.php file
<?
$ccphpVar = $_GET['param1'];
if (!ccphpVar){$ccphpVar = $_POST['param1'];};
if ($ccphpVar){
$new = "<p>Yes it works</p>";
} else {
$new = "<p>No doesn't work</p>";
};
echo $new ?>
Basically if i switch the javascript function to GET then the response i get back is "Yes it works" and if I use POST the response is "No doesn't work".
How do I get it to work?:confused:
Thanks for your help.
cc