我如何一次从php服务器上的两种表单中获取信息?

时间:2018-12-16 11:36:11

标签: php html forms

我正在处理html表单。我已使用下面的代码来设置表单。

<div class="formElementsWrapper formElementsUsername">
            <label for="userid" class="hidden">Username</label>
            <input type="text" accesskey="U" id="userid" class="formElement formElementText" name="j_username" maxlength="14" value="" placeholder="Username" autocomplete="off">
        </div>
        <div class="formElementsWrapper formElementsPassword">
            <label for="password" class="hidden">Password</label>
            <input type="password" accesskey="P" id="password" class="formElement formElementPassword" name="j_password" maxlength="32" placeholder="Password" autocomplete="off">
        </div>
        <div id="saveuid" class="formElementsWrapper formElementsSaveUsername">
            <input type="checkbox" accesskey="S" id="saveusername" class="c29link formElement formElementCheckbox" name="saveusername" data-content-id="c29content-save-username" triggerhover="false" isclickable="true">
            <label for="saveusername" class="lsc">Save username<span class="hidden">Opens a dialog</span></label>
            <input type="hidden" name="save-username" id="save-username" value="false">
            <input type="hidden" name="hdnuserid" id="hdnuserid">
        </div>
        <div class="formSubmit">
            <input type="submit" value="Sign On" name="btnSignon" id="btnSignon" class="c7" data-mrkt-tracking-id="3d6c76ba-9d34-4def-977d-a79cb8afc738">

提交此表单时,我希望该表单将我带到下一个包含其他信息的表单页面。我已经使用php代码完成了此操作

<form action="verification.php" method="post"> 

<div class="formElementsWrapper formElementsUsername">
            <label for="userid" class="hidden">Username</label>
            <input type="text" accesskey="U" id="userid" class="formElement formElementText" name="j_username" maxlength="14" value="" placeholder="Username" autocomplete="off">
        </div>
        <div class="formElementsWrapper formElementsPassword">
            <label for="password" class="hidden">Password</label>
            <input type="password" accesskey="P" id="password" class="formElement formElementPassword" name="j_password" maxlength="32" placeholder="Password" autocomplete="off">
        </div>
        <div id="saveuid" class="formElementsWrapper formElementsSaveUsername">
            <input type="checkbox" accesskey="S" id="saveusername" class="c29link formElement formElementCheckbox" name="saveusername" data-content-id="c29content-save-username" triggerhover="false" isclickable="true">
            <label for="saveusername" class="lsc">Save username<span class="hidden">Opens a dialog</span></label>
            <input type="hidden" name="save-username" id="save-username" value="false">
            <input type="hidden" name="hdnuserid" id="hdnuserid">
        </div>
        <div class="formSubmit">
            <input type="submit" value="Sign On" name="btnSignon" id="btnSignon" class="c7" data-mrkt-tracking-id="3d6c76ba-9d34-4def-977d-a79cb8afc738">

最后,当我提交第二个表单时,我希望第一个表单和第二个表单上的信息都通过服务器上的index.php文件发送到我的php服务器。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

正如您提到的,您可以通过使用每种形式的POST值以及一系列if / elseif语句来在一页上执行此操作,如下所示;

<?php

//check if the "page" value is NOT set and if true show the "page 1 form"

if(!ISSET($_POST["page"])){

//open the form tag
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
echo '<div class="formElementsWrapper formElementsUsername">
            <label for="userid" class="hidden">Username</label>
            <input type="text" accesskey="U" id="userid" class="formElement formElementText" name="j_username" maxlength="14" value="" placeholder="Username" autocomplete="off">
        </div>
        <div class="formElementsWrapper formElementsPassword">
            <label for="password" class="hidden">Password</label>
            <input type="password" accesskey="P" id="password" class="formElement formElementPassword" name="j_password" maxlength="32" placeholder="Password" autocomplete="off">
        </div>
        <div id="saveuid" class="formElementsWrapper formElementsSaveUsername">
            <input type="checkbox" accesskey="S" id="saveusername" class="c29link formElement formElementCheckbox" name="saveusername" data-content-id="c29content-save-username" triggerhover="false" isclickable="true">
            <label for="saveusername" class="lsc">Save username<span class="hidden">Opens a dialog</span></label>
            <input type="hidden" name="save-username" id="save-username" value="false">
            <input type="hidden" name="hdnuserid" id="hdnuserid">
        </div>';

//now we set the "page value" by using a hidden input field
echo '        <div class="formSubmit">
        <input type="hidden" name="page" value="1" />
            <input type="submit" value="Sign On" name="btnSignon" id="btnSignon" class="c7" data-mrkt-tracking-id="3d6c76ba-9d34-4def-977d-a79cb8afc738">';

//always remember to close all your tags

echo '</div></form>';

}
//now we end that "if statement" with the bracket above and below create another check, for page 2
elseif($_POST["page"]==1){

//if this parameter shows as page one continue below

//start the session
session_start();
//collect the data from the first form and put it into a session values
$_SESSION["uid"] = $_POST["userid"];

//display the second form
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post"> 

<div class="formElementsWrapper formElementsUsername">
            <label for="userid" class="hidden">Username</label>
            <input type="text" accesskey="U" id="userid" class="formElement formElementText" name="j_username" maxlength="14" value="" placeholder="Username" autocomplete="off">
        </div>
        <div class="formElementsWrapper formElementsPassword">
            <label for="password" class="hidden">Password</label>
            <input type="password" accesskey="P" id="password" class="formElement formElementPassword" name="j_password" maxlength="32" placeholder="Password" autocomplete="off">
        </div>
        <div id="saveuid" class="formElementsWrapper formElementsSaveUsername">
            <input type="checkbox" accesskey="S" id="saveusername" class="c29link formElement formElementCheckbox" name="saveusername" data-content-id="c29content-save-username" triggerhover="false" isclickable="true">
            <label for="saveusername" class="lsc">Save username<span class="hidden">Opens a dialog</span></label>
            <input type="hidden" name="save-username" id="save-username" value="false">
            <input type="hidden" name="hdnuserid" id="hdnuserid">
        </div>';

//again create the page number
echo '        <div class="formSubmit">
            <input type="hidden" name="page" value="2" />
<input type="submit" value="Sign On" name="btnSignon" id="btnSignon" class="c7" data-mrkt-tracking-id="3d6c76ba-9d34-4def-977d-a79cb8afc738">';

//again, remember to close your tags
echo '<div></form>';

//end if statement
}

//check if page 2 is complete
elseif($_POST["page"]==2){

echo $_SESSION["uid"] . ', your form has been completed';
echo '<br><br>';
echo 'Thanks for your submission';

}

?>

在最后的elseif语句中,您将需要对值进行一些操作,但这应该可以使您开始进行项目。

在您将任何内容发布到Internet上之前,我会强烈建议您阅读一些基本信息,请确保包括在内;  -防止SQL注入  -安全可靠地加密密码  -使用数组

您也不想在会话标题中传递密码,因此请谨慎规划计划

祝你好运!

答案 1 :(得分:-1)

在从第二个表单数据中获取数据之前,请使用php会话临时存储您的第一个表单数据。

提交第一个表格后,您可以通过php函数进行存储:

session_start()
$_SESSION['first_form'] = $_POST;

通过执行此操作,您可以重定向到第二种形式。并从中获取数据。然后提交