同一页面php上有两个表单


2 forms on same page php

我的学习本给了我一个作业,要求我在一页上有两个表格,并将它们输出到同一页。这可能吗?两种形式都可以独立工作。两者都有作用:

<?php echo $_SERVER['PHP_SELF']; ?>". 

两者都有一个提交按钮,但该按钮只提交它所属的表单。不过,这可能有道理。

问题是,我需要页面在点击2个提交按钮中的一个时发布两个表单输出,或者随后按下它们,但第一个表单的信息需要留在页面上。

这是可能的,还是我在尝试去做不可能的事情?

形式如下;

形式1:

<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Korting:
 <tr>
 <td>
 <input type="checkbox" name="korting[]" value=15 /> Student 15% <br>
 <input type="checkbox" name="korting[]" value=10 /> Senior 10% <br>
 <input type="checkbox" name="korting[]" value=5 /> Klant 5% <br>
 <hr />
 </td>
 </tr>
 <tr>
 <td>
 betalingswijze
 <input type="radio" name="betalingswijze" value="paypal"> Paypal
 <input type="radio" name="betalingswijze" value="mastercard"> Mastercard
 <input type="radio" name="betalingswijze" value="visa"> Visa
 <hr />
 </td>
 <tr>
 <td>
    <img src="toshiba.jpg" alt=" " />
 </td>
 </tr>
 <tr>
 <td>
    Toshiba Sattelite A100-510 Basisprijs 999.99
</td>
</tr>
<tr>
<td><!--Shopping Cart Begin-->
    <input type="hidden" name="toshibaproduct" value="001" />
    <input type="hidden" name="toshibamerk" value="toshiba" />
    <input type="hidden" name="toshibamodel" value="Sattelite A100-510" />
    Operating system <select name="toshibaos" value="Toshiba">
    <option value="xp">Windows XP</option>
    <option value="vista">Windows Vista</option>
    <option value="linux">Linux</option>
    </select>
    Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" value="0" />
    <input type="hidden" name="toshibaprijs" value="999.99" />
    <input type="image" src="bestel.jpg" border=0 value="bestellen" />
    <hr />
 <tr>
 <td>
    <img src="acer.jpg" alt=" " />
 </td>
 </tr>
 <tr>
 <td>
    Acer Aspire 5735Z Basisprijs 529.99
</td>
</tr>
<tr>
<td>    
    <input type="hidden" name="acerproduct" value="002" />
    <input type="hidden" name="acermerk" value="acer" />
    <input type="hidden" name="acermodel" value="Aspire 5735Z" />
    Operating system <select name="aceros" value="Acer">
    <option value="xp">Windows XP</option>
    <option value="vista">Windows Vista</option>
    <option value="linux">Linux</option>
    </select>
    Aantal: <input type="text" size=2 maxlength=3 name="aceraantal" value="0" />
    <input type="hidden" name="acerprijs" value="529.99" />
    <input type="image" src="bestel.jpg" border=0 value="bestellen" />
    <hr />
    </td><!--Shopping Cart End-->
</tr>
</form>

表格2

<form name="klant gegevens" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border=1 >
<tr>
<td colspan="2">
    <b>Factuur klantgegevens</b>
</td>
</tr>
<tr>
<td width="100">Naam: </td>
<td>
    <input type="text" sie="55" name="naam" />
</td>
</tr>
<tr>
<tr>
<td>Adres: </td>
<td>
    <input type="text" sie="55" name="adres" />
</td>
</tr>
<tr>
<td>Woonplaats:</td>
<td>
    <input type="text size="34" name="woonplaats">
    Postcode:<input type="text" size="6" name="postcode">
</td>
</tr>
<tr>
<td>e-mail:</td>
<td>
    <input type="text" size="55" name="email">
</td>
</tr>
<tr>
<td>Feedback:</td>
<td>
<textarea cols="40" rows="3" name="commentaar">
</textarea>
</td>
</tr>
</table>
<input type="image" src="checkout.png" value="send"/>
</form>

两者都具有在提交时生效的功能。抱歉空间太大。我在自己的文件中有更好的它们,但我只是不知道如何在这个网站上正确使用它们。

问候,

Lennart

操作表示将接收已发布数据的页面。您可以使用不同的操作,也可以使用具有不同参数的相同操作。如果使用相同的操作,则必须插入一个允许管理不同案例的参数。为此,您可以插入一个隐藏字段。

考虑一下这些简单的形式:

<form name="form_a" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="hidden" name="form" value="A">
    <button type="submit">Form A</button>
</form>
<form name="form_b" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="hidden" name="form" value="B">
    <button type="submit">Form B</button>
</form>

为了管理不同的提交,您必须检查隐藏字段的值:

if(isset($_POST['form'])){
    switch ($_POST['form']) {
        case "A":
            echo "submitted A";
            break;
        case "B":
            echo "submitted B";
            break;
        default:
            echo "What are you doing?";
    } 
} 

您不能同时提交两个单独的表单,因为每次提交都代表对服务器的不同请求。

您可以手动合并这两个表单的字段,也可以使用Javascript为您完成此操作。请记住,如果您通过Javascript执行此操作,表单的字段必须具有不同的名称。

正如你在这里看到的,你可以简单地通过jQuery:

var str1 = $("form_a").serialize();
var str2 = $("form_b").serialize();
$.post(url, str1 + "&" + str2);

其中url是形式的动作参数

您的表单应该是这样的
第一种形式

 <form method="post" >
      ... 
   <input type="hidden" name="form1submission" value="yes" >
    <input type="submit" name="submit" value="Submit" >
  </form>

第二种形式

  <form method="post" >
      ... 
   <input type="hidden" name="form2submission" value="yes" >
    <input type="submit" name="submit" value="Submit" >
  </form>

以及第一个表单的php。

  if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form1submission'])) {
        // first form code. 
   }

以及第二种形式的php代码。

  if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form2submission'])) {
        // second form code. 
   }

就是这样。

复制后

是的,你可以!

首先,为所有表单添加正确的操作,其次,给它们一个唯一的名称

HTML侧

不要忘记添加您想要的http方法(GET或POST)

<form method="post">
  <input type="hidden" name="orderform" />
  <!-- rest of form goes here -->
</form>
<form method="post">
  <input type="hidden" name="klant_gegevens" />
  <!-- rest of form goes here -->
</form>

action-属性保留为空或完全删除它将使表单提交到当前页面(请参阅本文),并且使用$_SERVER["PHP_SELF"]可能导致XSS注入读取在"PHP表单安全大注释"下

注:

避免在字段名称中使用空格,匹配它们可能会产生一些问题。。。

PHP端

通过过滤收到的表单名称获得输入值

if (isset($_POST["orderform"])) {
    // The first form was submitted
}
if (isset($_POST["klant_gegevens"])) {
    // The second form was submitted
}

注:

使用print_r()或var_dump()调试交换值的内容