多表单下拉表单、验证、PHP

多表单下拉表单、验证、PHP,php,forms,validation,nested-forms,Php,Forms,Validation,Nested Forms,我有完整的密码。混乱,但我正在尝试做的是有一个下拉菜单,从1-10选择。然后,在选择一个后,将弹出另一个表单,这样,如果选择了3个,将显示三行字段(姓名、电子邮件)。包括验证。我第一次“硬编码”的是要吐出多少行字段,但现在我想做的是在下拉菜单中有一个“选择”,以便用户能够选择。。。。似乎有效,但无法处理。有PHP专家的帮助吗?没有数据库,没有客户端,没有智能alec。如果你有时间帮忙,请做 <!DOCTYPE html> <html> <head> <t

我有完整的密码。混乱,但我正在尝试做的是有一个下拉菜单,从1-10选择。然后,在选择一个后,将弹出另一个表单,这样,如果选择了3个,将显示三行字段(姓名、电子邮件)。包括验证。我第一次“硬编码”的是要吐出多少行字段,但现在我想做的是在下拉菜单中有一个“选择”,以便用户能够选择。。。。似乎有效,但无法处理。有PHP专家的帮助吗?没有数据库,没有客户端,没有智能alec。如果你有时间帮忙,请做

<!DOCTYPE html>
<html>
<head>
<title>PHP FORM </title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="container">

<?php 

// Print some introductory text:
echo '<h2>Party Invitation Form</h2>
<p>Please enter list of people with first name, last name and email address to get an     invitation by email.</p>';


if (isset($_POST['submit-invite'])) { //DROPDOWN MENU

$row = $_POST['invitee']; 

// Check if the form has been submitted:
if (isset($_POST['submit'])) {

$problem = FALSE; // No problems so far.

// Check for each value...
for ($i = 1; $i < count($_POST['email']); $i++) {

if (empty($_POST['firstname'][$i])) {
$problem = TRUE;

echo '<input type="text" name="firstname[]" size="20" />';

}

if (empty($_POST['lastname'][$i])) {
$problem = TRUE;
}

if (empty($_POST['email'][$i]) || (substr_count($_POST['email'][$i], '@') != 1) ) {
$problem = TRUE;
}

}  



if (!$problem) { // If there weren't any problems...

// Print a message:
echo '<p><b>Thank you for registering! We will send each one an invitation:          <b>     </b></p>';
for ($i = 0; $i < count($_POST['email']); $i++) {
    $row = $i+1;
    echo $row.": ".$_POST['firstname'][$i]." ".$_POST['lastname'][$i].",  ".$_POST['email'][$i]."    <br/>";

 // Send the email:

$body = file_get_contents("Lab12_Obj1_email_template.txt");
$body = str_replace("#firstname#",$_POST['firstname'][$i],$body);
$body = str_replace("#lastname#",$_POST['lastname'][$i],$body);
$body = str_replace("#email#",$_POST['email'][$i],$body);

mail($_POST['email'][$i], 'Party Invitation', $body, 'From: jvicencio@johnvicencio.com');
}
// Clear the posted values:
$_POST = array();

} else { // Forgot a field.

echo '<p id="error">* Required field! Please try again. Thank you.</p>';

}

} // End of handle form IF.

//show form

 ?>

<form action="" method="post">
<table>
<tr style="font-weight:bold">
<td>First name:</td>
<td>Last name:</td>
<td>Email:</td>
</tr>
<?php for ($i = 1; $i <= $row; $i++) { ?>
<tr>
<td><?php if ($problem == TRUE) { echo '<p id="error">*'; } ?>

<? echo $i.': '; ?><input type="text" name="firstname[]" size="20" value="<?php if  (isset($_POST['firstname'][$i])) { print htmlspecialchars($_POST['firstname'][$i]); } ?>" />

</td>
<td><?php if ($problem == TRUE) { echo '<p id="error">*'; } ?>
<input type="text" name="lastname[]" size="20" value="<?php if (isset($_POST['lastname'] [$i])) { print htmlspecialchars($_POST['lastname'][$i]); } ?>" />
</td>
<td><?php if ($problem == TRUE) { echo '<p id="error">*'; } ?><input type="text"  name="email[]" size="20" value="<?php if (isset($_POST['email'][$i])) { print htmlspecialchars($_POST['email'][$i]); } ?>" />
</td>

</tr>

<?php } ?>

<tr><td><p><input type="submit" class="button" name="submit" value="Register!" /></td>
</tr>
</table>
</form>

<?php
}
else {
echo '
<form action="" method="post">
<select name="invitee">
<option value="">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
<option value="7">Seven</option>
<option value="8">Eight</option>
<option value="9">Nine</option>
<option value="10">Ten</option>
</select>
<input type="submit" class="button" name="submit-invite" value="Invite">
</form>
';
}
?>  
</div>
</body>
</html>

