简体   繁体   中英

php, submit 2 buttons in same page

I would to submit 2 buttons in same php page, every button show deferent message on label,

 <!DOCTYPE html>

 <html>

 <meta charset="UTF-8"> 

 <title>Test Submit 2 buttons</title>

 <body>

<?php
    $add_result = "";

    if (isset($_POST['addnews']))
    {
        $add_result = "Hello, add new news";
    }

    if (isset($_POST['deletenews']))
    {
        $add_result = "Hello, delete all news";
    }

?>

<br /> <br /> <br /> <br /> <br /> <br/>

   <form accept-charset="utf-8" method="POST" dir="rtl" style="text-align:center" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <label>Enter the news before click on any button::</label>
    <br /> <br />
    <input type="text" name="news_text" size="100" id="partNumber" required/> 
    <br /> <br /> <br /> <br />
    <input type="submit" name="addnews" id="round" value="Add" /> 
    <br /> <br/> <br />
    <input type="submit" name="deletenews" id="round" value="Remove" /> 
    <label for="message">
        <?php echo $add_result; ?>
    </label>
</form>

<style type="text/css">
 input#round{
       ....
     }
</style>

I used PHP_SELF in the form, I want the result show directly after click on any button without change the page

Thanks

  1. You can't do anything with PHP without reloading the page, because each time it needs to be interprrted by server.
  2. In case of 2 submit buttons both of them just submit the form, irrelevant what is the button name, and on the server-side you can't know, what button was pushed.

Either do some tricky js stuff, like changing some hidden input value or form action etc.

Or just keep it simple and use radio boxes

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