使用javascript验证的PHP邮件问题

时间:2012-04-25 09:57:17

标签: php javascript validation email

我正在尝试在Javascript验证后从同一个PHP页面发送电子邮件,但在发送电子邮件时遇到一些问题。有人可以帮我解决这个问题吗?

以下是JavaScript代码:

        <!-- -->
        <script type="text/javascript">                                                
        function checkCheckBoxes() {

            if (document.form.inforequired.checked == true)
            {
                if (document.form.option1.checked == false && document.form.option2.checked == false && document.form.option3.checked == false && document.form.option4.checked == false && document.form.option5.checked == false && document.form.option6.checked == false && document.form.option7.checked == false && document.form.option8.checked == false  && document.form.option9.checked == false && document.form.option10.checked == false)
                {
                    document.getElementById('error').innerHTML = "Please choose your options.";
                    return false;   
                }

                if (document.form.option8.checked == true  || document.form.option9.checked == true || document.form.option10.checked == true)
                {
                    if (document.form.address.value == "" )
                    {
                        document.getElementById('erroraddress').innerHTML = "Please enter your address.";
                        return false;
                    }
                }
                else
                {
                    document.forms["form"].submit();
                    return true;
                }

            }
            else
            {
                document.forms["form"].submit();
                return true;
            }
        }
    </script>
    <!-- Email -->

以下是在else部分发送电子邮件的PHP代码。

<div id="content">
    <h1 id="formSpacingHeading"> Visitor Information </h1>

 <?if( !isset($_POST['submit'])):?> 

    <form id="form" name="form" onsubmit="return checkCheckBoxes();" action="" method="POST">

        <p id="formSpacing"><label for="username" class="iconic user" > Name <span class="required">*</span></label> <input type="text" name="username" id="username"  required="required" placeholder="Enter your name"  /></p>        
        <p id="formSpacing"><label for="usermail" class="iconic mail-alt"> E-mail address <span class="required">*</span></label> <input type="email" name="usermail" id="usermail" placeholder="Enter your E-mail ID" required="required"  /> </p>
        <p id="formSpacing"><label for="contactno" class="iconic link"> Contact number <span class="required">*</span></label> <input type="text" pattern="^\d\d\d\d\d\d\d\d\d\d$" name="contact" id="contactno"  placeholder="Enter your contact number" required="required" /></p>
        <input type="submit" name="submit" value="Submit Form" />
    </form>

<?php else :
        $name=$_POST['username'];
        $mail=$_POST['usermail'];
        $contact=$_POST['contact'];

        $to = "admin@xxx.com";
        $subject = "New Visitor Information";
        $message = $name;
        $from = $mail;
        $headers = "From:" . $from; 
        $email = mail($to, $subject, $message, $headers);

        if($email){
            echo "Thank you. We will keep you updated with the latest information.";
        }
        else{
            echo "Error processing your request. Please try again later.";
        }
?>
<? endif ?> 

1 个答案:

答案 0 :(得分:-1)

我不是PHP大师,但我已经编写了一些基本的东西。

我认为你的if语句都错了。

改变..

<?if( !isset($_POST['submit'])):?> 

 <?if( !isset($_POST['submit'])){?> 

更改

<?php else :

<?php } else {

更改

?>
<? endif ?> 

 }?>