0
필자는 필수 입력란을 만들려고하는 PHP 양식을 가지고 있지만 제대로 작동하지 않는 것처럼 보일 수 있습니다. 나는 단지 $ name $ bank $ email과 $ phone을 요구하는 폼을 만들 필요가있다.PHP 폼 유효성 검사 필수 입력란
<?php
/* Set e-mail recipient */
$myemail = "[email protected]";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$bank = check_input($_POST['bank'], "Enter your name");
$email = check_input($_POST['email']);
$phone = check_input($_POST['phone']);
$headphones = check_input($_POST['headphones']);
$subject = "Allied Affiliated Funding - Bose Landing Page Form Submission";
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $name
Bank: $bank
E-mail: $email
Phone: $phone
Head Phones: $headphones
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
if($_POST['headphones'] == Yes){
header("Location: landing1/thank-you-bose.html");
}
else {
header("Location: landing1/thank-you.html");
}
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
나는 이것을 완성하는 방법에 대한 여러 가지 튜토리얼을 살펴 봤지만 아무런 해결책이없는 것으로 보인다.
'$ email'과'$ phone'에 대한 메시지를 제공해야합니다. 그렇지 않으면 유효성 검사 기능에 오류가 표시되지 않습니다. – Barmar
더 자세히 설명해 주시겠습니까? –
오류를 표시하기 전에'if ($ problem && strlen ($ data) == 0)'이 있습니다. 따라서 두 번째 인자없이'check_input()'을 호출하면 에러가 표시되지 않습니다. – Barmar