php 从同一页面上的php函数调用表单提交操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6060028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:15:09  来源:igfitidea点击:

Call form submit action from php function on same page

phphtmlformswebforms

提问by starsinmypockets

I'm working on a simple web application. In order to reduce the number of files, I want to put (php) code for a form submit function into the same page as the form. Something like this:

我正在开发一个简单的 Web 应用程序。为了减少文件的数量,我想把一个表单提交功能的(php)代码放到与表单相同的页面中。像这样的东西:

<body>
   <form id = "rsvp-status-form" action = "rsvpsubmit" method = "post">
     <input type="radio" name="rsvp-radio" value="yes"/> Yes<br/>
     <input type="radio" name="ravp-radio" value="no" checked/> No<br/>
     <input type="radio" name="rsvp-radio" value="notsure"/> Not Sure<br/>
     <input type="submit" value="submit"/>
   </form>
 </body>

<?php 
  function rsvpsubmit() {
    // do stuff here
  }

What is the proper way to call the submit function?

调用提交函数的正确方法是什么?

回答by Quentin

After you fix your radio group so they all have the same name:

修复无线电组后,它们都具有相同的名称:

if (isset($_POST['rsvp-radio'])) {
    rsvpsubmit();
}

回答by line-o

<?php
   if (isset($_POST['rsvpsubmit'])) {
     //do something
     rsvpsubmit();
   }
   else {
    //show form
?>
<body>
   <form id="rsvp-status-form" action="?rsvpsubmit" method="post">
      <input type="radio" name="rsvp-radio" value="yes"/> Yes<br/>
      <input type="radio" name="rsvp-radio" value="no" checked/> No<br/>
      <input type="radio" name="rsvp-radio" value="notsure"/> Not Sure<br/>
      <input type="submit" value="submit"/>
   </form>
 </body>
<?php 
  }

  function rsvpsubmit() {
    // do stuff here
  }
?>