Need help changing PHP variable with AJAX
Posted: Sat Sep 10, 2011 3:20 pm
Hello folks, hope everyone is having a great Sunday. Well I'm trying to use AJAX as the topic suggests, to change the value of a PHP variable with an onclick event. The variable ($set_stream_limit) is assigned a default value of 4 and I'm trying to change this value to 11 with the onclick event. So the idea is just to simply post some data to the server, onto the same file (member.php) that contains the variable and the onclick link. I'm not trying to get any response text back from the server. So somewhere on member.php page, I initialize the variable like so:
and somewhere down the page, I include the onclick event:
Here is the AJAX code that's supposed to perform the magic:
Well I expect the printed value of 4 to change to 11 when I click on the link but it doesn't. Am I making any sense at all with this approach? If so, where am I going wrong?
Code: Select all
//Define the stream limit variabe.
if(isset($_POST["send_data"])) {
$set_stream_limit = $_POST["send_data"];
} else {$set_stream_limit = 4;}
echo $set_stream_limit;
and somewhere down the page, I include the onclick event:
Code: Select all
<a href ="javascript:;" onclick = "set_stream_limit('member.php' , 11);"> Set Limit </a>
Code: Select all
<script type="text/javascript">
function set_stream_limit(url, data_to_send){
var page_request = false;
if (window.XMLHttpRequest) page_request = new XMLHttpRequest();
else if (window.ActiveXObject) page_request = new ActiveXObject("Microsoft.XMLHttp");
else return false;
if (data_to_send) {
var send_data = 'send_data=' + data_to_send;
pageRequest.open ('POST' , url , true);
pageRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
pageRequest.send(send_data);
} else {
pageRequest.open('GET, url, true);
pageRequest.send(null);
}
</script>