简体   繁体   English

表单的输入类型=“提交”似乎没有触发onsubmit处理程序

[英]Form's input type=“submit” does not seem to trigger the onsubmit handler

I am attempting to trigger a javascript function via onsubmit to validate my form. 我试图通过onsubmit触发javascript函数来验证我的表单。

I know that the function itself works because I can call it within script tags immediately after the form's markup and I receive my console.log message and the error messages it creates appear on the form. 我知道该函数本身起作用,因为可以在表单标记后立即在脚本标记中调用它,并且会收到console.log消息,并且它创建的错误消息会出现在表单上。

The PHP validation itself works, but I've only been able to actually submit the form via javascript's .submit placed upon a div. PHP验证本身可以工作,但实际上我只能通过div上的javascript .submit提交表单。 Obviously because this bypasses any client-side validation, I can't leave it that way, thus I've replaced my div id="submit" with an input type="submit" . 显然,因为它绕过了任何客户端验证,所以我不能那样做,因此我用input type="submit"替换了div id="submit" input type="submit"

I've been looking at other examples, including forms I've coded myself in the past that I know work, and seem completely out of answers. 我一直在看其他示例,包括我过去曾经编写过的表格,这些表格我很了解,但似乎完全没有答案。 This is probably insanely easy, and for some reason, I just can't and haven't been able to see it for the last 6 hours. 这可能非常容易,并且由于某些原因,我只是过去六个小时无法看到,也一直无法看到它。 o_O O_O

Here is my form's markup: 这是我表单的标记:

<form name="emailus" id="emailus" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return validateForm();">
    <a href="mailto:example@email.com">
        <h3><i class="icon-envelope-alt icon-large"></i>Send us an email<span>: example@email.com</span></h3>
    </a>

    <div class="half">
        <fieldset class="name">
            <label for="cf_name"><h4>Name <span>*</span></h4></label>
            <input type="text" name="cf_name" class="textualformfield" alt="Jane Doe" pattern="^[a-zA-Z'\s]+$">
        </fieldset>
        <fieldset class="emailaddress">
            <label for="cf_email"><h4>E-mail <span>*</span></h4></label>
            <input type="text" name="cf_email" class="textualformfield" alt="janedoe@email.com" pattern="{long string of regex}">
        </fieldset>
        <fieldset class="phonenumber">
            <label for="cf_phonenumber"><h4>Phone</h4></label>
            <input type="tel" name="cf_phonenumber" class="textualformfield" alt="555-555-5555">
        </fieldset>
        <fieldset class="eventdate">
            <label for="cf_eventdate"><h4>Event Date</h4></label>
            <input type="text" name="cf_eventdate" class="textualformfield" alt="May 25th, 2012">
        </fieldset>
        <fieldset class="location">
            <label for="cf_location"><h4>Location</h4></label>
            <input type="text" name="cf_location" class="textualformfield" alt="The Church">
        </fieldset>
    </div>

    <div class="half">
        <textarea name="cf_message" class="textualformfield" alt="Your Message"></textarea>
    </div>

    <input type="submit" for="emailus" name="submit" id="submit" value="Submit">
</form>

I've attempted several different iterations of onsubmit="return validateForm();" 我尝试了onsubmit="return validateForm();"几种不同迭代onsubmit="return validateForm();" —with or without the semicolon, writing it as onSubmit or onsubmit ... I don't know. —有或没有分号,将其写为onSubmitonsubmit ...我不知道。 I can't see what's wrong with this at all. 我完全看不出这是怎么回事。 Anyone? 任何人?

Below is the function to be called onsubmit , validateForm . 下面是被称为onsubmitvalidateForm的函数。 It is located in another file, but the file is always included when the form is, and I've made sure this function is not within $(document).ready and is available globally as I've called it from within script tags directly following the form. 它位于另一个文件中,但是在包含表单时总是包含该文件,并且我确保此函数不在$(document).ready ,并且可以全局使用,因为我直接在脚本标记中调用了该函数按照表格。

var validateForm = function() {
    var valid = true;

    jQuery('p.validationhelpers').remove();

    if (document.emailus.cf_email.value == "" || document.emailus.cf_email.value == "janedoe@email.com") {
        jQuery('.emailaddress').append("<p class='validationhelpers'>Please enter an email address.</p>");
        jQuery('.emailaddress>input').focus();
        valid = false;
    }

    if (document.emailus.cf_name.value == "" || document.emailus.cf_name.value == "Jane Doe") {
        jQuery('.name').append("<p class='validationhelpers'>Please enter your name.</p>");
        jQuery('.name>input').focus();
        valid = false;
    }
    console.log("I was triggered");

    return valid;
}

Edit 2: Added PHP Validation/Post code: 编辑2:添加了PHP验证/邮政编码:

