【问题标题】:PHP, HTML How to achieve 2 actions in a form?PHP、HTML 如何在一个表单中实现 2 个动作?
【发布时间】:2014-12-01 06:14:23
【问题描述】:

我有 1 个主要表格,这是我的索引。我为_REQUEST 链接了第二页。我的问题是……让我们看看我能不能解释一下。我已经查看了这里的所有内容,但无法完全掌握完成此任务的最佳途径。

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

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

第二部分(工作)在此页面上,它将在屏幕上打印出需要填写的内容。我不想要这个。我希望该人留在原始/索引页面上,直到完成。如果按下 SUBMIT 并且一切都已完成,那么它将进入“form_output”页面(由于已经处理完毕,因此无需任何关于丢失信息的警报)。

第三部分(稍微偏离这里,但在数字下拉框中,我怎么能默认说“数字”或“日期”,这两者都是由日期和年份的范围提供的。我能够只需将“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>

【问题讨论】:

    标签: php html forms submit action


    【解决方案1】:

    最简单的方法:将“显示脚本”与“验证脚本”代码结合起来

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 2023-03-12
      • 2014-05-14
      • 2017-07-27
      • 2022-12-17
      相关资源
      最近更新 更多