将表单数据发布到重定向页面

时间:2016-11-18 18:44:58

标签: javascript php html redirect

我有一个联系表单,我使用phpmailer发送表单中输入的信息,一旦发送,就会被重定向到另一个页面。哪个效果很好,现在我想要的是将所有发送到电子邮件地址的信息也发布在重定向页面上(即 - target.php)。我使用3页1代表html,1代表验证(js)和1代(phpmailer)发送。我在这里已经阅读了其他问题,如果您将重定向页面放在操作中,我知道#34; target.php"在表单上你可以得到数据,除了我必须把动作" appt.php"这是我的phpmailer表单。我试过了:

 <body>
<div class="container">
    <div class="row">
        <h2>Form data</h2>
        <hr/>
        <p>This is a simple page showing the data you have just submitted</p>
        <?php
        $fullnameField = $_POST['fullname'];
        echo $fullnameField;
        $addressField = $_POST['address'];
        echo $addressField;
        ?>
  </div>
 </div>
</body>

我将上面的代码放在target.php页面上。

这是重定向到target.php页面时的结果:

注意:未定义的索引:第14行的C:\ xampp \ htdocs \ appt \ target.php中的fullname

注意:未定义的索引:第16行的C:\ xampp \ htdocs \ appt \ target.php中的地址

还尝试在$结束时取消Field并仍然获得相同的代码。

这是html页面:

<form id="defaultForm" method="post" class="form-horizontal" action="appt.php">
   <fieldset>

  <!-- Form Name -->
  <legend>Appointment Request</legend>

  <!-- Full Name -->

  <div class="form-group">
    <label class="col-md-4 control-label">Full Name*</label>
    <div class="col-md-6  inputGroupContainer">
      <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
        <input  name="fullname" placeholder="Your Full Name" class="form-control"  type="text">
      </div>
    </div>
  </div>

  <!-- Address -->

  <div class="form-group">
    <label class="col-md-4 control-label" >Address*</label>
    <div class="col-md-6  inputGroupContainer">
      <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-road"></i></span>
        <input name="address" placeholder="Your Address" class="form-control"  type="text">
      </div>
    </div>
  </div>


  <!-- City -->

  <div class="form-group">
    <label class="col-md-4 control-label">City*</label>
    <div class="col-md-6  inputGroupContainer">
      <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
        <input name="city" placeholder="Your City" class="form-control" type="text">
      </div>
    </div>
  </div>

  <!-- State Select -->

  <div class="form-group">
    <label class="col-md-4 control-label">State*</label>
    <div class="col-md-6 selectContainer">
      <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
        <select name="state" class="form-control selectpicker" >
          <option value=" " >Please select your state</option>
          <option>Ohio</option>
          <option>Pennsylvania</option>
        </select>
      </div>
    </div>
  </div>

  <!-- Zip Code -->

  <div class="form-group">
    <label class="col-md-4 control-label">Zip Code*</label>
    <div class="col-md-6  inputGroupContainer">
      <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-globe"></i></span>
        <input name="zip" placeholder="Your Zip Code" class="form-control" type="text">
      </div>
    </div>
  </div>

  <!-- Phone Number -->

  <div class="form-group">
    <label class="col-md-4 control-label">Phone*</label>
    <div class="col-md-6  inputGroupContainer">
      <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
        <input name="phone" placeholder="(330)123-1234" class="form-control"  type="text">
      </div>
    </div>
  </div>

  <!-- Email -->

  <div class="form-group">
    <label class="col-md-4 control-label">E-mail*</label>
    <div class="col-md-6  inputGroupContainer">
      <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
        <input name="email" placeholder="Your Email Address" class="form-control"  type="text">
      </div>
    </div>
  </div>

  <!-- Appt Reason Select -->

  <div class="form-group">
    <label class="col-md-4 control-label">Appt Reason*</label>
    <div class="col-md-6 selectContainer">
      <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
        <select name="reason" class="form-control selectpicker" >
         <option value=" ">Select Your Appointment Reason</option>
         <option>Roofing</option>
         <option>Siding</option>
         <option>Doors</option>
         <option>Windows</option>
         <option>Decking</option>
         <option>Comfort Rooms</option>
         <option>Kitchen</option>
        </select>
      </div>
    </div>
  </div>

  <!-- Appt Date -->

  <div class="form-group">
    <label class="col-md-4 control-label">Appt Date*</label>
    <div class="col-md-6  inputGroupContainer">
      <div class="input-group date" id="datepicker"> <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
        <input name="apptDate" placeholder="Date You Would Like Appt" class="form-control"  type="text">
      </div>
    </div>
  </div>

  <!-- Appt Time-->

  <div class="form-group">
    <label class="col-md-4 control-label">Appt Time*</label>
    <div class="col-md-6  inputGroupContainer">
      <div class="input-group date" id="timepicker"> <span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
        <input name="apptTime" placeholder="Time You Would Like Appt" class="form-control"  type="text">
      </div>
    </div>
  </div>

  <!-- Contact Method Select -->

  <div class="form-group">
    <label class="col-md-4 control-label">Contact You*</label>
    <div class="col-md-6 selectContainer">
      <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
        <select name="method" class="form-control selectpicker" >
         <option value=" ">Select Your Contact Method</option>
         <option>Email</option>
         <option>Phone</option>
        </select>
      </div>
    </div>
  </div>

    <!-- Project Description -->

  <div class="form-group">
    <label class="col-md-4 control-label">Project Description*</label>
    <div class="col-md-6  inputGroupContainer">
      <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
        <textarea class="form-control" name="comment" placeholder="Project Description" rows="5"></textarea>
      </div>
    </div>
  </div>

  <!-- Captcha -->

  <div class="form-group">
      <label class="col-md-4 control-label">Captcha</label>
      <div class="col-md-6 inputGroupContainer">
       <div id="captchaContainer"></div>
     </div>
  </div>

  <!-- Button -->

  <div class="form-group">
    <label class="col-md-4 control-label"></label>
    <div class="col-md-4">
      <button type="submit" class="btn btn-primary" >Send <span class="glyphicon glyphicon-send"></span></button>
    </div>
  </div>

 </fieldset>
