6 Replies - 4660 Views - Last Post: 10 September 2011 - 08:24 PM

#1 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 199
  • Joined: 18-May 11

Need help changing PHP variable with AJAX

Posted 10 September 2011 - 01:21 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:

//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?

Is This A Good Question/Topic? 0
  • +

Replies To: Need help changing PHP variable with AJAX

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Need help changing PHP variable with AJAX

Posted 10 September 2011 - 04:19 PM

How do you know it's not being changed? You're not checking the Ajax's response for 11.
I'm not seeing the purpose of changing the value of $set_stream_limit and not processing it. $set_stream_limit is destroyed when the script ends also so I'd suggest storing it in a SESSION variable or cookie if you want to use it later.
Was This Post Helpful? 0
  • +
  • -

#3 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 199
  • Joined: 18-May 11

Re: Need help changing PHP variable with AJAX

Posted 10 September 2011 - 05:11 PM

View Postcodeprada, on 10 September 2011 - 04:19 PM, said:

How do you know it's not being changed? You're not checking the Ajax's response for 11.
I'm not seeing the purpose of changing the value of $set_stream_limit and not processing it. $set_stream_limit is destroyed when the script ends also so I'd suggest storing it in a SESSION variable or cookie if you want to use it later.



@ codeprada. If u noticed, I echoed the value of $set_stream_limit on the php page. It prints 4 when I run the page and I expect it to change to 11 after the event. I didn't want to go into unnecessary details but since you asked, the purpose of the variable is to set a limit for rows returned from a MySQL search query. I want this value to change with just a simple click.

View Postdrayarms, on 10 September 2011 - 05:08 PM, said:

View Postcodeprada, on 10 September 2011 - 04:19 PM, said:

How do you know it's not being changed? You're not checking the Ajax's response for 11.
I'm not seeing the purpose of changing the value of $set_stream_limit and not processing it. $set_stream_limit is destroyed when the script ends also so I'd suggest storing it in a SESSION variable or cookie if you want to use it later.



@ codeprada. If u noticed, I echoed the value of $set_stream_limit on the php page. It prints 4 when I run the page and I expect it to change to 11 after the event. I didn't want to go into unnecessary details but since you asked, the purpose of the variable is to set a limit for rows returned from a MySQL search query. I want this value to change with just a simple click.



Also I don't need the variable to persist throughout the session. I just need it on one particular page. That's why I don't use a session variable in this case.
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Need help changing PHP variable with AJAX

Posted 10 September 2011 - 06:14 PM

Echoing it makes no difference if there isn't a function assigned to page_request.onreadystatechange to process the response.

Example
page_request.onreadystatechange = function() {
	if(this.readyState == 4 && this.status == 200) {
		//if 11 is alerted then you know it was set
		alert(this.responseText);
	}
}



Also check your error console for any errors.

This post has been edited by codeprada: 10 September 2011 - 06:15 PM

Was This Post Helpful? 0
  • +
  • -

#5 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 199
  • Joined: 18-May 11

Re: Need help changing PHP variable with AJAX

Posted 10 September 2011 - 06:51 PM

View Postcodeprada, on 10 September 2011 - 06:14 PM, said:

Echoing it makes no difference if there isn't a function assigned to page_request.onreadystatechange to process the response.

Example
page_request.onreadystatechange = function() {
	if(this.readyState == 4 && this.status == 200) {
		//if 11 is alerted then you know it was set
		alert(this.responseText);
	}
}



Also check your error console for any errors.


I omitted this step because I wasn't really trying to output anything from the server. Just post some data which in this case is changing a varianle's value. So are you saying that I have to define an oneeadystatechange function in order to achieve this?
Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Need help changing PHP variable with AJAX

Posted 10 September 2011 - 07:03 PM

No you don't have to but for debugging purposes you can only see the result of echo $set_stream_limit; with onreadystatechange.
Was This Post Helpful? 0
  • +
  • -

#7 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 199
  • Joined: 18-May 11

Re: Need help changing PHP variable with AJAX

Posted 10 September 2011 - 08:24 PM

View Postcodeprada, on 10 September 2011 - 07:03 PM, said:

No you don't have to but for debugging purposes you can only see the result of echo $set_stream_limit; with onreadystatechange.




Ok thanks. I'll try that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1