PHP表单

您必须将第二个表单提交代码移到第一个表单提交代码之外。试试这个

<!DOCTYPE html>
<html>
<head>
    <title>PHP FORM </title>
    <link rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="container">

    <?php

    // Print some introductory text:
    echo '<h2>Party Invitation Form</h2>
<p>Please enter list of people with first name, last name and email address to get an     invitation by email.</p>';


    if (isset($_POST['submit-invite'])) { //DROPDOWN MENU

        $row = $_POST['invitee'];
        $problem = false; //always try to initialize variables, you may not run into unexpected warnings and problems.
//show form

        ?>

        <form action="" method="post">
            <table>
                <tr style="font-weight:bold">
                    <td>First name:</td>
                    <td>Last name:</td>
                    <td>Email:</td>
                </tr>
                <?php for ($i = 1; $i <= $row; $i++) { ?>
                    <tr>
                        <td><?php if ($problem == TRUE) {
                                echo '<p id="error">*';
                            } ?>

                            <? echo $i . ': '; ?><input type="text" name="firstname[]" size="20"
                                                        value="<?php if (isset($_POST['firstname'][$i])) {
                                                            print htmlspecialchars($_POST['firstname'][$i]);
                                                        } ?>"/>

                        </td>
                        <td><?php if ($problem == TRUE) {
                                echo '<p id="error">*';
                            } ?>
                            <input type="text" name="lastname[]" size="20"
                                   value="<?php if (isset($_POST['lastname'] [$i])) {
                                       print htmlspecialchars($_POST['lastname'][$i]);
                                   } ?>"/>
                        </td>
                        <td><?php if ($problem == TRUE) {
                                echo '<p id="error">*';
                            } ?><input type="text" name="email[]" size="20"
                                       value="<?php if (isset($_POST['email'][$i])) {
                                           print htmlspecialchars($_POST['email'][$i]);
                                       } ?>"/>
                        </td>

                    </tr>

                <?php } ?>

                <tr>
                    <td><p><input type="submit" class="button" name="submit" value="Register!"/></td>
                </tr>
            </table>
        </form>

    <?php
    } // moved second for submit to here
    elseif (isset($_POST['submit'])) { // Check if the form has been submitted:

        $problem = FALSE; // No problems so far.

// Check for each value...
        for ($i = 1; $i < count($_POST['email']); $i++) {

            if (empty($_POST['firstname'][$i])) {
                $problem = TRUE;

                echo '<input type="text" name="firstname[]" size="20" />';

            }

            if (empty($_POST['lastname'][$i])) {
                $problem = TRUE;
            }

            if (empty($_POST['email'][$i]) || (substr_count($_POST['email'][$i], '@') != 1)) {
                $problem = TRUE;
            }

        }


        if (!$problem) { // If there weren't any problems...

// Print a message:
            echo '<p><b>Thank you for registering! We will send each one an invitation:          <b>     </b></p>';
            for ($i = 0; $i < count($_POST['email']); $i++) {
                $row = $i + 1;
                echo $row . ": " . $_POST['firstname'][$i] . " " . $_POST['lastname'][$i] . ",  " . $_POST['email'][$i] . "    <br/>";

                // Send the email:

                $body = file_get_contents("Lab12_Obj1_email_template.txt");
                $body = str_replace("#firstname#", $_POST['firstname'][$i], $body);
                $body = str_replace("#lastname#", $_POST['lastname'][$i], $body);
                $body = str_replace("#email#", $_POST['email'][$i], $body);

                mail($_POST['email'][$i], 'Party Invitation', $body, 'From: jvicencio@johnvicencio.com');
            }
// Clear the posted values:
            $_POST = array();

        } else { // Forgot a field.

            echo '<p id="error">* Required field! Please try again. Thank you.</p>';

        }

    } // End of handle form IF.


    else {
        echo '
            <form action="" method="post">
            <select name="invitee">
            <option value="">Select</option>
            <option value="1">One</option>
            <option value="2">Two</option>
            <option value="3">Three</option>
            <option value="4">Four</option>
            <option value="5">Five</option>
            <option value="6">Six</option>
            <option value="7">Seven</option>
            <option value="8">Eight</option>
            <option value="9">Nine</option>
            <option value="10">Ten</option>
            </select>
            <input type="submit" class="button" name="submit-invite" value="Invite">
            </form>
            ';
    }
    ?>
</div>
</body>
</html>

PHP表单
名字:
姓氏:
电邮: