dongxun5349 2014-10-06 21:56
浏览 25

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

I have 1 main form which is my index. I have a second page linked for a _REQUEST. My question is... let's see if I can explain it. I've looked at all the stuff here and can't quite get a grasp on the best route for accomplishing this.

I have a working form with inputs.

FIRST PART (not working) What I want is when the SUBMIT button is pressed that it will verify the input data and if a text box(or other type of selection) and if the form is not completely filled out that will stay on that page, keep what is filled out, filled out but print out next to the incomplete info box something along the lines of "this must be filled out".

SECOND PART (working) on this page it will print out on the screen what needs to be filled out. I dont want this. I want the person to stay on the original/index page until it is completed. If when the SUBMIT is pressed and everything IS completed then it will go the 'form_output' page (with no need for any alerts of missing info due to already being taken care of).

THIRD PART (slightly off here, but on a number drop down box how can I have it default to say "NUMBER" or "DATE" with both of these being supplied by a range for date and year. I was able to just put "MONTH" on the array populated 'month' box but cant figure out how to have a label on the range/numeric box?

I'm new to PHP and this may be kind of sloppy. I've built it as I go just trying to use different things I find. Once I get through this I will be canabalizing it for a website I'm trying to build by stepping outside of straight HTML.

This is the code for my main page:


            <!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>
";
                }   
                echo '</select>';

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

                echo '<select name="year">';
                for ($year = 2011; $year <= 2021;
                     $year++){
                    echo "<option value=\"$year\">
                    $year</option>
";
            }
            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>

This is my 2nd, or, OUTPUT page... I want SUBMIT to stay on the first page until everything is filled out then come to this page when everything is completed. (I am aware that would eliminate the "error" css in the beginning if this can end up working).

            <!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条回答 默认 最新

  • dqc19941228 2014-10-06 22:00
    关注

    Simplest method: combine your "show the script" with the "validate the script" code

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

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起