<?php if (!empty($_POST) ) { 
    $field_name = $_POST['cf_name'];
    $field_email = $_POST['cf_email'];
    $field_phone = $_POST['cf_phonenumber'];
    $field_eventdate = $_POST['cf_eventdate'];
    $field_location = $_POST['cf_location'];
    $field_message = $_POST['cf_message'];

    $mail_to = 'example@email.com';

    //final attempt at validation or email submission preventation
    if ($field_name == "Jane Doe" || empty($field_name) || $field_email == "janedoe@email.com" || empty($field_email)) {
        return false;
    }


    if (!($field_eventdate == "May 25th, 2012") && !empty($field_eventdate) && !($field_name == "Jane Doe") && !empty($field_name)) {
        $subject = 'Request for '.$field_date. ' from '.$field_name . 'via thiswebsite.com';
    } else {
        $subject = $field_name . ' has contacted you via thiswebsite.com';
    }

    if (!($field_name == "Jane Doe") && !empty($field_name)) {
        $body_message = 'Client\'s Name: '.$field_name."\n\n";
    }
    if (!($field_email == "janedoe@email.com") && !empty($field_email)) {
        $body_message .= 'E-mail: '.$field_email."\n\n";
    }
    if (!($field_phone == "555-555-5555") && !empty($field_phone)) {
        $body_message .= 'Phone: '.$field_phone."\n\n";
    }
    if (!($field_eventdate == "May 25th, 2012") && !empty($field_eventdate)) {
        $body_message .= 'Event Date: '.$field_eventdate."\n\n";
    }
    if (!($field_location == "The Church") && !empty($field_location)) {
        $body_message .= 'Location: '.$field_location."\n\n";
    }
    if (!($field_message == "Your Message") && !empty($field_message)) {
        $body_message .= 'Message: '. "\n".$field_message;
    }

    $headers = 'From: '.$field_email."\r\n";
    $headers .= 'Reply-To: '.$field_email."\r\n";

    $mail_status = mail($mail_to, $subject, $body_message, $headers);

    $body_message = stripslashes($body_message);

    if ($mail_status) { ?>
        <script language="javascript" type="text/javascript">
            jQuery(document).ready(function () {
                jQuery('#contactus').before("<hr class='confirmationhelpers'><p class='confirmationhelpers'>Your e-mail has been sent!<br/>Thank you! We'll contact you shortly.</p><hr class='confirmationhelpers'>");
            });
        </script>
    <?php
    }
    else { ?>
        <script language="javascript" type="text/javascript">
            jQuery(document).ready(function () {
                alert('It seems there\'s been an error. Please, send an email to example@email.com to request your appointment.');
            });
        </script>
    <?php
    }
}

?>

Just to be clear—when I click the Submit button, absolutely nothing happens. 只是要清楚一点—当我单击“提交”按钮时,绝对没有任何反应。

Your jQuery is blocking the form submit here: 您的jQuery在此处阻止了表单提交:

//this hides the contact form if the overlay is clicked. children is there to prevent the children from also having this effect.
    $('.overlay_vertical_align').click(function() {
        $('.grey_overlay').fadeOut('slow');
    }).children().click(function(e) {
        return false;
    });

I mean, when I debug your site, and press the submit button, I reach the return false; 我的意思是,当我调试您的网站并按下提交按钮时,我return false; above, after which the event processing is over. 以上,之后事件处理结束。

The reason nothing happens is that you use the new html5 'pattern' attribute which tries to regex validate your input before it even reaches your validate function. 没有任何反应的原因是您使用了新的html5'pattern'属性,该属性试图在输入到达验证功能之前对其进行正则表达式验证。 Remove the pattern attribute and try again. 删除模式属性,然后重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义JS表单提交不会停止onsubmit处理程序进行调试 - custom JS form submit does not stop onsubmit handler to debug 在表单中渲染一个 button[type="submit"] 触发 onSubmit 回调 - Render a button[type="submit"] in a form trigger the onSubmit callback React.js:以编程方式提交表单不会触发 onSubmit 事件 - React.js: submit form programmatically does not trigger onSubmit event 如何通过 onSubmit 事件处理程序提交表单 - How to submit a form through onSubmit event handler form.submit触发onsubmit但不提交 - form.submit triggers onsubmit but does not submit 提交功能不会触发onsubmit返回功能吗? - submit function does not trigger onsubmit return function? OnSubmit function 不适用于输入类型=“提交” - OnSubmit function not working for an input type= 'submit' 为什么每次击键后反应表单的 onSubmit 都会触发,如何使用输入按钮提交表单? - Why does react form's onSubmit fire after every keystroke, how to use enter button to submit form? 使用提交()从链接提交的表单不能被onsubmit处理程序捕获 - Form submitted using submit() from a link cannot be caught by onsubmit handler JavaScript隐藏HTML表单的“提交”按钮,直到从另一个输入中提交 - JavaScript Hiding an HTML Form's Submit Button until onsubmit from another input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM