tags:

views:

57

answers:

6

I was wondering if my code below is even correct, I've been having numerous errors with this, but am not sure if the problem really exists here. The code is below:

The user will click 'Exit Group'.

<p class="logout"><a id="exit" name="logout" href="#">Exit Group</a></p>

The code that should be execute when 'Exit Group' is clicked is below:

if(isset($_GET['logout'])){ 

    //CODE TO BE EXECUTED        
                            } 

However, the code I am trying to execute when the user clicks 'Exit Group' is not even being executed. There is nothing wrong with the code within the braces, as numerous people have checked it. But I was wondering if my problem may lie in the code above? Thank you.

+2  A: 

You're using a regular hyperlink, no form will get posted. you need a submit button of some kind in a form with method="post" to do that. regular links just result in GET requests and nothing will ever be posted that way.

edit: added simple example:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
    <head>
        <title>Form test</title>
    </head>
    <body>
<?if ($_SERVER['REQUEST_METHOD'] == 'POST'):?>
        <pre><? print_r($_POST)?></pre>
<?endif;?>
        <? // $_SERVER['REQUEST_URI'] holds the current URL, so we know that ?>
        <? // we'll end up back in this file when the form is submitted.     ?>
        <form method="post" action="<?= $_SERVER['REQUEST_URI']; ?>">
            <input type="text" name="textbox" 
                value="<?= isset($_POST['textbox'])?$_POST['textbox']:'Type something' ?>" />
            <input type="submit" name="submitbutton" value="Submit" />
        </form>
    </body>
</html>
Kris
+3  A: 

a isnt a form control. it needs to be an input or select if it's within a form.

For manual linking, do href="/page?logout"

meder
+1  A: 

$_POST will only be filled if you use a form with method=post.

Femaref
A: 

Yes. A POST and a GET are two different things ;)

if(isset($_GET['logout']))
hollsk
+3  A: 

If you click the link, nothing happens because the URL only contains the fragment identifier #. Not even a GET request will be issued.

You use this kind of link normally to jump to an element inside the page (e.g. <a href="#top">Top</a> to jump to an element with ID top). This is completely handled in the browser.

And if you only put the fragment identifier there, just nothing will happen. This is very often used if the link should execute some JavaScript and should actually not link to something else.


You are testing the $_POST array at the server side. But this array only contains elements, if you initiate a POST request by a form. That means you need to create a form with a submit button, e.g.:

<form action="" method="POST">
    <input type="submit" name="logout" value="Exit Group" />
</form>

Here comes the name attribute into play, which will be the key in the $_POST array. But assigning this on a normal link will have no effect.


You could do it also with the link, but with a GET request this way:

<a id="exit" href="?logout=1">Exit Group</a>
<!--                 ^-- parameter must be part of the URL, name has no effect    -->

and

if(isset($_GET['logout'])){    
    //CODE TO BE EXECUTED        
} 

Note that you have to pass a parameter logout it here.


It seems you have mixed up GET and POST requests. If you have a form, the name s of the form elements will be transmitted as parameters to the server. That means given this form:

<form method="POST">
    <input type="text" name="foo" value="" />
    <input type="text" name="bar" value="" />
    <input type="submit" name="send" value="Send" />
</form>

if the user clicks on the submit button, the $_POST array at the server side will have the keys:

$_POST['foo']
$_POST['bar']
$_POST['send']

This does not work with links though. A click on a link will create a normal GET request, and here, the parameters must be part of the URL, appended after a question mark ? and separated by an ampersand &:

<a href="somePage.php?foo=1&bar=1&andMore=0"> Link </a>

will result in

$_GET['foo']
$_GET['bar']
$_GET['andMore']

You probably should read about the HTTP protocol.

Felix Kling
SORRY everyone, but I should have mentioned that IT DOES NOT WORK with $_GET either. I tried it before, thus thought $_POST would work, but BOTH OF THEM don't work.
Newbie_25
@newbie-25: Read the first paragraph of my answer. The URL you use is wrong.
Felix Kling
Are you actually passing any URL parameters? Your example doesn't have any, is it a faithful representation of what you're doing?
hollsk
A: 

This <a id="exit" name="logout" href="#"> should be <a id="exit" href="?logoff=true#">.

Then logoff will be in the $_GET array.

Michael Speer