0

我有整个代码。混乱,但我想做的是有一个下拉菜单,其中包含 1 -10 的选择。然后在选择一个之后,会吐出另一个表单,如果选择了 3 个,则会显示三行字段(姓名,电子邮件)。包括验证。我首先对要吐出多少行字段进行“硬编码”,但现在我想做的是在下拉菜单中进行“选择”,以便用户能够选择......似乎工作但不处理。任何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']; 

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

1 回答 1

0

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

<!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>
于 2013-09-05T05:28:45.067 回答