drayarms Posted September 10, 2011 Share Posted September 10, 2011 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: //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: <a href ="javascript:;" onclick = "set_stream_limit('member.php' , 11);"> Set Limit </a> Here is the AJAX code that's supposed to perform the magic: <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> 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? Quote Link to comment https://forums.phpfreaks.com/topic/246863-need-help-changing-php-variable-with-ajax/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.