php - 帮我制作一个简单的时事通讯订阅表格吗?

标签 php forms email webforms newsletter

我正在处理的时事通讯订阅表格可以在页脚中找到:this link.

目前,提交表单后,错误/成功消息会显示在新页面上。

相反,我希望错误/成功消息直接显示在页脚中的表单下方。

IE。如果用户输入电子邮件地址[email protected] ,文本“成功”应显示在输入字段下方,而不是显示在新页面上。

非常感谢您的帮助!

这是我现在得到的:

在标题中

<script>
function submitEmail() {

    var email = $('#emailText').val();

    jQuery.post('/common/php/send_news.php', {
        email: email
    },function(data){
        $('#submissionResponse').html(data);
    },html);

}
</script>

表格

<form method="post">
        <fieldset>
        <h3></h3>
        <input type="text" name="email" maxlength="80" class="input" value="enter your email address..." onfocus="this.className=('input_active')" onblur="this.className=('input')" onclick="this.value='';" />
        <input type="button" name="submit"  value="" class="subscribe_btn" onmouseover="this.className=('subscribe_btn_over')" onmouseout="this.className=('subscribe_btn')" onclick="submitEmail();" />
        <small>*we will not share your email address</small><span class="clear"></span>
        </fieldset>
      </form>
      <div id="submissionResponse"></div>

send_news.php

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

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="116574626551747c70787d657462653f727e7c" rel="noreferrer noopener nofollow">[email protected]</a>";
    $email_subject = "Newsletter Subscription";


    function died($error) {
        // your error code can go here
        echo "There was an error with your submission";
        echo $error."<br /><br />";
        die();
    }

    // validation expected data exists
    if(
        !isset($_POST['email'])) {
        died('There was an error with your submission');       
    }

    $email_from = $_POST['email']; // required


    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Email: ".clean_string($email_from)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

Thanks for signup up for the Newsletter!

<?php
}
?>

最佳答案

我要做的是设置一个简单的页面来回显成功或失败,然后使用 Ajax 调用将信息从表单传递到该页面 - 您可以使用 jQuery 来实现此目标。

将其添加到 javascript 文件或脚本标记的 header 中:

function submitEmail() {

    var email = $('#emailText').val();

    jQuery.post('/submission.php', {
        email: email
    },function(data){
        $('#submissionResponse').html(data);
    },html);

}

然后更改表单,使提交按钮如下所示:

<input type="button" name="submit" value="Submit" onclick="submitEmail();" />

然后在表单下方添加一个 ID 为“submissionResponse”的 div,如下所示:

<div id="submissionResponse"></div>

这样您就可以像更改页面一样处理帖子,但您将自动提交数据并获取响应。


另一个编辑:

当我这样做时,我不妨提供另一种方法来做类似的事情。您可以不使用 Ajax,而是按照现在的方式进行操作,但指示表单重新加载当前页面 ( action="<?php echo $_SERVER['PHP_SELF']; ?>" ),然后在此页面的顶部添加

if(isset($_POST['email'])){
    include('submission.php');
}

表格下方有:

<div id="formStatus"><?php echo isset($formStatusResponse) ? $formStatusResponse : null; ?></div>

实现同一目标的两种方法。使用 Ajax 速度更快,因为它只需要最终用户加载一行文本,而第二种方法需要完全重新加载整个页面。

关于php - 帮我制作一个简单的时事通讯订阅表格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6688120/

相关文章:

php - 我可以在 iPad 2 上编程吗?

php - PHP 中的 BSE/NSE 报价

php - 如何使用 PHP 只上传 HTML 表单的特定部分?

html - 如果我不提交数据,是否应该使用 FORM 元素?适当的 (X)HTML 标记

validation - 互联网电子邮件的最大消息ID长度

html - 如何制作一个只是链接而不是 iFrame 的 Facebook "Like"按钮?

php - 如何使用返回值的存储过程从MySQL返回总记录数

php - CSS:如何在CSS中的每个h3之前显示图像图标?

C#:替换 ListView 中空白项的文本?

email - 发送电子邮件时出现 VBScript 错误 '80040211'