2014-10-06 22 views
1

我有1個主要表格,這是我的索引。我有一個鏈接爲_REQUEST的第二頁。我的問題是...讓我們看看我能否解釋它。我已經看過這裏的所有東西,無法完全掌握完成這件事的最佳途徑。PHP,HTML如何在表單中實現2個動作?

我有一個輸入的工作表單。

第一部分(不工作)我想要的是當SUBMIT按鈕被按下時,它將驗證輸入數據,如果一個文本框(或其他類型的選擇),如果表格沒有完全填寫,留在該頁面上,保留填寫內容,填寫內容,但在不完整的信息框旁邊打印出「必須填寫」的內容。

第二部分(工作)在此頁面上,它將在屏幕上打印出需要填寫的內容。我不想要這個。我希望這個人留在原來的/索引頁面,直到它完成。如果按下提交按鈕並且所有內容都已完成,那麼它將進入'form_output'頁面(由於已經被處理,不需要任何關於丟失信息的提示)。

第三部分(稍微偏離這裏,但在一個數字下拉框中,我怎麼能默認說「數字」或「日期」,這些都是由日期和年份的範圍提供的。只是把「MONTH」放在數組填充的「月」框中,但不知道如何在範圍/數字框上有標籤?當我去嘗試使用不同的東西時,我已經建立了它,一旦我通過這個,我將會爲一個我想通過跨越直接HTML而建立的網站進行canabalizing。

這是我的主頁:


  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
      <html xmlns="http://www.w3.org/1999/xhtml"> 
      <head> 
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
      <!-- This is the link to my external css --> 
      <link rel="stylesheet" type="text/css" href="ass2_php.css" /> 

      <!-- This is the css for the _error when it _POST's on this page --> 
      <style type="text/css" title="text/css" media="all"> 
      .error{ 
       font-weight: bold; 
       color:#F00; 
      } 
      </style> 

      <title>My Schedule</title> 
      </head> 

      <body> 
      <center> 

      **<!-- THIS IS MY FIRST QUESTIONABLE AREA OF INTEREST, I WANT THE EFFECT OF BEING ABLE TO DO 2 ACTIONS -->** 

      <form action="formoutput.php" method="post"> 
      <table width="120%" border="0" align="center" cellspacing="0" bordercolor="#006600" id="form"> 

       <tr> 
       <td>&nbsp;</td> 
       <td colspan="2" align="center"><h3>Please fill out all information.</h3></td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       </tr> 
       <tr> 
       <td><?php 
       $fname = "First Name"; 
        echo $fname; 
         ?></td> 
       <td> 
        <label for="fname"> 
        <input type="text" name="fname" id="fname" value="" /> 
        </label> 

       </td> 
       <td>&nbsp;</td> 
       <td></td> 
       <td >&nbsp;</td> 
       <td >&nbsp;</td> 
       <td></td> 
       </tr> 
       <tr> 
       <td> 
       <?php 
       echo "Last Name" 
         ?> 
       </td> 
       <td><label for="lname"> 
        <input type="text" name="lname" id="lname" value=""/> 
        </label> 

        </td> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
       </tr> 
       <tr> 
       <td><?php 
       echo "Email" 
         ?></td> 
       <td> 
        <label for="email"> 
        <input type="text" name="email" id="email" /> 
        </label> 
       </td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       </tr> 
       <tr> 
       <td>Phone Number:</td> 
       <td><input type="number" name="phone" id="phone" value="phone" /></td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       </tr> 
       <tr> 
       <td>Gender</td> 
       <td colspan="2"> 
        <p> 
         <label for="gender"> 
         <input type="radio" name="gender" value="M" /> Male 
         <input type="radio" name="gender" value="F" /> Female 
         </label> 
         <br /> 
        </p> 


       </td> 
       <tr> 
       <td>Comments or Suggestions:</td> 
       <td colspan="3"> 

        <label for="comments"> 
         <textarea name="comments" id="comments" cols="45" rows="5"> 
      </textarea> 
        </label> 
       </td> 
       <td colspan="2">What time of day do you prefer?</td> 
       <td><p> 
        <label for="days"> 
        <input type="checkbox" name="checks[]" value="Days" /> Days <br/> 
        <input type="checkbox" name="checks[]" value="Nights" /> Nights <br/> 
        <input type="checkbox" name="checks[]" value="Weekends" /> Weekends <br/> 
        </label></p> 
       </td> 
       </tr> 
       <tr> 
       <td>How many hours do I work a week?</td> 
        <td> 
        <p> 
       <label> Hours: 
       <select name="hours"> 
       <option value="0-16">Under 16 
       </option> 
       <option value="17-30">Between 17-30 
       </option> 
       <option value="31-40">Up to 40 hours 
       </option> 
       </select> 
       </label> 
       </p> 
       </td> 

       </tr> 
       <tr> 
       <td>What is todays date?</td> 
       <td colspan="2"> 

        <!-- set the variable for the month array for following calendar --> 
        <?php 
       $months = array (1 => 'Month', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); 

       // Set the array for the days and years 
       $days = range (1, 31); 
       $years = range (1970, 2014); 

       echo '<select name="month">'; 
       foreach ($months as $key => $value) { 
        echo "<option value=\"$value\"> 
        $value</option>\n"; 
       } 
       echo '</select>'; 

       echo '<select name="day">'; 
       for ($day = 1; $day <= 31; $day++) { 
        echo "<option value=\"$day\"> 
        $day</option>\n"; 
       } 
       echo '</select>'; 

       echo '<select name="year">'; 
       for ($year = 2011; $year <= 2021; 
        $year++){ 
        echo "<option value=\"$year\"> 
        $year</option>\n"; 
      } 
      echo '</select>'; 

       //from class use array_merge $yearsarray = array_merge($yearsdefault, $years 
      ?> 

       </td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 

       </tr> 
       <tr> 
       <td>Sunday</td> 
       <td>Monday</td> 
       <td>Tuesday</td> 
       <td>Wedneday</td> 
       <td>Thursday</td> 
       <td>Friday</td> 
       <td>Saturday</td> 
       </tr> 

       </fieldset> 

       <tr> 
       <td><input name="submit" type="submit" value="Submit Information" /></td> 
       <td><button type="reset" name="reset" value="Reset" >Reset</button> 
       </td> 

      </table> 

      </body> 
      </html> 

這是我的第二個,或者OUTPUT頁......我要提交給留在第一頁上,直到一切都填寫了,然後來到這個頁面完成一切時。 (我知道,如果這可以結束工作,將消除一開始的「錯誤」CSS)。

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang+"en" lang="en"> 

      <head> 
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"   /> 
      <link rel="stylesheet" type="text/css" href="ass2_php.css" /> 
      <style type="text/css" title="text/css" media="all"> 
      .error { 
       font-weight: bold; 
       color: #c00 
      } 
      </style> 
      </head> 

      <title>Form Feedback</title> 

      <body> 
      <p><a href="index.php">Back</a></p> 
      <p> 
      <?php # Script 2.2 - formoutput.php 
      //$fname = $_REQUEST['fname']; 

      if (!empty($_REQUEST['fname'])) { 
       $name = $_REQUEST['fname']; 
      } else { 
       $fname = NULL; 
       echo '<p class="error">You forgot to enter your first name!</p>  '; 
      } 
      ?> 

      <?php 
      // $lname = $_REQUEST['lname']; 

      if (!empty($_REQUEST['lname'])) { 
       $laname = $_REQUEST['lname']; 
      } else { 
       $lname = NULL; 
       echo '<p class="error">You forgot to enter your last name!</p>'  ; 
      } 
      ?> 

      <?php 
      $email = $_REQUEST['email']; 
      $hours = $_REQUEST['hours']; 
      $comments = $_REQUEST['comments']; 
      $prefer = $_REQUEST['checks']; 
      $phone = $_REQUEST['phone']; 

      // This is the setting of the gender - radio box variable 
      if (isset($_REQUEST['gender'])) { 
       $gender = $_REQUEST['gender']; 
      } else { 
       $gender = NULL; 
      } 
      ?> 

      <?php 
       if ($gender == "M") { 
        echo 'Good day Mr. '; 
        echo ucfirst($laname) . "."; 
       } 
       elseif ($gender == "F") { 
        echo 'Good day Lady '; 
        echo ucfirst($name) . "."; 
       } 
      // else ($gender == NULL){ 
      //  echo 'You forgot to pick a gender.'; 
      //  } 
      ?> 
      <br/> 

      <?php 
      echo " Thank you for taking the time to fill out this form, "; 
      echo ucfirst($name); 
      echo " " . ucfirst($laname); 
      ?> 
       <br /> 
       <br/> 
      <?php 
      echo "We will be contacting you at the email address you gave us   at, " . $email . ". "; 
      echo "And we have your phone number as " . $phone . "."; 
      ?> 
       <br /> 

       <?php 
      echo " And you prefer to work " . $prefer . "."; 
      ?> 
       <br /> 
       <br /> 
      <br/> 
      <?php 
      echo "We're glad you are willing to work " . $hours . " hours!"; 
      ?> 
       <br /> 
       <br/> 

      <?php 
      echo "You made these comments: "; 
      ?> 
      <br/> 
      <?php 
      echo $comments . "."; 
      ?> 

      </p> 
      <table width="40%" border="1" cellspacing="1" cellpadding="1"> 
       <tr> 
       <td>Sunday</td> 
       <td>Monday</td> 
       <td>Tuesday</td> 
       <td>Wednesday</td> 
       <td>Thursday</td> 
       <td>Friday</td> 
       <td>Saturday</td> 
       </tr> 
      </table> 

      </body> 
      </html> 

回答

0

最簡單的方法:結合您的「顯示腳本」與「驗證腳本」代碼

<?php 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    ... handle the form submission 
    ... validate the form submission 
} 

if ($form_ok) { 
    .. save form data into session 
    header("Location: next_stage.php"); 
    exit(); 
} 

... display form again and fill in any field data, if available. 
... if code reaches here, then either the form was just loaded for the first time 
.... or validation failed and you're prompting to correct any errors.