Current location: Hot Scripts Forums » Programming Languages » PHP » Php/ajax


Php/ajax

Reply
  #1 (permalink)  
Old 11-18-11, 02:35 PM
DLJ DLJ is offline
Newbie Coder
 
Join Date: Nov 2011
Location: 303 See Other
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Question Php/ajax

First off, I understand that PHP is server-side, and Javascript is client-side. I also understand that typically you need to post Javascript data to the server to retrieve it with PHP.

That being said, I'm have a rather difficult time with a script my boss is wanting.

Is there any way to use AJAX to dynamically pass a Javascript variable to PHP without a page refresh? I need to change the contents of "text.txt" on the server to the value of a text input on the page, but I've read that you cannot modify server contents with Javascript, which is where the PHP comes in. The problem is, PHP runs before Javascript, therefore I'm having a difficult time passing the JS variable through to the server.

Any help is much appreciated.
Reply With Quote
  #2 (permalink)  
Old 11-19-11, 04:43 AM
phplabs phplabs is offline
Newbie Coder
 
Join Date: Oct 2011
Posts: 37
Thanks: 0
Thanked 7 Times in 7 Posts
hi,

perhaps you could have it work this or similar way:

1. a php script that saves the data to the file, such as save.php. in that script, you take a GET/POST variable, perhaps do some validation, and update the file. it is a separate script, its only input is GET/POST data, and its only output might be a one word message: success or error, or a number like 1 or 0.

2. a piece of javascript code that sends an ajax request and optionally shows the response. here you make a request to the save.php and, if needed, show an appropriate message (alert box or whatever) whether the action was successful.

if you are using or willing to use jquery, this link might be useful: http://api.jquery.com/jQuery.post/
__________________
blog.phplabs.net
Reply With Quote
  #3 (permalink)  
Old 11-21-11, 10:45 AM
DLJ DLJ is offline
Newbie Coder
 
Join Date: Nov 2011
Location: 303 See Other
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
So if I were to write an AJAX function calling a php script at timed intervals, it could repetitively run a php script, as opposed to the server normally parsing it only once? Forgive my lack of knowledge - ajax is still painfully new to me.
Reply With Quote
  #4 (permalink)  
Old 11-21-11, 02:08 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
AJAX is like a new request to the server. You can pass any type of Javascript variables to the server without having to refresh the page, and then have PHP to retrieve and handle it. It can be done in time intervals as well.

You can retrieve the POST (or GET) data in your PHP script just like you would retrive normal POST and get data, using PHP's $_POST and $_GET variables.
Reply With Quote
  #5 (permalink)  
Old 11-21-11, 02:38 PM
DLJ DLJ is offline
Newbie Coder
 
Join Date: Nov 2011
Location: 303 See Other
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Alright, sweet. I'm beginning to understand.

Here's my AJAX function:

Code:
function ajaxRequest(){
	var requestData; 
	
	try{
		// Opera 8.0+, Firefox, Safari
		requestData = new XMLHttpRequest();
	} catch (e){
		// IE
		try{
			requestData = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				requestData = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Your browser is brokeded.
				alert("Please try again later!");
				return false;
			}
		}
	}
	// Send/receive data
	requestData.onreadystatechange = function(){
		if(requestData.readyState == 4){
			document.getElementById("testDiv").innerHTML = requestData.responseText;
		}
	}
	requestData.open("GET", "tester.php", true);
	requestData.send(null); 
}
"tester.php" is a simple echo command, a random string of numbers.

Is there any way I can use my newfound AJAX capabilities to set/update global PHP variables to use in different scripts on the same page?

I've found that:

PHP Code:

global $myvariable
Doesn't really work all that well, at least I can't seem to get it to.
Reply With Quote
  #6 (permalink)  
Old 11-21-11, 04:01 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Well yes and no.

I assume that you want to change this global variable after the main page has already been parsed and outputted. In that case, no. The script is only parsed once, and sent to the user. After that, there's no way to modify the variables on the server side because they've already been reased from the memory, and they're lost.

You can however modify a global variable within your AJAX request, but I'm not sure if that helps you.

Or you can save the new value in a database, and read it later on the next page request.
Reply With Quote
  #7 (permalink)  
Old 11-21-11, 04:07 PM
DLJ DLJ is offline
Newbie Coder
 
Join Date: Nov 2011
Location: 303 See Other
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
The program I'm writing is an online interface for a neural net, so it's all on a single dynamic page. I need to change variables according to the neural net, to plot points on a graph and draw splines. The last bit I need to do is to find a way to make my variables able to change, based upon what the neural net writes. Would this be possible by using the "parse_ini_file($file);" command, and having the AJAX function dynamically call the parse function in set time intervals, and having the net output to the ini file?
Reply With Quote
Reply

Bookmarks

Tags
ajax, javascript, php, script


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I will happily pay for this: PHP/AJAX script request Ricki4 Script Requests 0 03-06-11 05:42 PM
Forms, Formulas, Drap n Drop with PHP/AJAX mademarest PHP 0 09-27-06 04:49 PM


All times are GMT -5. The time now is 08:54 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.