a little PHP help

Absentee

[H]F Junkie
Joined
Aug 27, 2004
Messages
9,410
here is what I would like to do in PHP:

I have a page setup that just has a bunch of links on it. each link runs a certain command on the computer the page is hosted on. so when you click on a link it goes to

http://address.com/page.php?blah=COMMAND

and just basically reloads that same page. Is it possible to easily send the command, but NOT load that page again. instead just keep it on that page without it reloading? the reason is, I want to open this page on my blackberry, and i have to wait 1-2 seconds between clicking on each command because I have to wait for the page to reload again.

a way to imagine the type of thing I want it to do, is the edit functionality on this board. it does something without actually reloading the page.
 

Yea, after googling a bit i figure thats what I'd need

I am a total noob to ajax though, so could anyone help me get started on the code I would need to have to:

have a regular link on a page. when you click on it, it sends a console command to the server (I use "exec("command")" in my current PHP config)
after doing that it just sits there, and stays on the same page. no reloads, no nothing.

in the meantime, I'll be reading.
 
Instead of making calls via the URL string, why not make each link a seperate page alltogether ..... the 'index' page would show each loadable page as a link and would not be reloaded when a link is clicked.

I don't see why ajax is needed, it is a very simple process from what I gathered.

I still don't understand why using one page is a problem. Have the page do nothing until it's given a command. While using if/else statements to see if the URL variables are set (if you are avoiding throwable warnings from PHP - depending on how PHP is setup), have it execute the command if the $_GET variable is set.
 
Yea, after googling a bit i figure thats what I'd need

I am a total noob to ajax though, so could anyone help me get started on the code I would need to have to:

have a regular link on a page. when you click on it, it sends a console command to the server (I use "exec("command")" in my current PHP config)
after doing that it just sits there, and stays on the same page. no reloads, no nothing.

in the meantime, I'll be reading.

Couldn't you just use javascript for this?

like an <a href="#" onClick='something'... or <a href="javascript:function()"
and just call your "command script" that way?
 
Couldn't you just use javascript for this?

like an <a href="#" onClick='something'... or <a href="javascript:function()"
and just call your "command script" that way?

well from what I understand, you can't send commands to the console with javascript. which is why, after a bit of reading, I believe ajax (javascript + PHP) is what I need.

I just have no idea how to implement it.

for example, using php, I can click a link that performs the action "exec("killall iTunes")" and that will kill iTunes (this is on my mac.) Except that sends it like a regular link, and reloads the page.

so could anyone clue me in on the javascript I would need to write to execute some PHP code.
 
well from what I understand, you can't send commands to the console with javascript. which is why, after a bit of reading, I believe ajax (javascript + PHP) is what I need.

I just have no idea how to implement it.

for example, using php, I can click a link that performs the action "exec("killall iTunes")" and that will kill iTunes (this is on my mac.) Except that sends it like a regular link, and reloads the page.

so could anyone clue me in on the javascript I would need to write to execute some PHP code.

Did you read my above post, please LMK if thats not fitting so I'll know exactly what it is you are trying to do. AJAX is way overhead for such a simplistic application. I use remote script calling strings everyday that backup databases to multiple places.
 
Instead of making calls via the URL string, why not make each link a seperate page alltogether ..... the 'index' page would show each loadable page as a link and would not be reloaded when a link is clicked.

I don't see why ajax is needed, it is a very simple process from what I gathered.

I still don't understand why using one page is a problem. Have the page do nothing until it's given a command. While using if/else statements to see if the URL variables are set (if you are avoiding throwable warnings from PHP - depending on how PHP is setup), have it execute the command if the $_GET variable is set.

Did you read my above post, please LMK if thats not fitting so I'll know exactly what it is you are trying to do. AJAX is way overhead for such a simplistic application. I use remote script calling strings everyday that backup databases to multiple places.
no, I missed your post. sorry.

Instead of making calls via the URL string, why not make each link a seperate page alltogether ..... the 'index' page would show each loadable page as a link and would not be reloaded when a link is clicked.
I'm not sure what you mean by this?

