Hannah Hannah - 3 years ago 79
PHP Question

PHP $_GET['action']

I am trying to teach myself PHP and have been doing so for a couple weeks now. I'm currently trying to wrap my head around GET and POST methods, particularly GET. This is just some basic code I wrote:

<form action="" method="GET">
<input type="submit" name="action" value="Laugh">
<input type="submit" name="action" value="Cry">

$action = isset($_GET['action']) ? $_GET['action'] : NULL;

switch ($action) {
case 'Laugh':
echo 'You laughed!';
case 'Cry':
echo 'You cried!';

Upon clicking the Submit buttons, the above code will display the case result respectively. This works for submit buttons. How would I obtain the same result if I were to use links instead of submit buttons? I'd like to be able to click on a link and have it take me to one of the switch cases. I have tried searching online, and I was unable to get any information on this.

Thank you for your help!

Answer Source

Simply use an anchor:

<a href="?action=Laugh">Please Laugh</a><br>
<a href="?action=Cry">Please Cry</a>

Your PHP code can be unchanged.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download