如何使用PHP处理表单数据并将其提交到其他网页?

时间:2019-04-09 20:19:11

标签: php html

我正在设置一个网页,该网页输出从其他页面输入的表单数据,但是在处理和提交该数据时遇到了麻烦。我希望第二个网页在下面的HTML代码中概述输出。 HTML的第一部分是第一页,称为reservations.html,我想提交到contactform.php页面。

<form class="reservation-form" action="contactform.php" method="post" ng-submit="processForm()">
        <fieldset>
          <legend>Contact Information</legend>
          <label for="firstname">First Name:</label>
          <input type="text" id="firstname" name="firstname" class="form-control" ng-model="formData.firstname" ><br><br>
          <label for="middlename">Middle Name:</label>
          <input type="text" id="middlename" name="middlename" class="form-control" ng-model="formData.middlename"><br><br>
          <label for="lastname">Last Name:</label>
          <input type="text" id="lastname" name="lastname" class="form-control" ng-model="formData.lastname"><br><br>
          <label for="phone"> Phone Number: </label>
          <input type="tel" name="phone" class="form-control" ng-model="formData.phone"><br><br>
          <label for="email">Email Address: </label>
          <input type="email" name="email" class="form-control" ng-model="formData.email"><br><br>
        </fieldset>
<fieldset>
  <legend>Travel Information</legend>
     <label for="locations">Current Locations:</label>
       <select name="locations" id="locations" onchange="activites('locations', 'activitiesList')" ng-model="formData.locations">
        <option value="australia">Brisbane</option>
        <option value="canada">Vancouver</option>
        <option value="newyork">New York City</option>
        <option value="germany">Berlin</option>
        <option value="mexico">Cancun</option>
         </select><br><br>
          <label for="depart-date">Departure Date: </label>
          <input type="date" name="depart-date" id="depart-date" ng-model="formData.departuredate"><br><br>
          <label for="return-date">Return Date: </label>
          <input type="date" name="return-date" id="return-date" ng-model="formData.returndate"><br><br>
          <label for="needed">Options</label><br>
          <input type="checkbox" name="needed" value="flight" ng-model="formData.flight"> Flight<br>
          <input type="checkbox" name="needed" value="hotel" ng-model="formData.hotel"> Hotel<br>
          <input type="checkbox" name="needed" value="car" ng-model="formData.car"> Rental Car<br><br>
</fieldset>
  <div id="activities" class="">
                    <label class="">Choose an Activity</label>
                    <select id="activitiesList" name="activities" ng-model="formData.activities">
                    </select>
                </div>

