关于PHP,我还是个新手,但我想为我的大学俱乐部创建一种形式。但是,尽管运行正常,但实际上不会发送电子邮件。该代码应检查名称和电子邮件是否有效。然后,如果两者都正确,则代码将继续发送电子邮件。
<?php
print_r($_POST);
// define variables and set to empty values
$name_error = $email_error = "";
$name = $email = $phone = $job = $tools = $range = $smolioli = $exp = $concern = "";
//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$name_error = "Name is Required!";
} else {
$name = test_input($_POST["name"]);
//check if name only contains letters and spaces
if (!preg_match("/^[a-zA-Z ]*$/",$name)){
$name_error = "Only letters and spaces are allowed!";
}
}
if (empty($_POST["email"])) {
$name_error = "Email is Required!";
} else {
$name = test_input($_POST["email"]);
//check if name only contains letters and spaces
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "Invalid email =(";
}
}
if ($name_error == "" and $email_error == ""){
$phone = $_POST['phone'];
$job = $_POST['job'];
$powertool = $_POST['powertool'];
$range = $_POST['range'];
$smolioli = $_POST['smolioli'];
$dance = $_POST['dance'];
$exp = $_POST['exp'];
$concern = $_POST['concern'];
$mailTo = "MY EMAIL HERE";
$headers = "From: Application Page";
$subject = $name." has sent an application!";
if($job == "voice"){
$job = "Singer/Dancer";
$txt = "Name: ".$name."\nEmail: ".$email."\nJob: ".$job."\nVocal Range: ".$range."\nOkay witha small role: ".$smolioli."\nDancing Ability: ".$dance." out of 10\n\nExperience: ".$exp."\n\nQuestions and or Concerns: ".$concern;
} elseif ($job == "crew") {
$job = "Crew"
$txt = "Name: ".$name."\nEmail: ".$email."\nJob: ".$job."\nPowertool Comfort Lvl: ".$powertool." out of 10\n\nExperience: ".$exp."\n\nQuestions and or Concerns: ".$concern;
} else {
$job = "Singer/Dancer or Crew"
$txt = "Name: ".$name."\nEmail: ".$email."\nJob: ".$job."\nPowertool Comfort Lvl: ".$powertool." out of 10\nVocal Range: ".$range."\nOkay witha small role: ".$smolioli."\nDancing Ability: ".$dance." out of 10\n\nExperience: ".$exp."\n\nQuestions and or Concerns: ".$concern;
}
mail($mailTo, $subject, $txt, $headers);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>