0

我对 PHP 编码很陌生。我做了大量的研究来尝试帮助我。正如你可以想象的那样,我已经获得了大量的帮助材料。问题是当我试图把它们放在一起时。

具体来说这是我的问题。我遇到过:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "e-Mail is Valid";
} else {
    echo "Invalid e-Mail";
}

但我不知道如何实现它。就目前而言,验证器会在用户有时间输入它们之前检查这些字段......我很绝望

我确信解决方案非常简单,但我已经花了几个小时在这上面,我真的很渴望这个问题已经得到解决。

这是页面的链接

这是页面的代码:

<!DOCTYPE html>
<head>
<meta charset='utf-8'> 
<title>AWalsh Photography - Contact Me</title>
<link href="style/main_page.css" rel="stylesheet" type="text/css" />
</head>
<body>

<div id="email_container">
      <h1 class="email_head"> Contact Andrew walsh Photography</h1>
      <form id="email_form" name="email_form" method="post">
      <table>
    <tr>
    <td><label for="fname">First Name:</label>
    </td>
    <td><input type="text" name="fname_input" id="fname_input" /><br>
    </td>
    </tr>
     <tr>
    <td><label for="lname">Last Name:</label>
    </td>
    <td><input type="text" name="lname_input" id="lname_input" /><br>
    </td>
    </tr>
    <tr>
    <td><label for="email_input">Your Email:</label>
    </td>
    <td><input type="text" name="email_input" id="email_input" /><br>
    </td>
    </tr><tr>



    <td><label for="email_conf">Re-enter Email:</label>
    </td>
    <td><input type="text" name="email_conf" id="email_conf" /><br>
    </td>
    </tr><tr>
    <td>
    <label for="message_input">Message </label>
    </td><td>
    <textarea rows="8" cols="45" id="message_input" name="message_input"></textarea>
    </td></tr><tr><td></td>
    <td>
    <input id="submit"type="submit" value="submit" name="submit"/>        
    </td></tr>
    </table>
    </form>

<?php 

if($_POST['email_imput'] == $_POST['email_conf']){
    //stuff to do on success
    echo '<h1>Success!!</h1>';
} else {
    //stuff to do on failure
    echo '<h1>Sorry, The emails you entered do not match</h1>';
}


    $email_imput = $_POST['email_imput'];

if (filter_var($email_imput, FILTER_VALIDATE_EMAIL)) {
    echo $email_imput . ' is a valid email address.';
} else {
    echo $email_imput . ' is not a valid email address.';
}

$message_imput = $_POST['message_imput'];

$msg = "Email address: $email_imput \n" . "Message: $message_imput";

$to = 'myemail@gmail.com ';

$subject = 'AWP_email';



if (filter_var($email_imput)){

mail($to, $subject, $msg, $email);
}
if (mail($to, $subject, $msg, $email)) {
   echo("<p>Message successfully sent! Thanks for submitting your message. We will reply to you as soon as possible</p>");
  } else {
   echo("<h1>Sorry, There was an error in your imput. Please try again.</h1>");
  }

?>  
<span class="error"><?=$error;?></span>
<form method="post" action="">
    <h1> There was an error with your post</h1>
</form>     

</div>



</div>

</body>
</html>

任何输入都会令人惊叹。谢谢你。

4

2 回答 2

0

您可以在表单中添加一个隐藏字段,并在发送电子邮件时检查它的值。

if (isset($_POST["some_hidden_field"])) {
    // put form validation and sending email here
}
else {
    // print the form
}
于 2013-06-17T21:35:21.440 回答
0

您应该首先检查页面是否已提交。if ($_SERVER['METHOD'] == 'POST')您可能想在进行任何验证之前尝试

于 2013-06-17T22:34:24.070 回答