Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在表单上执行php脚本后显示成功消息_Javascript_Php_Html_Css - Fatal编程技术网

Javascript 如何在表单上执行php脚本后显示成功消息

Javascript 如何在表单上执行php脚本后显示成功消息,javascript,php,html,css,Javascript,Php,Html,Css,所以我想标题说明了一切,我有一个小php脚本连接到一个输入表单。我想要的只是一个成功页面,在用户提交电子邮件后,该页面会向用户显示一个链接。这应该很容易,但我对php非常陌生 无论如何,下面是php和html代码: <?php $visitor_email = $_POST['email']; $email_from = "LightDesigns"; $email_subject = "Newsletter group"; $email_body = "You ha

所以我想标题说明了一切,我有一个小php脚本连接到一个输入表单。我想要的只是一个成功页面,在用户提交电子邮件后,该页面会向用户显示一个链接。这应该很容易,但我对php非常陌生

无论如何,下面是php和html代码:

 <?php

  $visitor_email = $_POST['email'];

  $email_from = "LightDesigns";
  $email_subject = "Newsletter group";
  $email_body = "You have received a new person to add to the newsletter: 
  $visitor_email.\n".

  $to = "stefanvujic576@gmail.com";
  $headers = "From: $email_from \r\n";
  $headers .= "Reply-To: $visitor_email \r\n";
  mail($to,$email_subject,$email_body,$headers);
 ?>


 <section id="newsletter" style="margin-bottom: 0px;">
  <div class="container">
    <h1>Subscribe To My Newsletter</h1>

    <form action="email.php" method="POST" name="newsletterForm">
      <input class="emailBox_1" type="email" placeholder="Enter Email..." 
      name="email">
      <button class="button_1" type="submit" class="button_1">
      <span>Subscribe</span></button>
    </form>
 </div>

订阅我的时事通讯
订阅

您只需使用mail()结果并向视图中添加IF语句即可。另外,请使用isset(),这样邮件不会在打开页面时触发,而只会在提交电子邮件时触发

<?php
    if (isset($_POST['email'])){
      $visitor_email = $_POST['email'];

      $email_from = "LightDesigns";
      $email_subject = "Newsletter group";
      $email_body = "You have received a new person to add to the newsletter: 
      $visitor_email.\n".

      $to = "stefanvujic576@gmail.com";
      $headers = "From: $email_from \r\n";
      $headers .= "Reply-To: $visitor_email \r\n";
      $result = mail($to,$email_subject,$email_body,$headers);
      // if ($result){
      // Redirect to success
      // header("location:success.php");
      // exit();
      // }
    }
?>


 <section id="newsletter" style="margin-bottom: 0px;">
  <div class="container">
    <h1>Subscribe To My Newsletter</h1>
    <?php
    if ($result){
        // Inline success
        echo "Success";
        // or
        // Include success
        // include("success.php");
        // or
        // redirect to success
        // echo "<meta http-equiv=\"refresh\" content=\"0;URL='success.php'\" />";  
    }else{
        ?>
            <form action="email.php" method="POST" name="newsletterForm">
              <input class="emailBox_1" type="email" placeholder="Enter Email..." 
              name="email">
              <button class="button_1" type="submit" class="button_1">
              <span>Subscribe</span></button>
            </form>
        <?php
    }
    ?>
 </div>

订阅我的时事通讯

我相信你可以创建一个新的html文档,你的成功页面,然后在你的邮件()之后使用类似的东西来回送整个内容;行(将您的成功html复制到其中,然后将echo代码复制到php中)。这为您做了两件事:它允许自定义成功页面,并在用户点击后退按钮时防止双重提交。谢谢,这非常有效,不过,如何指定在表单处理时显示的html页面。我想我的问题是:我如何在if语句中引用html页面,而不只是重复成功?对不起,我很痛苦,没问题。我用一些建议编辑了我的脚本。别忘了接受答案;-)。如果使用header()重新定位,请确保在重定向之前没有输出任何HTML,否则会导致header已发送错误。您也可以在include()函数中使用.HTML。(只是说)谢谢,我是帕特里克。当我单击箭头时,它表示已记录答案,当我达到最低代表点数时,答案将被接受。看来我现在没有足够的代表分数。