Wrox Programmer Forums
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 26th, 2003, 11:00 AM
Registered User
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to G-Zilla
Default PHP functions

Hey I have a php function that I would like to execute if the user clicks on a link.

I tried something like
echo "<a href='DELETE($query)'>Click here to delete record</a>";

But that tried finding a page called DELETE($query).

Anyway I'm wonder if it is possible to call php functions like this, I think you can with Java script so if someone has a round about way of using an <a> tag to call a javascript which I then can call a php script from that'd be ok too, I just don't know ANY javascript.

Thanx if you can help ;) you guys wrox.

God that was horrible.

Will appear after each of your posts.
 
Old August 26th, 2003, 11:12 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

What you are trying to do can be acheived via the $_GET method.

Since PHP is a server-side language, this isn't really possible to do this with JavaScript. Well JavaScript can trigger an event-handler to open a window and send the HTTP request and then close the window. But that's not usually a very practical way of doing things.

Here is an example of how you would complete your record deletion using PHP and the $_GET method.

Code:
<?php

if (isset($_GET["delete"] && $_GET["delete"] == true) {

    # If $_GET["delete"] exists and contains a true value
    # execute query statement here

}

echo "<a href=\"{$_SERVER["PHP_SELF"]}?delete=1\">Click here to delete the record</a>";
?>
The $_GET method essentially takes the values declared after the question mark and reads those into the variable namespace in name value pairs under the $_GET[""] super-global array.

$_SERVER["PHP_SELF"] will contain the name of the current script running and will cause the same page to reload.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old August 26th, 2003, 11:15 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Awe hell, I noticed a little typo in there... missing the right parenthesis on the isset() function...

if (isset($_GET["delete"]) && $_GET["delete"] == true) {


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old August 26th, 2003, 09:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

For larger applications, I generally extend the idea of having a debug flag set. I don't like using global variables for more than is absolutely necessary; I'd rather use a function.

The use of a function lets you control much more specifically how to handle debug output. Perhaps a certain set of users are allowed to view the debugging output of a page or set of pages. Perhaps you should generate code to open a new window and display your debug output there, so your original generated page doesn't change appearance when debug mode is on. The list goes on.

The translation between global variable and function is not direct. That is,

if ($GLOBALS['do_debug'])
{
   // yada yada yada
}

does not simply become

if (do_debug())
{
   // yada yada yada
}

Rather, you'd write a function that recieved the output to be displayed.

debug("yada yada yada");

This function will handle checking whether or not debugging is enabled and do the appropriate thing with it.

My .02!

Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
begin php & mysql - chapter 12, user_form.php jon_stubber Beginning PHP 1 March 9th, 2006 10:57 AM
Calling PHP functions from ASP Smoobly Classic ASP Basics 4 January 10th, 2005 06:11 PM
Error: movie.php & commit.php on p182-186, ch6 willburke BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 October 12th, 2004 02:48 PM
Chapter 15 - functions.php question buzzuh BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 September 7th, 2004 08:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.