简体   繁体   English

如何为我的联系表创建确认页面?

[英]How do I create a confirmation page for my contact form?

So, I have a contact form, and I want to create a confirmation page for it before the form is submit. 因此,我有一个联系表单,我想在提交表单之前为其创建一个确认页面。 And I wanna send an email with the form informations after the user enter their information, and then go to the confirmation page and then click confirm. 在用户输入信息后,我想发送一封包含表单信息的电子邮件,然后转到确认页面,然后单击“确认”。 I have the contact form and confirmation page, it's just that, the form was already submit and send email before it got to the confirmation page. 我有联系表单和确认页面,仅此而已,表单已经提交并发送电子邮件,然后才到达确认页面。 I don't want it to send the email until the users check and confirm their information in the confirmation page. 在用户检查并在确认页面中确认其信息之前,我不希望它发送电子邮件。

This is the contact form: 这是联系表格:

<?php include('report_process.php'); ?>
<link rel="stylesheet" href="report.css" type = "text/css">

<div class="container">
    <form id = "contact" action="confirm.php" method="post" enctype="multipart/form-data">
    <label for="name">Your Name</label>
    <input type="text" id="name" name="name" value = "<?= $name ?>" placeholder="Name">
    <span class = "error"><?= $name_error ?></span>


    <label for="email">Your E-mail</label>
    <input type="email" id="email" name="email" value = "<?= $email ?>" placeholder="Email">
    <span class = "error"><?= $email_error ?></span>


    <label for="phone">Your phone number</label>
    <input type = "tel" id = "phone" name = "phone" value = "<?= $phone ?>" placeholder = "Phone">
    <span class = "error"><?= $phone_error ?></span>

    <fieldset>
    <label for="address">Address of the Incident</label>
    <input type = "text" id = "address" name = "address" value = "<?= $address ?>" placeholder = "Address">
    <select name = "state" id = "state" value = "<? $state ?>"> 
      <option value = "state">States</option>
      <option>-----------</option>
    <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>
<input type="number" placeholder = "Zip Codes" id = "zip" name="zip" min = "1" max = "1000000" value = "<?= $zip ?>" >              
</fieldset>


    <label for="date">Date</label>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#datepicker" ).datepicker();
  } );
  </script>
</head>
<body>
<input placeholder = "Date" type="text" id = "date" name = "date" value = "<?= $date ?>" size="30">

<body>
<label for="times">Time</label>
<fieldset>
<input type="number" placeholder = "Hours" id = "hours" name="hours" min = "1" max = "12" value = "<?= $hours ?>" >
<input type="number" placeholder = "Minutes" name="minutes" id = "minutes" min = "00" max = "59" value = "<?= $minutes ?>">

<select name = "time" id = "time" value = "<? $time ?>">
  <option value = "am" name = "am"> AM </option>
  <option value = "pm" name = "pm"> PM </option>
</select>
<label>Select image to upload: </label>
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="file" name="fileToUpload[]" id="fileToUpload[]" multiple = "">
</fieldset>

    <label for="message">Message</label>

    <textarea id="message" name="message" placeholder="Please enter your message here" required></textarea>

  <button type="submit" name = "send" id = "send">Send Message</button>
</body>

</div>
</form>

This is my confirmation page, I wanna display the informations that the users put in the contact form here for double checking and confirm. 这是我的确认页面,我想在此显示用户在联系表中输入的信息,以供再次检查和确认。

<?php include('report_process.php'); ?>
<link rel="stylesheet" href="report.css" type = "text/css">

