在同一页面上的2种形式php [英] 2 forms on same page php

查看:68
本文介绍了在同一页面上的2种形式php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的学习簿给我分配了一项作业,该作业要求我在一页上有2个表格并将它们都输出到同一页上.这有可能吗?两种形式都可以独立正常工作.两者都有动作:

My studybook gives me an assignment which requires me to have 2 forms on one page and output them both to the same page. Is this even possible? Both forms work fine independently. Both have the action:

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

两个都有一个提交按钮,但是该按钮仅提交其所属的表单.不过,这可能会引起人们的注意.

Both have a submit button, but the button only submits the form it's part of. This probably makes sence though.

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

Thing is i need the page to either post both form outputs when clicking one of the 2 submit buttons or press them subsequently but the information from the first form needs to stay on the page.

这是可能的还是我在尝试解决不可能的事情?

Is this possible or am i trying do to the impossible?

表格如下;

表格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>

两者都具有在提交时启动的功能.很抱歉的间距.我将它们保存在自己的文件中更好,但我只是不知道如何在此站点上正确放置它们.

Both have functions which kick in on submit. Sorry for the spacings. I have them better in my own files but i just don't know how to get them right on this site.

问候,

Lennart

推荐答案

操作表示将接收已发布数据的页面.您可以使用不同的动作或具有不同参数的相同动作. 如果使用相同的操作,则必须插入一个允许管理不同情况的参数.您可以插入一个隐藏字段来执行此操作.

The action represent the page that will receive the posted data. You may use differents actions or the same action with different parameters. If you use the same action, you had to insert a parameter that permit to manage different cases. You may insert an hidden field to do this.

考虑以下简单形式:

<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>

要管理其他提交,您必须检查隐藏字段的值:

To manage the different submission, you had to check the value of the hidden field:

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?";
    } 
} 

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

You can't submit two separate forms at the same time, because each submission represent a different request to the server.

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

You may merge manually the fields of the two forms, or use Javascript to do this for you. Keep in mind that if you do this via Javascript, the field of the forms had to have differents names.

在您看到此处时,您可以简单地进行操作通过jQuery:

As you caan see here you can do simply via jQuery:

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

其中url是形式的操作参数

这篇关于在同一页面上的2种形式php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