首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >HTML PHP复选框

HTML PHP复选框
EN

Stack Overflow用户
提问于 2018-06-16 00:57:21
回答 3查看 74关注 0票数 0

我刚刚在我的联系人表单中添加了一个复选框,以指示用户是否愿意注册我的时事通讯。我希望我的PHP脚本输出复选框的值,当它给我发送一封电子邮件。我在互联网上彻底搜索了一遍,也找不到任何解决方案,只是有类似问题的人。https://joebaileyphotography.com/contact%20me.html

HTML:

<form method="post" name="myemailform" action="form-to-email.php">

<label class="label" for="fname">First Name</label>
<br>
<input type="text" id="fname" name="fname" placeholder="Your name..">
<br>
<label class="label" for="lname">Last Name</label>
<br>
<input type="text" id="lname" name="lname" placeholder="Your last name..">
<br>
<label class="label" for="Email">Email Address</label>
<br>
<input type="text" id="Email" name="Email" placeholder="Your email address...">
<br>
<label class="label" for="subject">Subject</label>
<br>
<textarea id="subject" name="subject" placeholder="Drop me a line..." style="height:200px"></textarea>
<br>
<input type="checkbox" id="subscribeNews" name="subscribe" value="newsletter" checked>
<label for="subscribeNews">Yes, I would like to recieve news from Joe Bailey Photography.</label>
<br>
<br>
<input type="checkbox" id="privacyPolicy" name="privacy" value="privacy">
<label for="privacyPolicy">I consent to having this website store my submitted information so they can respond to my inquiry. For more info, read the <a href="/privacy-policy.html">Privacy Policy.</a></label>
<br>
<br>
<div class="g-recaptcha" data-sitekey="6LeqeRkTAAAAAFGmVFmAorEU9n0yL4NDEpSUnM0R"></div>
<br>
<input type="submit" value="Send Form">

PHP:

<?php
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
    'secret' => '6LeqeRkTAAAAABjyRvL0c1vrqG3ZmV51O0-S3xcz',
    'response' => $_POST["g-recaptcha-response"]
);
$options = array(
    'http' => array (
        'method' => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
    echo "<p>Please go back and verify you are human.</p>";
} else if ($captcha_success->success==true) {

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $visitor_email = $_POST['Email'];
    $subject = $_POST['subject'];

    $email_subject = "Joe Bailey Photography Contact Form";
    $email_body = "You have received a new message from $fname $lname.\n".
    "Here is the message:\n$subject
    ";

    $to = "enquiries@joebaileyphotography.com";
    $headers = "From: $visitor_email \r\n";
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: Contact%20Me%20Thank%20You.html#thankyou');
    function IsInjected($str)
    {
    $injections = array('(\n+)',
          '(\r+)',
          '(\t+)',
          '(%0A+)',
          '(%0D+)',
          '(%08+)',
          '(%09+)'
          );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str))
    {
        return true;
    }
        else
    {
        return false;
    }
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-06-16 04:02:22

已修复以下代码的问题:

<?php
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
    'secret' => '6LeqeRkTAAAAABjyRvL0c1vrqG3ZmV51O0-S3xcz',
    'response' => $_POST["g-recaptcha-response"]
);
$options = array(
    'http' => array (
        'method' => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
    echo "<p>Please go back and verify you are human.</p>";
} else if ($captcha_success->success==true) {

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $visitor_email = $_POST['Email'];
    $subject = $_POST['subject'];
    $subscribe = $_POST['subscribe'];

    $email_subject = "Joe Bailey Photography Contact Form";
    $email_body = "You have received a new message from $fname $lname.\n".
    "Here is the message:\n$subject
    ".
    "$fname wishes to subscribe to the Newsletter: $subscribe --\n";

    if (isset($_POST['subscribe'])){
        $subscribe= $_POST['subscribe'];
    }

    $to = "enquiries@joebaileyphotography.com";
    $headers = "From: $visitor_email \r\n";
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: Contact%20Me%20Thank%20You.html#thankyou');
    function IsInjected($str)
    {
    $injections = array('(\n+)',
          '(\r+)',
          '(\t+)',
          '(%0A+)',
          '(%0D+)',
          '(%08+)',
          '(%09+)'
          );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str))
    {
        return true;
    }
        else
    {
        return false;
    }
    }
}
?> 

按如下方式输出电子邮件:您已收到来自Joe Bailey的新消息。这是一条消息: test Joe愿意订阅时事通讯:是--或者您收到了Joe Bailey发来的新消息。以下是消息: test Joe希望订阅时事通讯:--

还将HTML元素#subscribeNews的值更改为"Yes“

乱七八糟,但很管用。

票数 0
EN

Stack Overflow用户

发布于 2018-06-16 01:09:50

要理解从表单接收到的内容,可以使用var dump它。

var_dump($_POST);

您将看到,当复选框被勾选时,数组中就有了密钥privacy。因此您可以使用if条件,例如

if (array_key_exists('privacy', $_POST) && !empty($_POST['privacy'])) {
    // send email
}
票数 0
EN

Stack Overflow用户

发布于 2018-06-16 01:09:58

只需使用

if (isset($_POST['subscribe'])){
$subscribe= $_POST['subscribe'];
}

$email_subject = "Joe Bailey Photography Contact Form";
$email_body = "You have received a new message from $fname $lname.\n".
"Here is the message:\n$subject
".
"$fname wishes to subscribe to the Newsletter: $subscribe --\n";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50879878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档