<div class="container">  
  <form id="contact" action="confirm.php" method="post">
    <h3>Confirmation</h3>
    <h4>Please confirm the information you enter below</h4>

    <label> Name: </label>
    <fieldset>
        <?php if(isset($_POST['send'])){
          echo $_POST['name'];
        }
          ?>
    </fieldset>

    <label> Email: </label>
    <fieldset>
    <?php if(isset($_POST['send'])){
          echo $_POST['email'];
        }
          ?>
    </fieldset>

    <label> Phone: </label>
    <fieldset>
    <?php if(isset($_POST['send'])){
          echo $_POST['phone'];
        }
          ?>
    </fieldset>
    <label> Phone: </label>
    <fieldset>
    <?php if(isset($_POST['send'])){
          echo $_POST['address'];
        }
          ?>
    </fieldset>

 <label> Date: </label>
 <fieldset>
 <?php if(isset($_POST['send'])){
          echo $_POST['date'];
        }
          ?>
</fieldset>

<label> Time: </label>
<fieldset>
<?php if(isset($_POST['send'])){
          echo $_POST['time'];
        }
          ?>
</fieldset>

<label> Files 1: </label>
<fieldset>
<?php if(isset($_POST['send'])){
          echo $_POST['fileToUpload'];
        }
          ?>
</fieldset>

<label> Files 2: </label>
<fieldset>
<?php if(isset($_POST['send'])){
          echo $_POST['fileToUpload[]'];
        }
          ?>
</fieldset>

    <label> Message: </label>
    <fieldset>
       <?php if(isset($_POST['send'])){
          echo $_POST['message'];
        }
          ?>
    </fieldset>
    <fieldset>
      <button name="confirm" type="submit" id="confirm" >Confirm</button>
    </fieldset>
    <div class = "success"><?= $success; ?></div>
  </form>


</div>

And this is my contact process file, where I send the email and do the error checking. 这是我的联系流程文件,我在其中发送电子邮件并进行错误检查。

if($_SERVER["REQUEST_METHOD"] == "POST" ){

if(empty($_POST["email"])){
    $email_error = "Email is required";
} else{
    $email = test_input($_POST["email"]);
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $email_error = "Invalid email format";
    }
}

if(empty($_POST["phone"])){
    $phone_error = "Phone number is required";
} else{
    $phone = test_input($_POST["phone"]);
    if(!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i", $phone)){
        $phone_error = "Invalid Phone number";
    }
}

if(!empty($_POST)){

    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    try{
        $db = mysqli_connect($servername, $username, $password, $dbname);
    }catch (Exception $e){
        $error = $e->getMessage();
        echo $error;
    }
    $sql = 'INSERT INTO cases(name, email, address, phone, `case`) VALUES(?,?,?,?,?)';
    try{
        $stmt = $db->prepare($sql);
        $stmt->bind_param('sssss',$_POST['name'],$_POST['email'],$_POST['address'],$_POST['phone'],$_POST['message']);
        $stmt->execute();
    } catch(Exception $e) {
        var_dump($e);
    }
}
if(empty($_POST["name"])){
    $name_error = "Name is required";
} else{
    $name = test_input($_POST["name"]);
    if(!preg_match("/^[a-zA-Z ]*$/", $name)){
        $name_error = "Only letters and white spaces are allowed";
    }
}

if(empty($_POST["message"])){
    $message = "";
}else{
    $message = test_input($_POST["message"]);
}

if($name_error == '' and $email_error =='' and $phone_error == ''){
    $message_body = '';
    unset($_POST['confirm']);
    foreach($_POST as $key => $value){
        $message_body .= "$key: $value\n";
    }
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
  ->setUsername(EMAIL)
  ->setPassword(PASSWORD)
;

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// Create a message
$messagee = (new Swift_Message('Test'))
  ->setFrom(EMAIL)
  ->setTo($email)
  ->setBody($message_body)
  ;

// Send the message
$result = $mailer->send($messagee);
if($mailer->send($messagee)){
    $success = "Message have been sent, we will be right back with you. Thank You.";
    $name = $email = $phone = $date = $hours = $minutes = $time = $times = $message = '';
}
}
}

在联系表单页面的操作中,您可以将表单数据保存在会话变量中,并可以在确认页面上显示数据,然后可以使用会话中存储的相同数据发送电子邮件

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

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