繁体   English   中英

PHP,HTML如何以表格形式实现2个动作?

[英]PHP, HTML How to achieve 2 actions in a form?

我有1个主要表格,这是我的索引。 我有第二页链接到_REQUEST。 我的问题是...让我们看看是否可以解释它。 我已经看过这里所有的内容,无法完全掌握实现此目的的最佳方法。

我有一个输入工作表。

第一部分(不起作用),我要的是按“提交”按钮时,它将验证输入数据,以及是否有文本框(或其他类型的选择)以及如果表单未完全填写,将保留在该页面上,请保留已填写,已填写的内容,但在“不完整的信息”框旁边打印出类似“必须填写”的内容。

在此页面上的第二部分(工作中)将在屏幕上打印出需要填写的内容。 我不想要这个。 我希望该人在原始/索引页面上停留直到完成。 如果按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>

这是我的第二个页面,或者说是“输出”页面...我希望“提交”停留在第一页上,直到所有内容填写完毕,然后在完成所有操作后进入此页面。 (我知道,如果最终可以解决问题,它将在一开始消除“错误” 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

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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM