Page 1 of 1

Need help changing PHP variable with AJAX

Posted: Sat Sep 10, 2011 3:20 pm
by drayarms
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:

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>	 	 
Here is the AJAX code that's supposed to perform the magic:

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>

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?

Re: Need help changing PHP variable with AJAX

Posted: Sun Sep 11, 2011 10:09 am
by KCAstroTech
Just so you understand, Javascript is client side (on your computer) and php is server side (on the server). The server cannot directly change the client side page unless you refresh the page or change pages.

The problem with your script is that once you send the data you aren't doing anything with what the server returns. I personally would prefer to use jQuery's (http://www.jquery.com) built in ajax request function, capture the results in JSON format and then change the value on the client side using javascript / jquery. However, if that isn't possible then you need a function that catches the ready state change and based on the stated value, do something to the page locally (see: http://www.w3schools.com/ajax/ajax_xmlh ... change.asp).

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.onreadystatechange=function()
                        {
                            if ( pageRequest.readyState==4 &&  pageRequest.status==200)
                            {
                                 document.getElementById("myDiv").innerHTML=pageRequest.responseText;
                            }
                        }

                        pageRequest.send(send_data);

                } else {

                pageRequest.open('GET, url, true);

                pageRequest.send(null);


        }

</script>

Re: Need help changing PHP variable with AJAX

Posted: Mon Sep 12, 2011 3:07 am
by drayarms
@kcastro, well the whole idea was to change the limit (as in number of rows returned) in a mysql query with a simple click event. I don't want the page to be refreshed. Just for the value of that variable to change with the onclick and for the results to be visible on the page. Is that even feasible with the suggestions you made?

Re: Need help changing PHP variable with AJAX

Posted: Wed Sep 14, 2011 11:33 am
by KCAstroTech
Ok. When you set the variable like you did in your example that was for that one instance or call to the server so next time you make a call that value is no longer set. Depending on how the rows are being retrieved you are going to want to either send the number of rows to be returned with the request or use a session variable that will follow that browser session, e.g. $_SESSION['stream_limit']. Then when you send the set_stream_limit it changes the session variable:

Code: Select all

//Define the stream limit variabe.
// You always need to start the session before addressing the $_SESSION variable, if you have already started the session somewhere else in your script then you don't need to start it here
session_start();

if(isset($_POST["send_data"])) { 

        $_SESSION['stream_limit'] = $_POST["send_data"];
 
} else {$_SESSION['stream_limit'] = 4;} 

echo $_SESSION['stream_limit'];
Then modify your mysql query so that when the server goes to retrieve the rows it uses the session variable to know how many rows to get e.g.:

Code: Select all

$query =  "SELECT * FROM `sometable` where `name` = 'some name' LIMIT " . $_SESSION['stream_limit'];
Or if you need an offset with the limit it would look something like:

Code: Select all

$query =  "SELECT * FROM `sometable` where `name` = 'some name' LIMIT " . $_SESSION['stream_offset'] . "," . $_SESSION['stream_limit'];
Where $_SESSION['stream_offset'] is set to the row offset to start from.

That make sense?