Not submit jquery validating on sexy woman!

Friday, August 10, 2018 by Sungo

Replies(9)

A form gets a submit event. An <input type="submit" …> gets a click event.
$('#sendform').submit(function(event) {
  1. $('[name="myForm"]').submit(function(event) {
  1. $('#sendform').click(function(event) {
Leave a comment on jakecigar's reply
Trying to compare jQuery and PHP is like trying to compare apples and oranges. PHP validation is on the server-side while JavaScript (which jQuery is) is on the client-side. Both server-side and client-side validation should be done; they are necessary.
In a document, you need only included jQuery once; if you have it included twice it will break your code.
You use the .submit() jQuery method on the form element, not the submit element, and you use the click event on the buttons (submit or otherwise). Therefore the following are valid:
  1. $( 'form' ).submit( function( event ) {
  2.       //you validation code here
  3.       // you use event.preventDefault()
  4.       // only when the form is not valid
  5. });
  1. $( '#sendform' ).on( 'click', function( event );
  2.        //you validation code here
  3.       // you use event.preventDefault()
  4.       // only when the form is not valid
  5. });
Of course they have to be wrapped in DOM ready.
Leave a comment on Godsbest's reply
The first $ is just jQuery. The second, the parameter is passed back from the jQuery. Even if $ is reassigned or overridden, the second $ stays. It makes noConflict superfluous!
Leave a comment on Godsbest's reply
Thanks very much everyone, it's nice to see a board that is very helpful and isn't as dead as some others. I hope to be able to help others here myself too one day.

I really wanted the validation to be all in PHP for security purposes, as users could probably use my site without jQuery/JavaScript enabled.

Even though it's for my company's use only who will always have it enabled, I still wanted to write everything securely from the start rather than getting into habits which could cause me problems down the line.

My PHP approach was to compare the values from two arrays but it didn't work (my final validation plan is not complex but as a newbie I could be here for ages!).

Again thank you, this has saved my neighbours the sound of me screaming through the walls :)
Leave a comment on axdskhmd's reply
You should validate on the server for security.
You should validate on the client for the user's sake,
If you skip either one, you're missing the point.

Leave a comment on jakecigar's reply
If one of them can be broken (JavaScript) should I not use the most secure method of validation (PHP)?

Assuming PHP is the most secure, of course.

I want to cut my teeth on both languages so will try each way.
Leave a comment on axdskhmd's reply
It's not a matter of "more" secure.
Validation in the browser is insecure. Period. 
It's there as a convenience for the user, so that you can let them know that they've typed something wrong before the page is ever submitted to the server.
When the request arrives at the server, you need to validate again (and perhaps differently). The user might be a hacker and isn't running your Javascript at all, but just sending requests to your server using some command-line tool (like cUrl).
If you've already validated in Javascript, there's no need worry about letting the user know when a validation fails on the server, though, unless you want to support users with Javascript turned off. (Many sites today won't work at all with Javascript turned off, though...) Log it, then give the user a generic error page, or send them off to "hacker jail". (Give them fake information. I'd suggest filling-in random FBI contact number for phone numbers, LOL...)
Leave a comment on watusiware's reply
Reply to axdskhmd's question

.submit() not working

in Getting Started •  3 years ago  
Hi all,

I'm hitting a roadblock in validating my form with PHP so have attempted to use jQuery which I understand... better. I hoped!

.submit() doesn't run the script, nor does it prevent the form being submitted to the PHP.

However, if I use .click in place of .submit, the script runs.

I've bounced in and out of Google and people are saying wrap the function inside document.ready, which I have.

They've also said make sure none of the name attributes are called "submit". They aren't... what now!!

The jQuery is in the head of my HTML and the form elements in the body. document.ready should make it unnecessary to have the jQuery before the closing body tag?

I'm learning jQuery in the mornings on the bus and PHP in thevenings on the bus, so maybe I'm too rookie or getting confused... please help :)

  1. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function($) {

    $('#sendform').submit(function(event) {

        if ($('input:text').val().length > 0) {
        // validate if at least one form field is populated
        alert("Well done, at least one form field is populated!");
        event.preventDefault(); // to be removed when validation passed
        }

        else {
        // all form fields empty, don't submit
        alert("All form fields empty!");
        event.preventDefault();
        }

    });

    });
    </script>
  2. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" name="myForm">
    -input form fields here-
    <input type="submit" id="sendform" name="something" value="Submit" />
    </form>

{"z36808437":[14737000005240122,14737000005240249,14737000005241177],"z8483411":[14737000005236915,14737000005236919],"z7664639":[14737000005241181],"z2950240":[14737000005239405,14737000005239409,14737000005239415,14737000005241125]}
http://forum.jquery.com/topic/submit-not-working-22-4-2014