dslfq06464
2018-05-25 11:53
采纳率: 0%
浏览 101
已采纳

PHP表单数据检查提交按钮名称

I have two forms on the same page, each with different fields and different submit button names.

<button type="submit" name="formOne">Submit form</button>
<button type="submit" name="formTwo">Submit form</button>

On my mailer.php page I would like to check which submit button has been clicked, and process the code accordingly.

The code I have so far works for a single form, as follows;

if ($_SERVER["REQUEST_METHOD"] == "POST" ) {
    // my form 1 data
    // do stuff
}

However I thought I coulld add an if/else to check which submit button was clicked, something like;

if ($_SERVER["REQUEST_METHOD"] == "POST" ) {
    if($_POST['formOne']){
        // form 1 data
        // my form 1 data
        // do stuff
    }
    if($_POST['formTwo']){
        // form 2 data
        // my form 2 data
        // do stuff
    }
}

This doesn't seem to work. The error I receive is;

Notice: Undefined index: formOnein ... line 7

How can I achieve this?

图片转代码服务由CSDN问答提供 功能建议

我在同一页面上有两个表单,每个表单都有不同的字段和不同的提交按钮名称。 \ n

 &lt; button type =“submit”name =“formOne”&gt;提交表单&lt; / button&gt; 
&lt; button type =“submit”name =“formTwo”&gt;提交表单&lt; / 按钮&gt; 
   
 
 

在我的 mailer.php 页面上,我想检查单击了哪个提交按钮,并相应地处理代码。

我到目前为止的代码适用于单个表单,如下所示;

  if($ _SERVER [“REQUEST_METHOD”] =  =“POST”){
 //我的表格1数据
 //做东西
} 
   
 
 

但是我想我可以添加 if / else 检查点击了哪个提交按钮,如:

  if($ _SERVER [“REQUEST_METHOD”] ==“POST”){
  if($ _ POST ['formOne']){
 //形成1个数据
 //我的表格1数据
 //做东西
} 
 if($ _ POST ['formTwo']){
  //表单2数据
 // 我的表单2数据
 //做东西
} 
} 
   
 
 

这似乎不起作用。 我收到的错误是;

注意:未定义索引:formOnein ...第7行

我怎样才能做到这一点?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douzhang6646 2018-05-25 12:07
    已采纳

    If you want, you can alternatively use hidden inputs. You can access hidden inputs from php just like a normal text input.

    <form method="GET" action="https://postman-echo.com/get">
        <input type="hidden" name="form" value="one" />
        <button type="submit">Submit form</button>
    </form>

    </div>
    
    打赏 评论
  • drllqg2903 2018-05-25 11:55

    You just forgot to check if the var isset().

    if ($_SERVER["REQUEST_METHOD"] == "POST" ) {
        if(isset($_POST['formOne'])){
            // form 1 data
            // my form 1 data
            // do stuff
        }
        if(isset($_POST['formTwo'])){
            // form 2 data
            // my form 2 data
            // do stuff
        }
    }
    
    打赏 评论
  • dpvp56187 2018-05-25 12:30

    Instead of using

    <button type="submit" name="formOne">Submit form</button> <button type="submit" name="formTwo">Submit form</button>

    use

    <input type="submit" name="formOne" value="Submit Form"/>
    <input type="submit" name="formTwo" value="Submit Form"/>
    

    php code:-

     <? if(isset($_POST['formOne'])){echo "form One";} elseif(isset($_POST['formTwo'])){echo "form Two"; }?>
    
    打赏 评论

相关推荐 更多相似问题