php表格未经验证或发送邮件

时间:2016-11-16 14:19:03

标签: php forms validation

我试图修复一个不起作用的php表单 我是php新手,请帮助
我在index.html文件中有表单,并有另一个validation.php来验证表单字段。

<!-- contact form --->
<div class="containerq">
    <div class="form_container">
        <h2 class="text-center">Contact us</h2>
        <form action="index.php" method="post" class="rd-mailform">
            <input type="hidden" value="contact" name="form-type">
            <fieldset>
                <div class="row">
                    <div class="col-md-preffix-1 col-md-12">
                        <label data-add-placeholder="" class="mfInput">
                            <input type="text" name="name" placeholder="Name">
                        </label>
                    </div>
                    <div class="col-md-preffix-1 col-md-12">
                        <label data-add-placeholder="" class="mfInput">
                            <input type="text" name="email"  placeholder="Email">
                        </label>
                    </div>
                    <div class="col-md-preffix-1 col-md-12">
                        <label data-add-placeholder="" class="mfInput">
                            <input type="text" name="phone"  placeholder="Phone">
                        </label>
                    </div>
                    <div class="col-md-preffix-1 col-md-12">
                        <label data-add-placeholder="" class="mfSelect">
                            <select class="opt">
                                <option>Choose subject</option>
                                <option>I have a problem with a game</option>
                                <option>I have a problem with a course</option>
                                <option>I have an Idea</option>
                                <option>Collaboration with schools</option>
                                <option>Collaboration with game developers</option>
                                <option>General query and other subjects</option>                                   
                            </select>
                            <ul class="dropdown">
                                <li class="option">I have a problem with a game</li>
                                <li class="option">I have a problem with a course</li>
                                <li class="option">I have an Idea</li>
                                <li class="option">Collaboration with schools</li>
                                <li class="option">Collaboration with game developers</li>
                                <li class="option">General query and other subjects</li>
                            </ul><span class="mfPlaceHolder"></span>
                        </label>
                    </div>
                    <div class="col-md-preffix-1 col-md-12">
                        <label data-add-placeholder="" class="mfInput">
                            <textarea data-constraints="" name="message" placeholder="Message" class="mtext"></textarea>
                        </label>
                    </div>
                    <!--<input class="header_button" type="submit" name="submit" value="Send Request">-->
                    <div class="header_button"><a href="#">Send Request</a>  </div>
                    <span class="success"><?php echo $successMessage;?></span>
                    <div class="mfInfo mfProgress"><span class="cnt"></span><span class="loader"></span><span class="msg"></span></div>
                </div>
            </fieldset>
        </form>
    </div>
</div>

标题中的

(包含在index.php开头)我添加了:

<?php require_once "validation.php";?>

因为如果我在索引的开头放置require_once validation.php,则不会访问该表单。

验证.php中的

是发送邮件的部分:

if( !($name=='') && !($email=='') && !($phone=='') &&!($message=='') )
    { // Checking valid email.
        if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
        {
            $header= $name."<". $email .">";
            $headers = "mywebsite.com"; /* Let's prepare the message for the e-mail */
            $msg = "Hello! $name Thank you...! For Contacting Us.
            Name: $name
            E-mail: $email
            Phone: $phone
            Message: $message
            This is a Contact Confirmation mail. We Will contact You as soon as possible.";
            $msg1 = " $name Contacted Us. Hereis some information about $name.
            Name: $name
            E-mail: $email
            Phone: $phone
            Message: $message "; /* Send the message using mail() function */
            if(mail($email, $headers, $msg ) && mail("receiver_mail_id@mywebsite.com", $header, $msg1 ))
            {
                $successMessage = "Message sent successfully.......";
            }
        }
        else
        { 
            $emailError = "Invalid Email";
        }
    }  

但是id没有发送任何内容 页面重新加载到index.php#
我没有收到无效的电子邮件或消息发送成功消息

这是完整的validation.php:

<?php // Initialize variables to null.
//echo "validating";
$name =""; // Sender Name
$email =""; // Sender's email ID
$purpose =""; // Subject of mail
$message =""; // Sender's Message
$phone ="";
$nameError ="";
$emailError ="";
$phoneError ="";
$messageError ="";
$successMessage =""; // On submittingform below function will execute.
if(isset($_POST['submit'])) 
{ // Checking null values in message.
    echo "validating2";
    if (empty($_POST["name"])){
        $nameError = "Name is required";
    }
    else
    {
        $name = test_input($_POST["name"]); // check name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name))
        {
            $nameError = "Only letters and white space allowed";
        }
    } // Checking null values inthe message.
    if (empty($_POST["email"]))
    {
        $emailError = "Email is required";
    }
    else
    {
        $email = test_input($_POST["email"]);
    } // Checking null values inmessage.
    if (empty($_POST["phone"]))
    {
        $phoneError = "Phone is required";
    }
    else
    {
        $phone = test_input($_POST["phone"]);
    } // Checking null values inmessage.
    if (empty($_POST["message"]))
    {
        $messageError = "Message is required";
    }
    else
    {
        $message = test_input($_POST["message"]);
    } // Checking null values inthe message.
    if( !($name=='') && !($email=='') && !($phone=='') &&!($message=='') )
    { // Checking valid email.
        if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
        {
            $header= $name."<". $email .">";
            $headers = "mywebsite.com"; /* Let's prepare the message for the e-mail */
            $msg = "Hello! $name Thank you...! For Contacting Us.
            Name: $name
            E-mail: $email
            Phone: $phone
            Message: $message
            This is a Contact Confirmation mail. We Will contact You as soon as possible.";
            $msg1 = " $name Contacted Us. Here is some information about $name.
            Name: $name
            E-mail: $email
            Phone: $phone
            Message: $message "; /* Send the message using mail() function */
            if(mail($email, $headers, $msg ) && mail("contact@mywebsite.com", $header, $msg1 ))
            {
                $successMessage = "Message sent successfully.......";
            }
        }
        else
        { 
            $emailError = "Invalid Email";
        }
    }
} // Function for filtering input values.function test_input($data)
// Function for filtering input values.
function test_input($data)
{
    $data = trim($data);
    $data =stripslashes($data);
    $data =htmlspecialchars($data);
    return $data;
}

&GT?;

任何想法?

0 个答案:

没有答案