I really am a PHP/ javascript noob. I only know HTML and CSS :( so you're gonna hafta help me out a bit.

"I still don't understand why using one page is a problem."
using one page isn't a problem. I'm not sure why your suggesting that it is?
 
no, I missed your post. sorry.


I'm not sure what you mean by this?

I really am a PHP/ javascript noob. I only know HTML and CSS :( so you're gonna hafta help me out a bit.

"I still don't understand why using one page is a problem."
using one page isn't a problem. I'm not sure why your suggesting that it is?


I think your confused ..... maybe because I confused you :D

Here is why I thought you did not want a single page, your quote:
"....and just basically reloads that same page. Is it possible to easily send the command, but NOT load that page again..."


Using multiple pages:
What I meant by index page was making an index.html page. On that index.html page, name links according to what page they need to go to in order to run your specific script. That would keep you from being on the same page and alleviate using query strings (stuff like www.mysite.com?command=my_command)


Using 1 page:
What I meant by using if/else statements was going more with your original idea, but I was confused at why your post wasn't efficient enough to handle your needs. For example, if I were making a page that had a number of links to do different stuff depending on which link I clicked .... I wouldn't want a seperate page for every link. If you start getting a lot of commands you'll have to create a page for each one. Instead, you should make ONE page and use if/else statements to check the URL for what it is supposed to do. It will reload the page (essentially), but not try to do all of the other commands at once.

Here is a very basic example of what Im talking about (youd replace the empty exec() with your command obviously):
Code:
<b>Commands:</b>
<a href=page.php?cmd=open_prompt>Open command prompt</a> | 
<a href=page.php?cmd=shutdown_pc>Shutdown PC</a> | 
<a href=page.php?cmd=open_ie>Open IE</a>


<?php
if( isset($_GET["cmd"]) ) {

	$cmd = $_GET["cmd"];
	
	if ($cmd == "open_prompt") {
		exec(); 
	} else if ($cmd == "shutdown_pc") {
		exec(); 
	} else if ($cmd == "open_ie") {
		exec();
	}
	
} else {
	//do nothing, no command was called
}
?>
 
I think your confused ..... maybe because I confused you :D
Using 1 page:
What I meant by using if/else statements was going more with your original idea, but I was confused at why your post wasn't efficient enough to handle your needs. For example, if I were making a page that had a number of links to do different stuff depending on which link I clicked .... I wouldn't want a seperate page for every link. If you start getting a lot of commands you'll have to create a page for each one. Instead, you should make ONE page and use if/else statements to check the URL for what it is supposed to do. It will reload the page (essentially), but not try to do all of the other commands at once.

Here is a very basic example of what Im talking about (youd replace the empty exec() with your command obviously):
Code:
<b>Commands:</b>
<a href=page.php?cmd=open_prompt>Open command prompt</a> | 
<a href=page.php?cmd=shutdown_pc>Shutdown PC</a> | 
<a href=page.php?cmd=open_ie>Open IE</a>


<?php
if( isset($_GET["cmd"]) ) {

	$cmd = $_GET["cmd"];
	
	if ($cmd == "open_prompt") {
		exec(); 
	} else if ($cmd == "shutdown_pc") {
		exec(); 
	} else if ($cmd == "open_ie") {
		exec();
	}
	
} else {
	//do nothing, no command was called
}
?>

that is exactly what I'm doing now. but here is my problem:
I'm using this page on my blackberry, which only has EDGE data. the latency sucks, and it takes 1-2 seconds just to call for the page to load it up again. I would like it to send the same commands like you listed above, without refreshing the page, and it seems AJAX is exactly what I need. (yes blackberrys support javascript)
 
DIdn't see the blackberry part .... I could shoot myself in the foot for it too. How do other javascript tactics work? Does it only delay when loading a page? How do dropdown menus that utilize javascript like tree designs with +/- work?
 
a drop down wouldn't work, as the links are all images (its an iTunes controller)

in a nutshell what I need is, some javascript, that when run (with the "onclick" command) runs some PHP code. I'm trying to figure it out using AJAX...but i have no clue what I'm doing...(yet)
 
Maybe I can be of some help. To use AJAX with PHP, you'll need to create 2 PHP pages: 1 is your main page (the one that you access with your blackberry, I'll call it Main.php), and the other is your command page (the one that runs your commands, I'll call it Command.php). You will also want to have a javascript file that you will use to handle the ajax calls.

On the Main.php page, you'll have all your links to whatever commands you have. Each link will have a javascript function call in it's onClick event, which will pass the command to the Command.php page. You will also need (technically you don't, but in my example I use it) a division somewhere on the page, just to write responses to for your own visual affirmation that the requests actually went through.

Code:
<a href='#' onClick='javascript:send('update')>Update Something</a><div id='msg'></div>

The command.php page will be looking at the querystring for the commands.
Code:
<?php
$cmd = $_REQUEST['c'];
$div = $_REQUEST['d'];

// Validate the requests before directly running anything!

// Command list
switch($cmd){
	case 'update':
		// Update some stuff
	break;
	case 'restart':
		// Restart
	break;
	case 'etc':
		// You get the idea
	break;
	default:
		// Most likely toss an error
	break;
}
?>

The way I usually have my ajax setup is to write a value on the calling page, like OK or FAIL or whatever you want. All you do is put an $echo "message here" into the case statements and it will appear on your main.page.

Here is the AJAX js file you'll want to use. (I modified one of mine for your methods)

Code:
//Global Variables
var xmlHttp;	// xmlHttp object.
var div; //Div to write to

// Function used in Main.php. Used to pass data to Command.php
function send(cmd,d){
	var str="";
	if (d.length>0){
		if(div=document.getElementById(d)){}
		else{
			alert("Invalid DOM Element in function send().");
			return;
		}
	}
	else{
			alert("Invalid parameter in function send().");
			return;
	}
	
	if (cmd.length<0){
		div.innerHTML="<font face='Arial, Helvetica, sans-serif' size='3' color='#FF0000'>Invalid command.</font>";
		return;
	}
	
	str="c=" + cmd + "&d=" + div.id;
	runCmd(str);
}

// Function that actually passes the data to the Command.php page.
function runCmd(str){
	var sURL="./Command.php";
	document.getElementById(div.id).innerHTML="";
	if (str!=0){
		if (str.length==0){ 
			document.getElementById(div.id).innerHTML="";
			return;
		}
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null){
			alert ("Browser does not support HTTP Request");
			return;
		} 
		xmlHttp.abort();
	
		sURL+="?"+str;
		sURL+="&sid="+Math.random();
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",sURL,true);
		xmlHttp.send(null);
	}
} 

//*********************************************************************************************
//	Function:			GetXmlHttpObject()
//	Inputs:				none
//	Description:	Creates a new XMLHttpRequest object (non-IE), or a new XMLHTTP object (IE).
//	Returns:			objXMLHttp;
//*********************************************************************************************

function GetXmlHttpObject(){ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

//*********************************************************************************************
//	Function:			stateChanged()
//	Inputs:				none
//	Description:	Displays the "load" division (shown as loading image), waits for the correct
//								xmlHttp.readyState, and then prints the response to the "info" division.
//	Returns:			none;
//*********************************************************************************************

function stateChanged(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			document.getElementById(div.id).innerHTML=xmlHttp.responseText;
	}
}


That should get you started. The hardest part is comprehending how the XMLHttpRequest object works, but you don't really need to understand it in this example since it's already done for you.
 
that looks like what I need, but I can't get it to work. I don't even see the error message I put under default on the commands page when I send it a bum command

Am i correct in assuming that I have to insert the .js file (I named it ajax.js) in the head of the main.php file using this line:
Code:
<script language="javascript" src="ajax.js"></script>
 
Code:
<a href='#' onClick='javascript:send('play')'>Play</a><br />
<a href='#' onClick='javascript:send('pause')'>Pause</a><br />
<a href='#' onClick='javascript:send('junk')'>junk</a>

and the corresponding commands ARE listed in the command.php file.
Code:
switch($cmd){
	case 'pause':
		exec("osascript -e 'tell app \"iTunes\" to pause'");
		echo "Paused";
	break;
	case 'play':
		exec("osascript -e 'tell app \"iTunes\" to play'");
		echo "playing";
	break;
	default:
		echo "error";
	break;
if I go directly to command.php in the browser and manually tack on "?c=pause" or another command, it works fine. this makes me think it's some code in the js file, which i am (barely) starting to understand, but can't figure it out.

note: even when I click on the "junk" link, it does not echo the word 'error' like it should.
 
Code:
<a href='#' onClick='javascript:send('play')'>Play</a><br />
<a href='#' onClick='javascript:send('pause')'>Pause</a><br />
<a href='#' onClick='javascript:send('junk')'>junk</a>

The links need an extra param, (the <div> that you are writing the message to from the command.php page).

Add the <div> somewhere on the page like:
Code:
<div id='message'></div>

and update the links to say
Code:
<a href='#' onClick="javascript:send('pause','message');">Pause</a>

ahh, also make sure your onClick is in double quotes, as the javascript will die (currently it will only read: "javascript:send(" instead of the whole thing.)
 
Make an iframe, like so:

Code:
<iframe id="cmd_frame" frameborder="0" width="0" height="0">Must support inline frames.</iframe>

Create your links like this:

Code:
<a href="send_crap.php?variable=1&x=true" target="cmd_frame">send information</a>
 
Make an iframe, like so:

Code:
<iframe id="cmd_frame" frameborder="0" width="0" height="0">Must support inline frames.</iframe>

Create your links like this:

Code:
<a href="send_crap.php?variable=1&x=true" target="cmd_frame">send information</a>

that was my original idea, and certainly easy :p. Unfortunatly though, blackberry's don't support frames :(
 
I'm not even sure if blackberries support the xmlHTTPRequest. I just tried it on my PSP and it said it didn't support it.
 
I was trying it on my Mac in safari and it wasn't working. ill try making the changes you suggested above when I get home tonight

:edit:
well I'm an idiot....
tried the suggestion you gave me earlier.

it gave me this alert:
"Invalid DOM Element in function send()."

and didn't perform the right command.

turns out when you suggested this code:
Code:
<a href='#' onClick="javascript:send('pause','message');">Pause</a>
<div id='message'></div>
I didn't get it and was changing "message" in the link to "Paused"..

working now! thanks a lot!
unfortunately, you were correct in assuming the blackberry browser does not support http requests :(
fortunately opera mini does :)
 
Glad to hear it's working for you! Hopefully one day the mobile browsers will support xmlHttpRequest.

Cheers!
 
Glad to hear it's working for you! Hopefully one day the mobile browsers will support xmlHttpRequest.

Cheers!

haha, well like I said. opera mini does, so I'll just use that instead. I like it better anyways, and was only using the blackberry browser because it rendered page slightly faster. but thats moot point now.
 
Back
Top