简体   繁体   中英

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?

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 ).

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> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM