Log in

View Full Version : Perform Function when...



Rockonmetal
09-12-2007, 10:51 PM
I was wondering if PHP can perform a function when a link, button, or something is pressed...
Heres what I want todo...
Heres the HTML page code *its still in development stages but who really cares...*


<html>
<head>
<!-- Style Stuff -->
</head>
<body>
<table>
<tr>
<td>Menu:
<ul class="menu">
<li><a href="Tutorial/1/Introduction/" target="phpincludediv">Intro to PHP</li>
<li><a href="Tutorial/2/Introduction/" target="phpincludediv">What you need to run PHP</li>
...
</ul></td>
<td>Tutorial Below:
<?php
//Stuff here that says:
//If link going to or named "url/blank" then do this...
include("Tutorial/1/Introduction/index.php");
//Else
include("News/new.php");
?>
</td>
</tr>
</table>
</body>
</html>

Twey
09-12-2007, 11:08 PM
No. PHP scripts are run at request time: they can only respond to request data. If you want to pass data to a PHP script, you have to either submit a form to the PHP script in question, in which case the form data is passed along with the request and available to the script in the $_GET or $_POST arrays (or the $_REQUEST array, which is a combination of $_GET, $_POST and $_COOKIE) or use Javascript to process the event and display the result, possibly using the XMLHttpRequest object to retrieve the data from the PHP script. Note, however, that this usually involves some fairly involved Javascript, and requires careful and elegant design to be accessible.

Rockonmetal
09-12-2007, 11:29 PM
OK... thanks man...
I can't do it, cuz my client won't accept any javascript *he hates it...* same here...

Twey
09-13-2007, 12:16 AM
Javascript is actually a fairly decent language. What it is is very flexible, and thus open to abuse. Most of the time, bad Javascript code stems from bad design patterns (like writing a Javascript function without a pure HTML/server-side backup), rather than actual language flaws. Used properly, it can dramatically enhance a site, without losing any accessibility.

Either way, the form submission route is still open to you.

Rockonmetal
09-13-2007, 12:21 AM
k... but my client still won't accept any javascript...

Twey
09-13-2007, 12:42 AM
Javascript was only one option of two that I gave you. I was discussing Javascript purely as an aside, and on that note alone you should talk to your client about this preference. Practically, in terms of your project, the form submission method will be fine and Javascript is entirely unnecessary, although it could be used to enhance the interface.

boogyman
09-13-2007, 01:18 PM
(or the $_REQUEST array, which is a combination of $_GET, $_POST and $_COOKIE)
it also contains variables of $_SERVER

djr33
09-13-2007, 01:43 PM
SERVER is not included. (Nor is SESSION.)


Note: Prior to PHP 4.3.0, $_FILES information was also included in $_REQUEST.

An associative array consisting of the contents of $_GET, $_POST, and $_COOKIE.

boogyman
09-13-2007, 01:51 PM
oops, okay its not. learn something new every day. thanks mate

djr33
09-13-2007, 02:19 PM
Indeed. When I read your post, I was unsure, so I wanted to check it. I was also thinking that the SESSION array was included.

boogyman
09-13-2007, 02:36 PM
I was also thinking that the SESSION array was included.
thats what I meant to say... but my fingers typed SERVER instead... haha I didnt even catch that until you posted.

djr33
09-13-2007, 04:06 PM
Ha. Ok. But SERVER makes some sense too. the IP, REQUEST_URI, HTTP_REFERER, brower info, etc., are input in some sense.

I like to stay away from $_REQUEST, anyway. Too much guesswork involved. If I do need to use both post and get (and/or cookie), then I just write that directly, explicitly, so it's easy to keep track of.