PHP,HTML如何在表单中实现2个动作?

时间:2014-10-06 21:56:00

标签: php html forms submit action

我有1个主要表单,这是我的索引。我有一个链接为_REQUEST的第二页。我的问题是......让我们看看能否解释一下。我已经看过这里的所有内容,并且无法完全掌握实现这一目标的最佳途径。

我有一个带输入的工作表。

第一部分(不工作)我想要的是当按下SUBMIT按钮时它将验证输入数据以及文本框(或其他类型的选择)以及表格是否未完全填写在那个页面上,保留填写的内容,填写完成但打印出不完整的信息框旁边的“必须填写”的内容。

第二部分(工作)在此页面上,它将在屏幕上打印出需要填写的内容。我不想要这个。我想让这个人留在原始/索引页面上直到它完成。如果按下SUBMIT并完成所有操作,那么它将进入'form_output'页面(由于已经处理,不需要任何丢失信息的警报)。

第三部分(稍微偏离此处,但在数字下拉框中我怎么能默认说“NUMBER”或“DATE”这两个都是由日期和年份的范围提供。我能够只需在阵列填充的“月份”框中输入“MONTH”,但无法弄清楚如何在范围/数字框上添加标签?

我是PHP新手,这可能有点草率。当我试图使用我发现的不同的东西时,我已经构建了它。一旦我完成了这一点,我将通过跨越直接HTML的方式为我正在尝试构建的网站强化它。

这是我的主页的代码:


            <!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页面......我希望SUBMIT保持在第一页,直到所有内容都填写完毕,然后在完成所有内容后进入此页面。 (我知道如果这最终会起作用的话,会在开头消除“错误”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>

1 个答案:

答案 0 :(得分:0)

最简单的方法:结合你的&#34;显示脚本&#34;使用&#34;验证脚本&#34;代码

<?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.