简体   繁体   English

使用php重置表单的问题

[英]problems resetting a form using php

I have a contact form which sends an email once submitted and displays an alert successfully when sent.我有一个联系表格,它在提交后发送一封电子邮件,并在发送时成功显示警报。 My problem was 1) after its sent the email form still has content.我的问题是 1) 发送电子邮件表单后仍然有内容。 2) If I refresh the page the email is sent again with the previous details. 2) 如果我刷新页面,则会再次发送包含先前详细信息的电子邮件。 In order to solve this I used the below code in php which is doing the job of refreshing with out sending an email.为了解决这个问题,我在 php 中使用了以下代码,它在不发送电子邮件的情况下完成刷新工作。

 header("Location: contact.php"); exit;

Now my problem is The alert is not displayed anymore which is true because the alert is in my HTML and header refreshes the page and it never reaches the alert in HTML(displayed using php echo).现在我的问题是警报不再显示,这是真的,因为警报在我的 HTML 中,标题刷新页面并且它永远不会到达 HTML 中的警报(使用 php echo 显示)。 Here is the HTML Code :这是 HTML 代码:

 <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3" id="emailForm"> <h1 class="center">Email Form</h1> <?php echo $result; //header( "Location: contact.php"); //exit; ?> <p>Please get in touch ! I will get back to you as soon as I can !!</p> <form method="post" id="contactForm"> <div class="form-group"> <label for="name">Your Name</label> <input type="text" name="name" class="form-control" placeholder="Your Name" value="<?php echo $_POST['name'] ?>" /> </div> <div class="form-group"> <label for="email">Your Email</label> <input type="email" name="email" class="form-control" placeholder="Your Email" value="<?php echo $_POST['email'] ?>" /> </div> <div class="form-group"> <label for="message">Message</label> <textarea class="form-control" name="message"> <?php echo $_POST[ 'message'] ?> </textarea> </div> <input type="submit" name="submit" id="send" class="btn btn-success btn-lg center" value="Submit" /> </form> </div> </div> </div>

Here is the PHP code:这是PHP代码:

<?php

if($_POST['submit']) {

    if(!$_POST['name']){

        $error.="<br>Please enter your name";
    }
    if(!$_POST['email']){

        $error.="<br>Please enter your email address";
    }
    if(!$_POST['message']){

        $error.="<br>Please enter a message";
    }

    if ($_POST['email']!= "" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

        $error.="<br>Please enter a valid email address";
    } 

    if( $error )
    {
        $result='<div class="alert alert-danger"><strong>There were error(s):'.$error.'</strong></div>';
    }

    else {

        if(mail("madistackcloud@gmail.com","Message from Deepak", "Name: ".$_POST['name']."Email: ".$_POST['email']."Message: ".$_POST['message'])) {

            $result='<div class="alert alert-success alert-dismissible"><strong>Thank you! I\'ll be in touch.</strong></div>';   


        }

        else
        {
            $result='<div class="alert alert-danger alert-dismissible"><strong>Oops! Something went wrong. Please try again !</strong></div>';
        }

    }
}

?> ?>

Is there any workaround for this or any other method which can do the job.是否有任何解决方法可以解决此问题或任何其他方法可以完成这项工作。 Kindly help.请帮忙。

Change your form to将您的表格更改为

  <form method="post" id="contactForm">
    <div class="form-group">
      <label for="name">Your Name</label>
      <input type="text" name="name" class="form-control" placeholder="Your Name" value="" />
    </div>

    <div class="form-group">
      <label for="email">Your Email</label>
      <input type="email" name="email" class="form-control" placeholder="Your Email" value="" />
    </div>

    <div class="form-group">
      <label for="message">Message</label>
      <textarea class="form-control" name="message"></textarea>
    </div>
    <input type="submit" name="submit" id="send" class="btn btn-success btn-lg center" value="Submit" />
  </form>

If you don't want to show the content sent, dont use echo $POST on the value of the inputs.如果您不想显示发送的内容,请不要对输入的值使用echo $POST

You're always echoing the post data in the html section.您总是在 html 部分回显帖子数据。 You only want to echo data if there is an error.如果出现错误,您只想回显数据。

value="<?php echo $_POST['name'] ?>"

in the control code above do something like在上面的控制代码中做类似的事情

$name = cleanInput($_POST['name']);
$email = cleanInput($_POST['name']);
// etc...

Then in your然后在你的

if( $error )
...
else
{
    $name = "";
    $email = "";

    sendMail();
    // etc...

and then back in your html:然后回到你的html:

value="<?php echo $name; ?>"

为什么要这样做: value="<?php echo $_POST['email'] ?这将输入字段的值设置为用户输入的值。这意味着下次加载页面时,该值是可见的,因此将发送时通过电子邮件发送。

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

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