简体   繁体   English

php,在同一页面提交2个按钮

[英]php, submit 2 buttons in same page

I would to submit 2 buttons in same php page, every button show deferent message on label, 我想在同一个php页面中提交2个按钮,每个按钮在标签上显示不同的消息,

 <!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 我在表单中使用了PHP_SELF,我希望在点击任何按钮后直接显示结果而不更改页面

Thanks 谢谢

  1. You can't do anything with PHP without reloading the page, because each time it needs to be interprrted by server. 在没有重新加载页面的情况下,您无法对PHP执行任何操作,因为每次需要由服务器进行交互操作。
  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. 在2个提交按钮的情况下,它们都只提交表单,与按钮名称无关,在服务器端,您无法知道按下了什么按钮。

Either do some tricky js stuff, like changing some hidden input value or form action etc. 要么做一些棘手的js,比如更改一些隐藏的输入值或表单动作等。

Or just keep it simple and use radio boxes 或者只是保持简单并使用无线电盒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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