Page 1 of 1

acheive the same as a form would, without the form

Posted: Wed Dec 29, 2004 8:06 pm
by sebnewyork
Hi all

I have been using PHP in combination with forms to do all sorts of things, and now I am in a situation where I wonder why is the form necessary at all.
Could I acheive the same with simple <a href="*"> links?
For example I made a simple content management interface where all editable pages are listed, and I placed a radio button next to each page name so that the user can choose the page by selecting the radio button, and then click on a submit button to go to the editor for the page...
Could I do just the same but by just clicking on the page name, which would be a link with a PHP action attached?

In other words, could I use any html links and attach to them PHP actions just as if these links were forms elements?
Like:
<a href="onClick='<?php do this; ?>'">myLink</a>

Thanks for any suggestions

Posted: Wed Dec 29, 2004 8:28 pm
by feyd
the php action is fully rendered before it's sent to the user.. so unless you entity the code, it'll either generate a warning, error, or final code/text...

If you want a link to cause an action to run, you need to call a script with the proper information to do whatever you want..

Posted: Wed Dec 29, 2004 8:34 pm
by sebnewyork
I guess that was my question:
How do I attach a php script to an html link.
I know how to attach a javascript to a link:

<a href="javascript:jsfuncion('blabla');">myLink</a>

How would I do that with a php script?

Posted: Wed Dec 29, 2004 8:39 pm
by feyd

Code: Select all

&lt;a href="foo.php?name=value"&gt;foo&lt;/a&gt;

Code: Select all

echo $_GET['name'];

Posted: Wed Dec 29, 2004 9:07 pm
by sebnewyork
thanks.
I'm not sure I understand how this would work with a concrete example. I need one more push, please.
How would I do if I want some php function to run if the link is clicked on?

(sorry I don't understand code if code elements are default names, could you give me any example where
"name" would be something concrete, "value" would be something concrete, etc?... My brain can't grasp abstraction)

say, if the link is clicked, pass a session variable called $bla, and go to a page called "newPage.htm".

Sorry for my lack of intuition

Posted: Wed Dec 29, 2004 9:13 pm
by feyd
typically, you don't get session variables to pass, as they are already available to the called script... so a basic example...

Code: Select all

&lt;a href="foo.php?concrete=toys"&gt;foo&lt;/a&gt;

Code: Select all

if( isset($_GET&#1111;'concrete']) )
{
  switch($_GET&#1111;'concrete'])
  {
    case 'toys':
      header('Location: newPage.htm');
      break;

    default:
      # something else...
  }
}

Posted: Wed Dec 29, 2004 9:22 pm
by sebnewyork
Thanks a lot! now it makes perfect sense!
Great!