duanchongzi9997 2019-05-11 11:30 采纳率: 0%
浏览 63
已采纳

如何在结果回显之前添加表单验证回显

I have a form in php which works just fine, except that the user can see result without providing email and name fields. I want to add a php code that will echo to user that name and email is required before he gets to see the final answer. I have tried adding various if and else statements but the form stops working if i fiddle with the code. I would b really grateful for some help

<?php 
$name = $_POST['fieldname5_1'];
$email = $_POST['fieldname6_1'];
$score = $_POST['fieldname158_1'];
if(empty($name) || empty($email))
{
echo "You did not fill out the required fields.";

}
else {
if($score<"6") {
echo " something1 $score";
} elseif($score<"11") {
echo " something2 $score";
} elseif($score<"30") {
echo " something3 $score";
} elseif($score<"81") {
echo " something4 $score";
}  else {
echo " something5 $score";
}
}
$formcontent="something6 $score";
$recipient = "myemail@email.com, $email";
$subject = "Your test";
$mailheader = "From: myemail@email.com 
";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

?>
  • 写回答

2条回答 默认 最新

  • dongzhong8834 2019-05-11 12:11
    关注

    if this html form u can add "required" to input? or in js if form submited check fields , if all ok, your POST will go to php action/method

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?