<fieldset>
 <legend>Billing Information</legend>
   <label for="line1">Address Line 1:</label>
     <input type="text" id="line1" name="line1" ng-model="formData.line1"><br><br>
       <label for="line2">Address Line 2:</label>
     <input type="text" id="line2" name="line2" ng-model="formData.line2"><br><br>
       <label for="city">City: </label>
     <input type="text" name="city" ng-model="formData.city"><br><br>
       <label for="state">State: </label>
     <select name="state" id="state" ng-model="formData.state">
         <option value="AL">Alabama</option>
         <option value="AK">Alaska</option>
         <option value="AZ">Arizona</option>
         <option value="AR">Arkansas</option>
         <option value="CA">California</option>
         <option value="CO">Colorado</option>
         <option value="CT">Connecticut</option>
         <option value="DE">Delaware</option>
         <option value="DC">District Of Columbia</option>
         <option value="FL">Florida</option>
         <option value="GA">Georgia</option>
         <option value="HI">Hawaii</option>
         <option value="ID">Idaho</option>
         <option value="IL">Illinois</option>
         <option value="IN">Indiana</option>
         <option value="IA">Iowa</option>
         <option value="KS">Kansas</option>
         <option value="KY">Kentucky</option>
         <option value="LA">Louisiana</option>
         <option value="ME">Maine</option>
         <option value="MD">Maryland</option>
         <option value="MA">Massachusetts</option>
         <option value="MI">Michigan</option>
         <option value="MN">Minnesota</option>
         <option value="MS">Mississippi</option>
         <option value="MO">Missouri</option>
         <option value="MT">Montana</option>
         <option value="NE">Nebraska</option>
         <option value="NV">Nevada</option>
         <option value="NH">New Hampshire</option>
         <option value="NJ">New Jersey</option>
         <option value="NM">New Mexico</option>
         <option value="NY">New York</option>
         <option value="NC">North Carolina</option>
         <option value="ND">North Dakota</option>
         <option value="OH">Ohio</option>
         <option value="OK">Oklahoma</option>
         <option value="OR">Oregon</option>
         <option value="PA">Pennsylvania</option>
         <option value="RI">Rhode Island</option>
         <option value="SC">South Carolina</option>
         <option value="SD">South Dakota</option>
         <option value="TN">Tennessee</option>
         <option value="TX">Texas</option>
         <option value="UT">Utah</option>
         <option value="VT">Vermont</option>
         <option value="VA">Virginia</option>
         <option value="WA">Washington</option>
         <option value="WV">West Virginia</option>
         <option value="WI">Wisconsin</option>
         <option value="WY">Wyoming</option>
            </select><br><br>
            <label for="zip">Zip Code: </label>
            <input type="text" name="zip" id="zip" ng-model="formData.zip"><br><br>
 </fieldset>
     <button ng-click="submit()" style="color:black;font-size:1em;">
          <b>Submit</b>
     </button><br><br>
     <button ng-click="reset()" style="color:black;font-size:1em;">
        <b>Reset</b>
     </button><br><br>
    </form>
<?php

if (isset($_POST['submit'])) {

    $firstname = $_POST['firstname'];
    $middlename = $_POST['middlename'];
    $lastname = $_POST['lastname'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $locations = $_POST["locations"];
    $depart = $_POST["depart-date"];
    $return = $_POST["return-date"];
    $options = $_POST["options"];
    $line1 = $_POST["line1"];
    $line2 = $_POST["line2"];
    $city = $_POST["city"];
    $state = $_POST["state"];
    $zip = $_POST["zip"];
    $departDB = strtotime($depart);
    $returnDB = strtotime($return);
    $departDB = date('Y-m-d', $departDB);
    $returnDB = date('Y-m-d', $returnDB);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Contact Form</title>
</head>
<body>
    <h1>Thank You</h1>
    <p>Here is the information you have submitted:</p>
    <ol>
        <li><em>First Name:</em> <?php echo $_POST["firstname"]?></li>
        <li><em>Middle Name:</em> <?php echo $_POST["middlename"]?></li>
        <li><em>Last Name:</em> <?php echo $_POST["lastname"]?></li>
        <li><em>Phone Number:</em> <?php echo $_POST["phone"]?></li>
        <li><em>Email:</em> <?php echo $_POST["email"]?></li>
        <li><em>Location:</em> <?php echo $_POST["locations"]?></li>
        <li><em>Departure Date:</em> <?php echo $_POST["depart-date"]?></li>
        <li><em>Return Date:</em> <?php echo $_POST["return-date"]?></li>
        <li><em>Options Selected:</em> <?php echo $_POST["options"]?></li>
        <li><em>Address Line 1:</em> <?php echo $_POST["line1"]?></li>
        <li><em>Address Line 2:</em> <?php echo $_POST["line2"]?></li>
        <li><em>City:</em> <?php echo $_POST["city"]?></li>
        <li><em>State:</em> <?php echo $_POST["state"]?></li>
        <li><em>Zip Code:</em> <?php echo $_POST["zip"]?></li>
    </ol>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

没有表单标签和提交按钮

<form method="POST" action="">
    ...
    <input type="text" name="firstname" value="">
    .. And all other fields ..
    ...
    <input type="submit" name="submit" value="send">
</form>