我正在尝试用php制作一个函数表单。起初,我制作了一个"index.html"文件和另一个名为send.php的文件。但它不起作用。我还尝试在send.php中插入"echo",结果成功了!因此,我将index.html转换为index.php,并将php代码放在其中,然后放在body结束标记(标记)之前,但它不起作用!所以我意识到错误在代码中,但我从互联网上复制了这些行,我对php不是很了解。有人能帮我吗?这是代码:
我检查了字段的name="是否正确。
<?php
$name = ($_POST['name'];
$mail = ($_POST['mail'];
$budjet = ($_POST['budjet'];
$society = ($_POST['society'];
$message = ($_POST['message'];
$error = "Message sending failed";
$errorMessage = "Sorry your message can not be sent.";
//Validate first
if(empty($name)||empty($mail)||empty($budjet)||empty($society)||empty($message))
{
echo "Please fill al the fields";
header('Location: index.html');
}
//validate against any email injection attempts
if(IsInjected($email))
{
echo "Bad email value";
header('Location: index.html');
}
$msg = "Name : $name 'r'n";
$msg = "Mail : $mail 'r'n";
$msg = "Budjet: $budjet 'r'n";
$msg = "Society: $society 'r'n";
$msg = "Message : ".stripslashes($_POST['message'])."'r'n'n";
$msg = "User information 'r'n";
$msg = "User IP : ".$_SERVER["REMOTE_ADDR"]."'r'n";
$msg = "Browser info : ".$_SERVER["HTTP_USER_AGENT"]."'r'n";
$msg = "User come from : ".$_SERVER["SERVER_NAME"];
$recipient = "myemail@mail.com"; // Change the recipient email adress to your adrees
$sujet = "Mail da Contact di Hindeto";
$mailheaders = "From: $email'r'nReply-To: $email'r'nReturn-Path: $email'r'n";
if (!$error){
$sending = mail($recipient, $sujet, $msg, $mailheaders);
if ($sending) {
// If the message is sent we output a string to use it
echo "Sent";
} else {
// Display Error Message
echo $errorMessage;
}
} else {
echo $error; // Display Error Message
}
// Function to validate against any email injection attempts
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;
}
}
?>
在$_POST
之前删除(
的前5行出现语法错误
$name = $_POST['name'];
$mail = $_POST['mail'];
$budjet = $_POST['budjet'];
$society = $_POST['society'];
$message = $_POST['message'];
以及之后header('Location: index.html');
和header('Location: index.html');
打印exit();
header('Location: index.html');
exit();
除了Jetawe,您还必须在验证语句中使用isset()。
$name = ((!isset($_POST['name']) || empty($_POST['name'])) ? null:$_POST['name']);