Php表单-添加重定向以及选择加入框


Php form - Add redirect as well as a opt in box

我是这方面的新手,正在尝试如何为代码添加一些额外的功能。

  1. 提交时,页面需要重定向到外部URL,即www.google.com
  2. 复选框必须自动选中,当客户收到电子邮件时,其订阅时事通讯的详细信息必须返回TRUE/FALSE(这样客户就知道他们是否想选择加入)

这是我的PHP代码:

<?php
    // Enter the email where you want to receive the notification when someone subscribes
    $emailTo = 'nissberry@gmail.com';
    $subscriber_email = addslashes(trim($_POST['email']));
    if (!isEmail($subscriber_email)) {
        $array = array('valid' => 0, 'message' => 'Insert a valid email address!');
        echo json_encode($array);
    } else {
        $array = array('valid' => 1, 'message' => 'Thanks for your subscription!');
        echo json_encode($array);
        // Send email
        $subject = 'New Subscriber (Free eBook)!';
        $body = "You have a new subscriber!'n'nEmail: " . $subscriber_email;
        // uncomment this to set the From and Reply-To emails, then pass the $headers variable to the "mail" function below
        // $headers = "From: ".$subscriber_email." <" . $subscriber_email . ">" . "'r'n" . "Reply-To: " . $subscriber_email;
        mail($emailTo, $subject, $body);
    }
?>

这是我的HTML:

<form class="form-inline" role="form" action="assets/subscribe.php" method="post">                                              
    <div class="form-group">
        <label class="sr-only" for="subscribe-email">Email address</label>
        <input type="text" name="email" placeholder="Enter your email..." class="subscribe-email form-control" id="subscribe-email">
    </div>
    <button type="submit" class="btn">Receive your free eBook</button>
    <br>
    <div class="checkbox">
        <label>
            <input type="checkbox" class="checked"> Receive Our Monthly Newsletter
        </label>
    </div>                                             
</form>
<div class="success-message"></div>
<div class="error-message"></div>
  1. 如果在执行mail()函数之前没有输出(没有echo或html),则可以在关闭PHP标签之前使用这个:

    header("Location: www.google.com");
    exit;
    

    因此,要使用PHP重定向,您需要从代码中删除所有echo。如果无法做到这一点,请使用javascript:

    window.location.replace("www.google.com");
    
  2. 将"checked"属性添加到复选框中。它也应该有一个名字,因此您可以使用全局$_POST变量访问它:

    <input name="newsletter" type="checkbox" class="checked" checked>
    

    然后你可以扩展你的信息:

    $newsletter_subscription = isset($_POST['newsletter']) ? 'TRUE' : 'FALSE';
    $body .= 'Newsletter subscription: '.$newsletter_subscription;