表格未在提交时清算

时间:2013-08-04 17:29:40

标签: php javascript html

我正在网站上工作,我想在提交时清除注册表。提交表单被重定向到php脚本,我通过php脚本将表单发送到电子邮件。我不知道我哪里出错,但我无法在提交后清除表格的内容。它成功检查空白值,当我提交它不清除控件,我使用一个函数清除表单,如果我在提交之前运行..清除表单,但后来没有数据发送到脚本..请帮助我进入相同的

这是我的Java scirpt

function verifyEmail(form) {
    //alert(frmContact.email.value);
    checkEmail = contactform.email.value;
    form=contactform;   
    //alert("Please select an answer.");

    if(form.fname.value=='')

        {

            alert("Name is blank");

            form.fname.select();

            return false;

        }

    else if(form.dob.value=='')

        {

            alert("Date Of Birth is blank");

            form.dob.select();

            return false;

        }



        else if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.'))) 

    {

    alert("You have entered an invalid email address. Please try again.");

    form.email.select();

    return false;

    } 
        else if(form.mob.value.search(/\d{3}\-\d{3}\-\d{4}/)==-1)

        {

            alert("The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx");

            form.mob.select();

            return false;

        }
    else if(form.state.value=='')

        {

            alert("State is blank");

            form.state.select();

            return false;

        }

        else if(form.city.value=='')

        {

            alert("City is blank");

            form.city.select();

            return false;

        }


        else if(form.message.value=='')

        {

            alert("Message is blank");

            form.message.select();

            return false;

        }
    else

    {
    form.method="post";
form.target="_self";
form.action="reg_contact.php";
form.submit();
form.fname.value='';
form.dob.value='';
form.email.value='';
form.mob.value='';
form.state.value='';
form.city.value='';
form.message.value='';
clearForm();


    }
    }

    //--></script>
    <script language="javascript">
    function clearForm(form)
    {
        $(":input", form).each(function()
        {
        var type = this.type;
        var tag1= this.tagName.toLowerCase();
        var tag = this.tagName.toLowerCase();
            if (type == 'text' || tag=='textarea' )
            {
            this.value = "";
            }
        });
    };

这是我的表单内容

<form id="contactform" class="form" method="post" name="contactform" onsubmit="return(verifyEmail())" style="height: 499px">
    <fieldset>
    <div>
        <label for="fname" style="width: 216px">Full Name:<span> *</span>
        </label><input id="fname" name="fname" type="text"> </div>
    <div style="margin-left: 45%; margin-top: -13%;">
        <label for="name" style="width: 250px">Date Of Birth:<span> *</span></label><input id="datepicker" name="dob" type="text" />
    </div>
    <div>
        <label style="width: 216px">E-mail:<span>*</span></label><input id="email" name="email" type="text">
    </div>
    <!--
                            <div>
                                <label for="email" >Email: <span>*</span></label>
                                <input name="email" type="email" id="email" pattern="^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$" />
                            </div>
                    -->
    <div style="margin-left: 45%; margin-top: -13%;">
        <label accesskey="U" for="name" style="width: 216px">Mobile Number:<span> 
        *</span></label> <input id="mob" name="mob" type="text" /> </div>
    <div>
        <label>State:<span> *</span></label>
        <input id="state" name="state" type="text"> </div>
    <div style="margin-left: 45%; margin-top: -12.5%;">
        <label accesskey="U" for="name">City:<span> *</span></label>
        <input id="city" name="city" type="text"> </div>
    <div>
        <label accesskey="U" for="name">Country:<span> *</span></label>
        <input id="country" disabled="disabled" name="country" type="text" value="India" />
    </div>
    <div>
        <label>Course:<span> *</span></label>
        <select id="cource" name="cource" style="height: 34px; width: 241px">
        <option value="Diploma Polytechnic">Diploma - Polytechnic</option>
        <option selected="" value="UG-BE">Bachelor Of Engineering (BE)</option>
        <option value="UG-B.Tech">Bachelor Of Technology (B.Tech)</option>
        <option value="PG-MCA">Master Of Computer Application (MCA)</option>
        <option value="PG-ME">Master Of Engineering (ME)</option>
        <option value="PG-M.Tech">Master Of Technology (M.Tech)</option>
        </select> </div>
    <div>
        <label accesskey="C" for="comments">Message: <span>*</span></label>
        <textarea id="comments" cols="40" name="message" rows="3" spellcheck="true"></textarea>
    </div>
    </fieldset>
    <input id="submit" class="submit" style="height: 36px; width: 90px; margin-top: -0.18%;" type="submit" value="      Send      ">
    <input id="reset" class="reset" style="height: 36px; width: 90px; margin-left: 5px; margin-top: -0.18%;" type="reset" value="   Reset    ">
    <div class="clearfix">
    </div>
</form>

请让我知道我哪里出错...我知道我弄乱这个简单的事情。

2 个答案:

答案 0 :(得分:1)

变化

<form ... onsubmit="return(verifyEmail())" ...>

<form ... onsubmit="this.reset(); return verifyEmail();" ...>

或保留您已有的功能:

function clearForm(form) { // pass a DOM element
    form.reset();
}

答案 1 :(得分:0)

为表单<form action="" ... >

提供操作