Aaron Warnke
Aaron Warnke

Reputation: 213

How to post back to same page without a form using PHP?

Hi I would like to know how to first post to a current page using the following code:

<?php  
    if(isset($_GET['button']) === true){
        echo 'Albums';
    }   
?>
<form action="" method="post">
    <input type="button" name="button" value="Albums">
</form>

I can't seem to get the button when clicked to echo out Albums on current page?? But my main question was how can I achieve what I am trying to do without using a form just a link or button that can return an action using PHP? Is this possible with PHP alone? If not then how can I use whatever scripting is needed to return the PHP function?

Upvotes: 0

Views: 3337

Answers (2)

Alex
Alex

Reputation: 12433

You submit the data as post, so you have to check for POST.

<?php  
    if(isset($_POST['button'])){
        echo 'Albums';
    }   
?>
<form action="" method="post">
    <input type="submit" name="button" value="Albums" />
</form>

Additionally I would either use <input type="submit" or <button type="submit" name="button">Albums</button>.

Upvotes: 0

Brad
Brad

Reputation: 163272

If you don't specify the action attribute, the form will post back to your current script.

If you want that $_GET variable to have any of your form fields, set your form's method to GET, or don't set it at all. (It defauls to GET).

Upvotes: 1

Related Questions