PHP mail()中的空$ _POST [英] empty $_POST in PHP mail()

查看:47
本文介绍了PHP mail()中的空$ _POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下方面的一些帮助将不胜感激!我一直在搜索网络和stackoverflow数小时.我的php邮件功能存在此问题.

Some help with the following would be greatly appreciated! I've been searching the web and stackoverflow for hours. There is this problem with my php mail function.

我的网站上有一个表格(该网站使用wordpress),其中包含两个文本字段,名称和电话号码.表单中还有一个隐藏字段,用于显示当前网址,因此我们可以看到表单的填写页面.此信息也存储在php会话中.

There's a form on my website (the site uses wordpress) with two text fields, name and phone number. There's also a hidden field in the form that displays the current url, so we can see on what page the form was filled in. This information is also stored in a php session.

<?php session_start();
$_SESSION['url'] = $_SERVER['REQUEST_URI']; 
?> 

<form name="callback" action="callme.php" method="post" onsubmit="return(validate());">
<label>Name:</label><input type="text" name="name" size=15 />
<label>Tel:</label><input class="sky" type="text" name="tel" size=15 />
<div id="afterfive"><p>Call after five<input type="checkbox" name="afterfive" value="Call back after five"></p></div>
<input type="hidden" name="url" value="<?php echo $_SESSION['url']; ?>">
<input type="submit" class="classname" value="Call me back!" title="Call me back!" />
</form>

<script>
function validate()
{
if( document.callme.name.value == "" )
{
 alert( "Please fill in your name" );
 document.callme.name.focus() ;
 return false;
 }
if( document.callme.tel.value == "" )
{
alert( "Please fill in your phone number" );
document.callme.tel.focus() ;
return false;
}
}
</script>

以下php代码为callme.php:

The following php code is callme.php:

<?php session_start();

$name = $_POST['name'];
$tel = $_POST['tel'];
$afterfive = $_POST['afterfive']; 
$url = $_POST['url'];

$to = "me@myemail.com";
$subject = "Please call back $name";
$message .= "Hi, the following person would like to be called back: \n";
$message .= "Name: $name \n";
$message .= "Phonenumber: $tel \n";
$message .= "$afterfive \n";
$message .= "This message was send from this page: $url \n";
$headers = "From: meagain@myemail.com" . "\r\n";
$headers .= "BCC: meagain@myemail.com" . "\r\n";

if(mail($to, $subject, $message, $headers)){
$_SESSION['name'] = $_POST['name'];
$_SESSION['tel'] = $_POST['tel'];
header("Location: http://www.mywebsite.thankyou");
}

?>

提交表单后,访问者将重定向到我们的谢谢"页面,并有机会使用第二个表单填写其他信息.以前存储在php会话中的信息(表单字段名称,tel和url)将添加到隐藏的表单字段中.

After submitting the form, the visitor is redirected to our thankyou page and given the opportunity the fill in additional information using a second form. The information previously stored in the php session (form fields name, tel and url) are added in hidden form fields.

大多数情况下,这一切都可以正常工作,但是有时我们会收到所有或部分字段为空的电子邮件.当然,这可能是禁用了JavaScript的用户或提交了空白表格的Google机器人,但奇怪的是,有时甚至url字段都是空的(该表格在我们的主页上不可见). $ _SERVER ['REQUEST_URI']不应该始终正常工作吗?

This all works fine most of the time, but sometimes we receive e-mails with all or some fields empty. Of course this could be users with javascript disabled or google bots that submit blank forms, but the weird thing is that sometimes even the url field is empty (the form is not visible on our homepage). Shouldn't $_SERVER['REQUEST_URI'] always still work?

我当时正在考虑添加php表单验证,但是我不确定这是否可以解决问题.这可能与用于wordpress的超高速缓存插件有关吗?还是可能与php会话有关?

I was thinking about adding php form validation, but I'm not sure this will solve the problem. Could this have something to do with the hyper cache plug-in for wordpress? Or could it be related to the php session?

推荐答案

",但有时我们会收到所有或部分字段为空的电子邮件"

您应该使用服务器端方法而不是JS之类的

You should be using a server-side method instead of JS such as

if(empty($_POST['name'])){ die("You need to enter your name."); 

( JS 始终可以由用户禁用,这是电子邮件/字段为空的可能原因)

(JS can always be disabled by the user, one probable cause for empty emails/fields)

,这将确保您希望不为空的字段被填充.与 Andrewsi 一起,在顶部使用if(isset($_POST['submit'])){的处理程序,然后将您的提交按钮命名为name="submit",这样,如果直接访问callme.php,则在未单击提交按钮的情况下将不会处理信息.

and that will ensure that the fields you wish to be NOT empty, be filled. In conjunction with what Andrewsi mentioned, use if(isset($_POST['submit'])){ at the top of your handler, and name your submit button to name="submit" that way the callme.php if accessed directly, won't process the information without the submit button being clicked.

注意:还有很多其他方法可以实现这一目标,但这是一种基本而有效的方法.

Note: There are many other ways to achieve this, but this is a basic yet effective method.

命名您的提交按钮,例如:

Naming your submit button such as:

<input type="submit" name="submit" value="Submit">

在您的情况下,应该是:

in your case, it would be:

<input type="submit" name="submit" class="classname" value="Call me back!" title="Call me back!" />

PHP处理程序

<?php 

session_start();

if(isset($_POST['submit'])){

    if(empty($_POST['name'])){ die("You need to enter your name."); }
    if(empty($_POST['tel'])){ die("You need to enter your telephone number."); }
    if(empty($_POST['afterfive'])){ die("You need to fill this field."); }
    if(empty($_POST['url'])){ die("You need to fill this field."); }

    $name = $_POST['name'];
    $tel = $_POST['tel'];
    $afterfive = $_POST['afterfive']; 
    $url = $_POST['url'];

    $to = "me@myemail.com";
    $subject = "Please call back $name";
    $message .= "Hi, the following person would like to be called back: \n";
    $message .= "Name: $name \n";
    $message .= "Phonenumber: $tel \n";
    $message .= "$afterfive \n";
    $message .= "This message was send from this page: $url \n";
    $headers = "From: meagain@myemail.com" . "\r\n";
    $headers .= "BCC: meagain@myemail.com" . "\r\n";

    if(mail($to, $subject, $message, $headers)){
        $_SESSION['name'] = $_POST['name'];
        $_SESSION['tel'] = $_POST['tel'];
        header("Location: http://www.mywebsite.thankyou");
    }

}

// You could use this at end also to show a message
// if callme.php is accessed directly.
// else {echo "You cannot do that from here.";exit;}

?>

这篇关于PHP mail()中的空$ _POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