Submit Form Redirect Back to index.html

拟墨画扇 提交于 2019-12-12 23:39:45

问题


I'm creating a website for a friend, my first website. Currently have it up and hosted too.

Created a basic "Contact Me" form, where a user can fill out information, then it will send an email.

The question is: upon clicking submit, how do I get the webpage to redirect back to the index.html after a "Your form has been submitted" response from php.

Here is html form section:

<div class="contactme" id="contactme">  
        <form class="form-horizontal" method="post" name="myemailform" action="form-to-email.php">
          <fieldset>
            <h2>Let's Create Together</h2>
            <hr class="small">
            <div class="form-group">
              <label class="col-lg-2 control-label">Name</label>
              <div class="col-lg-10">
                <input type="text" class="form-control" name="name" placeholder="Name">
              </div>
            </div>
            <div class="form-group">
              <label class="col-lg-2 control-label">E-Mail</label>
              <div class="col-lg-10">
                <input type="email" class="form-control" name="visitor_email" placeholder="E-Mail">
              </div>
            </div>
            <div class="form-group">
              <label class="col-lg-2 control-label">Message</label>
              <div class="col-lg-10">
                <textarea class="form-control" rows="8" name="message"></textarea>
              </div>
            </div>
            <div class="form-group">
              <div class="col-lg-10 col-lg-offset-2">
                <button type="reset" class="btn btn-default">Cancel</button>
                <button type="submit" name="submit" id="submitbutton" class="btn btn-primary mainButton">Submit</button>
              </div>
            </div>
          </fieldset>
        </form>
</div>

And here is the separate .php code. This is on a different file.

<?php
if (!isset ($_POST ['submit']))
{
    //This page should not be accessed directly. Need to submist the form. 
    echo "error, you need to submit the form!";
}

$name = $_POST ['name'];
$visitor_email = $_POST ['visitor_email'];
$message = $_POST ['message'];

//Validation

if(empty ($name)||empty ($visitor_email))
{
    echo "Name and email are mandatory!";
    exit();
}

else {
    echo "Submit Successful!";
    header("Location: index.html");
    exit(); 
}


$email_from = 'info@joshkelley.video'; //<== Put your email address here
$email_subject = "New Form Submission";
$email_body = "You have received a new message from the user $name.\n".
    "E-mail Address: $visitor_email\n".
    "Here is the message: $message\n".

$to = "brandontetrick@gmail.com"; // <==Put your email address here
$headers = "From: $email_from \r\n"; 

//Send the email! 

mail($to,$email_subject,$email_body,$headers);
//done
?>

Any help or tips out be great. Thanks!


回答1:


The header() function will not work if you output ANYTHING even a space before it. But you could store your success message to a session variable.

First, you will have to rename your index.html into index.php

Add a session_start() function to the very top (again, never outpuyt anything before) in index.php and in your form script (anywhere where you work with $_SESSION variables):

<?php
session_start();
//....

Then change

echo "Submit Successful!";
header("Location: index.html");
exit(); 

for this

$_SESSION['success_msg'] =  "Submit Successful!"; 
header("Location: index.html");
exit(); 

And in your index.php

<?php
session_start();

if(isset($_SESSION['success_msg'])) {
    echo $_SESSION['success_msg'];
    unset($_SESSION['success_msg']); // unset this after outputting, else it will keep showing
}


来源:https://stackoverflow.com/questions/43428332/submit-form-redirect-back-to-index-html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!