</form>

appt.php(phpmailer表格):

 <?php
session_start();
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 */

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');   

//Contact Form Data
$fullnameField = $_POST['fullname'];
$addressField = $_POST['address'];
$cityField = $_POST['city'];
$stateField = $_POST['state'];
$zipcodeField = $_POST['zip'];
$phoneField = $_POST['phone'];
$emailField = $_POST['email'];
$apptReasonField = $_POST['reason'];
$apptDateField = $_POST['apptDate'];
$apptTimeField = $_POST['apptTime'];
$methodField = $_POST['method'];
$commentsField = $_POST['comment'];

require 'mailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.aol.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username";

//Password to use for SMTP authentication
$mail->Password = "password";

//Set who the message is to be sent from
$mail->setFrom('sent from');

//Set who the message is to be sent to
$mail->addAddress('sent to');

//Set the subject line
$mail->Subject = 'Information For Appointment Wanted';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->Body = <<<EOD
<br><p>Here is the information $fullnameField entered in your Appointment form.</p>
<br><hr><br>
<b>Name:</b>&nbsp; $fullnameField<hr><br>
<b>Address:</b>&nbsp; $addressField<hr><br>
<b>City:</b>&nbsp; $cityField<hr><br>
<b>State:</b>&nbsp; $stateField<hr><br>
<b>Zip Code:</b>&nbsp; $zipcodeField<hr><br>
<b>Phone #:</b>&nbsp; $phoneField<hr><br>
<b>E-mail:</b>&nbsp; $emailField<hr><br>
<b>Appointment Reason:</b>&nbsp; $apptReasonField<hr><br>
<b>Date Wanted For Appointment:</b>&nbsp; $apptDateField<hr><br>
<b>Time Wanted For Appointment:</b>&nbsp; $apptTimeField<hr><br>
<b>Best Way To Contact You:</b>&nbsp; $methodField<hr><br>
<b>Project Description:</b>&nbsp; $commentsField<hr><br>
EOD;


