PDA

View Full Version : Send POST/GET Request Without Reloading Page.



boxxertrumps
02-02-2007, 12:31 AM
I Need Something Ajax-ey. there are several Divs Styled to match a style sheet that the left of my layout, and i hope to find a script that allows me to access a php file that sets a new style without interrupting the user's activities. (I have An IRC applet and games and music and junk on my site...)

Maybe a notification after if its not too much trouble.

EDIT:
searched for a bit, is this right?

function go(number)
{
var str = "style=number";
var url = "http://boxxer.mooo.com/index.php";// No question mark needed
xmlReq.open("POST",url,true);
xmlReq.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");
xmlReq.send(str);
}

Then In the Div onclick="go(1)"

tech_support
02-03-2007, 05:26 AM
function makeRequest(url, eId, getpost, senddata) {
if(getpost == "undefined" || getpost != "POST" || getpost != "GET") {
getpost = "GET";
}
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
// See note below about this line
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}

http_request.onreadystatechange = function() {
if (http_request.readyState == 4 && http_request.status == 200) {

document.getElementById(eId).innerHTML = http_request.responseText;
}
else {
document.getElementById(eId).innerHTML = "Please wait..."
}
};
http_request.open(getpost, url, true);
if(getpost == "POST") {
http_request.send(senddata);
} else {
http_request.send(null);
}
}

Untested but should work.

boxxertrumps
02-03-2007, 05:43 PM
Ive changed your code a bit to better suit my needs...


<script type="text/javascript">

function makeRequest(senddata) {
getpost = "POST";
var http_request = false;
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) {
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}

http_request.onreadystatechange = function() {
if (http_request.readyState == 4 && http_request.status == 200) {

document.getElementById("changeThis").innerHTML = http_request.responseText;
} else {
document.getElementById("changeThis").innerHTML = "Please wait..."
}
};
http_request.open(getpost, http://boxxer.mooo.com/cookie.php, true);
if(getpost == "POST") {
http_request.send(style=senddata);
} else {
http_request.send(null);
}
}
</script>
ive Set Cookie.php to create a dump text file for all $_POST[style] values, but none have shown up...

Forgot the divs...

<a href="#" onclick="makeRequest(3)"><div class="sample" onclick="makeRequest(3)" style="border-color:#ffaaaa #550000 #550000 #ffaaaa;background-color:#bb7777;"></div></a>
<p id="changeThis">Push Buttons To Change Colors.</p>

tech_support
02-04-2007, 04:03 AM
...
http_request.send(style=senddata);
...


Forgot the ' .

Twey
02-04-2007, 02:34 PM
As in:
http_request.send('style=senddata');
ive Set Cookie.php to create a dump text file for all $_POST[style] values, but none have shown up...Use $_POST['style']. Relying on PHP to convert an undefined constant to a string is decidedly bad practice.

boxxertrumps
02-04-2007, 05:08 PM
script works. Done is Echoed...
heres cookie.php

<?php
$handle = $_POST[style];
$f = fopen("dump.txt","a");
fwrite($f,$handle);
fclose($f);
$coo = setcookie("style",$handle,time()+3600*24*30*12,/,boxxer.mooo.com);
if ($coo) {echo "Done. Refresh to view new colors.";}
else {echo "Failed.";};
?>

Am I Doing It Wrong?

mburt
02-04-2007, 05:24 PM
It should work, but generally the $handle variable executes the fopen function, and in this case $f should contain the data to replace with.

Twey
02-04-2007, 05:43 PM
<?php
$handle = $_POST['style'];
$f = fopen("dump.txt","a");
fwrite($f,$handle);
fclose($f);
$coo = setcookie("style",$handle,time()+3600*24*30*12,/,'boxxer.mooo.com');
if ($coo) {echo "Done. Refresh to view new colors.";}
else {echo "Failed.";}
?>Don't forget those quotes! In many cases, PHP will convert an undefined constant to a string, but as I said above, relying on this is bad practice. It certainly won't try to convert / into '/', since / is an operator.

boxxertrumps
02-04-2007, 05:57 PM
I set up a form to send post data to cookie.php, and its fine.

the javascript is the problem, it wont send the data.
function makeRequest(value) {

... Object xmlhttp defined ...

xmlHttp.open("POST","http://boxxer.mooo.com/cookie.php",true);
xmlHttp.send('style=value');
}
<a href="#" onclick="makeRequest(0)">

mburt
02-04-2007, 05:57 PM
I completely missed that, but Twey's right. You need to put quotes around the "style" post

boxxertrumps
02-04-2007, 08:02 PM
yeah, i set up the form HERE (http://boxxer.mooo.com/?form)
inputting numbers 0-4 choses the styles you can get, but if you try the widget on the side, it only accesses the file and doesnt send any info to it.