php 多表格和一个处理页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10459012/
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-24 22:13:30  来源:igfitidea点击:

Multiple forms and one processing page

phpmysqlforms

提问by Ami

please i need your help with an issue. I have two forms on my homepage that i'll like users to fill and submit at different times. My problem is that i'll like to have only one processing page for both of them. Normally i can do this in seperate pages. But i'll like to know if doing it on the same page is possible.

请我需要你的帮助解决一个问题。我的主页上有两个表单,我希望用户在不同的时间填写和提交。我的问题是我只想为他们两个都只有一个处理页面。通常我可以在单独的页面中执行此操作。但我想知道是否可以在同一页面上进行。

Okay.. If i submit form A, on the action page, wont there be Undefined Index for variable of form B, that has not being submitted, and ofcourse using a GET is not adviced.

好的.. 如果我提交表单 A,在操作页面上,表单 B 的变量不会有未定义的索引,尚未提交,当然不建议使用 GET。

Thanks for your time and patience.

感谢您的时间和耐心。

回答by Ami

It's not completely unheard of to do this. Quite often, a different parameter is passed in the form element's action attribute like /submit.php?action=registeror /submit.php?action=activate.

这样做并非完全闻所未闻。通常,在表单元素的 action 属性中传递不同的参数,例如/submit.php?action=registeror /submit.php?action=activate

So, you have code like this:

所以,你有这样的代码:

if ($_GET['action'] == 'register') {
  // Register user
} else if($_GET['action'] == 'activate' {
  // Activate user
}

However, you could also just change the value of the submit button and have the action attribute the same for both forms:

但是,您也可以只更改提交按钮的值,并使两种表单的 action 属性相同:

if (isset($_POST['submit'])) {
  if ($_POST['submit'] == 'register') {
    // Register user
  } else if($_POST['submit'] == 'activate') {
    // Activate user
  }
}

回答by aifarfa

create separate form_process script then include in form pages.

创建单独的 form_process 脚本,然后包含在表单页面中。

if(!empty($_POST)){
 include 'form_process.php';
}

form_process.php should contain only class/function without echo or print out.

form_process.php 应该只包含没有回显或打印输出的类/函数。

alternately you may set action url to the same page then redirect back to proper page.

或者,您可以将操作 url 设置为同一页面,然后重定向回正确的页面。

<form id="add-profile-form" action="form_controller.php" method="post">
    <input type="hidden" name="act" value="adding"/>
    <!-- form 1. -->
</form> 

<form id="edit-profile-form" action="form_controller.php">
    <input type="hidden" name="act" value="editing"/>
    <!-- form 2. -->
</form>

form_controller.php

form_controller.php

if(isset($_POST['act']){
    if($_POST['act'] == 'adding'){
        //process form1
    }else if($_POST['act'] == 'editing'){
        //process form2
    }

    header("Location: success.php");
}

回答by Shaikh Farooque

You can do it on the same page also. Just you need to make actionsame for both the forms.

您也可以在同一页面上执行此操作。只是你需要action为这两种形式做同样的事情。

You need to make some condition and write the individual functionality for Form Aand for Form Bdepending on the source form.

您需要创建一些条件并根据源表单为Form A和为编写单独的功能Form B

You can check with the parameters in action like @Ami has used.

您可以像@Ami 使用的那样检查正在运行的参数。

/submit.php?action=registeror /submit.php?action=activate

/submit.php?action=register或者 /submit.php?action=activate

So, you have code like this:

所以,你有这样的代码:

if ($_GET['action'] == 'register') {
  // Register user
} else if($_GET['action'] == 'activate' {
  // Activate user
}

However, you could also just change the value of the submit button and have the action parameter the same for both forms:

但是,您也可以只更改提交按钮的值,并使两种表单的操作参数相同:

if (isset($_POST['submit'])) {
  if ($_POST['submit'] == 'register') {
    // Register user
  } else if($_POST['submit'] == 'activate') {
    // Activate user
  }
}