//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

//send the message, check for errors
if(!$mail->Send())
{
 echo "Mailer Error: " . $mail->ErrorInfo;
}else{

$_SESSION[fullname]=$fullnameField; 
$_SESSION[address]=$addressField; 
$_SESSION[city]=$cityField;
$_SESSION[state]=$stateField; 
$_SESSION[zip]=$zipcodeField; 
$_SESSION[phone]=$phoneField;
$_SESSION[email]=$emailField; 
$_SESSION[reason]=$apptReasonField; 
$_SESSION[apptDate]=$apptDateField;
$_SESSION[apptTime]=$apptTimeField;
$_SESSION[method]=$methodField;
$_SESSION[comment]=$commentsField;

     header('Location: http://localhost/appt/target.php');

}

这是target.php

    <?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
    <title>Thank You</title>
    <link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>

    <div class="container">
        <div class="row">
            <p class="thank">Thank you <b><?php echo $_SESSION['fullname']; ?></b> for scheduling an appointment with U.S. Pride Home Specialists. We will be contacting you by the contact method you chose as soon as possible.</p>
        </div>
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8">Your Appointment Form Information</div><div class="col-md-2"></div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8"><b>Name:</b> <?php echo $_SESSION['fullname']; ?></div><div class="col-md-2"></div>
        </div>
        <hr>        
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8"><b>Address:</b> <?php echo $_SESSION['address']; ?></div><div class="col-md-2"></div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8"><b>City:</b> <?php echo $_SESSION['city']; ?></div><div class="col-md-2"></div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8"><b>State:</b> <?php echo $_SESSION['state']; ?></div><div class="col-md-2"></div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8"><b>Zip Code:</b> <?php echo $_SESSION['zip']; ?></div><div class="col-md-2"></div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8"><b>Phone:</b> <?php echo $_SESSION['phone']; ?></div><div class="col-md-2"></div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8"><b>E-mail:</b> <?php echo $_SESSION['email']; ?></div><div class="col-md-2"></div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8"><b>Appointment Reason:</b> <?php echo $_SESSION['reason']; ?></div><div class="col-md-2"></div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8"><b>Appointment Date Wanted:</b> <?php echo $_SESSION['apptDate']; ?></div><div class="col-md-2"></div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8"><b>Appointment Time Wanted:</b> <?php echo $_SESSION['apptTime']; ?></div><div class="col-md-2"></div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8"><b>Way You Wanted Contacted:</b> <?php echo $_SESSION['method']; ?></div><div class="col-md-2"></div>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-2"></div><div class="col-md-8"><b>Project Description:</b> <?php echo $_SESSION['comment']; ?></div><div class="col-md-2"></div>
        </div>          
    </div>
<?php
// remove all session variables
session_unset(); 

// destroy the session 
session_destroy();
?>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

您需要将表单数据存储在数据库表或会话中以实现持久性。 类似的东西:

SELECT COALESCE(convert(int,convert(varchar(6),expected.date, 112)), reality.date) AS [expectedDate], 
       Sum(expected.items), 
       Sum(reality.items) 
FROM   a expected FULL JOIN b reality
       ON convert(int,convert(varchar(6),expected.date, 112)) = reality.date
GROUP  BY COALESCE(convert(int,convert(varchar(6),expected.date, 112)), reality.date) 
ORDER  BY expected.date DESC 

在重定向后显示的页面中 在$mail->AltBody = 'This is a plain-text message body'; //send the message, check for errors if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; }else{ // BUILD $form_data here $_SESSION['temp_form_data'] = serialize($form_data); and after redirect just get data from session. header('Location: http://localhost/appt/target.php'); }

target.php

另一种选择是将其存储在数据库中并在重定向后检索它。

我不建议将表单数据参数和值放在网址上。