2013-09-05 2 views
0

У меня есть весь этот код. Сбитый, но я пытаюсь сделать это с выпадающим меню с выбором от 1 до 10. Затем, выбрав один из них, выплюнул бы другую форму, чтобы, если бы были выбраны 3, отобразилось бы три строки полей (имя, адрес электронной почты). Валидация включена. Что я был первым «жестко закодированным» на том, сколько строк полей выплевывает, но теперь я хотел бы сделать «выбор» в выпадающем меню, чтобы пользователь имел возможность выбирать .... Кажется, что нужно работать но не обрабатывается. Любая помощь PHP-специалиста? Нет db, нет клиентской базы, нет smart alec. Если у вас есть время, чтобы помочь, пожалуйста.Несколько форм раскрытия формы, проверка, 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: [email protected]'); 
} 
// 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> 

ответ

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: [email protected]'); 
      } 
// 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> 
Смежные вопросы