Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
Like Tree3Likes
  • 1 Post By mlseim
  • 1 Post By Ronald Roe
  • 1 Post By Ronald Roe

Thread: js and forms

  1. #1
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times

    js and forms

    so I know about javascript now, if/else, loops, switches, oop and all that crazy sstuff but how does a javascript script prevent a form from being submitted to the server if a field is not a match?

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    JS, or JQuery, AJAX ... all of that is related to the user experience. Some of the validation can be done with JS. AJAX does the work of submitting to PHP without refreshing the page. Error messages, user notifications, etc. can be done with JS. So JS can do some validation (field checking). Ultimately, PHP is the workhorse for real validation. Nothing should be submitted to the server unless it passes the PHP validation. If there is a problem with a field, PHP returns that back as an error. Once again, it's JQuery/AJAX that displays these problems to the user. This all happens without any page refreshes. The User Interface is smooth and slick.

    With JQuery/AJAX on your web page, a person can view the HTML source and see the javascripting. They can see which PHP script is handling the POST or GET (the URL). If you think that might be a problem, because you can't hide it, you can use two PHP scripts. The one that shows in the AJAX URL handling is a PHP script that does all of the validation and if successful, uses CURL to POST the data to the real PHP script ... the one that does the server database stuff, emails, text messages, etc. That PHP script is not visible to any users.
    busso likes this.


  4. #3
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times
    i have seen ajax make requests to a server using jquery (which is trivial compared to hardcore ajax) but how is it used to send form data to a php script?

  5. #4
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Quote Originally Posted by busso View Post
    so I know about javascript now, if/else, loops, switches, oop and all that crazy sstuff but how does a javascript script prevent a form from being submitted to the server if a field is not a match?
    To answer the original question, you need to add an event listener to the form's submit event. Then the first line of the callback function will be event.preventDefault().

    Like so:
    Code:
    let form = document.querySelector('.the_form');
    form.addEventListener('submit', function(event){
      event.preventDefault();
      // AJAX code goes here
    });
    busso likes this.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  6. #5
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Quote Originally Posted by busso View Post
    i have seen ajax make requests to a server using jquery (which is trivial compared to hardcore ajax) but how is it used to send form data to a php script?
    You make the request almost the same way. You just send the data with it. Normally, you'd use POST instead of GET, but it isn't exactly required. Of course, you have to send it all to a script that will do something with it...
    busso likes this.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:46 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com