Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    Javascript using PHP/SQL

    I've got a web page which has a form on it. When the Submit is clicked, it saves all the information to a database (SQL) using PHP.

    I've got PHP validation for the email and currently when the email is incorrect it redirects to a different page with the same design but with the text 'Incorrect Email'.

    However I want it to redirect to the same current page, but use javascript/jQuery to show 'Incorrect Email'.

    Any ideas of how I would do this, if it is possible.

    Last edited by ryan1234; 12-12-2012 at 08:08 PM.

  2. #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Sydney, Australia
    Thanked 932 Times in 919 Posts
    To redirect to the same page just needs a small change to the PHP. Either the validation of the form is done in the same page as the form with the form action pointing to the current page and a header location call redirects to the next page if the form is valid OR if the validation is done on a different page then it just needs a header location call to redirect back to the form if the validation falis - before outputting anything from the new page.

    Once you get the page working correctly without JavaScript you can then make it easier for your visitors who have Javascript enabled by adding field validations to the form with JavaScript (eg. http://javascriptexample.net/domform15.php ) while still knowing that the form will function correctly without JavaScript.

    jQuery is just a huge JavaScript library that provides lots of common JavaScript functionality. There is no great benefit in using it for form validation unless you are using a significant part of it for other purposes.
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  3. #3
    Senior Coder
    Join Date
    Apr 2011
    London, England
    Thanked 354 Times in 353 Posts
    Well you could add a querystring to your url:

    and use JS/jQuery to extract this value and modify the content of the page or, more usefully, just change the class of an error-message paragraph from, say "error-no" to "error-yes" so that it displays on the page.

    This is not a fully-robust solution as it is trivial to edit the address bar - but, of course, you won't be relying on this 'fail' value!!

    Preferably you should add PHP to your main page which displays different content according to whether they're log-in (or email verification) was successful. It is possible to dynamically insert a JS/jQuery script to run when the page loads, but if you are doing this you might as well just use PHP to modify the page.

    Personally, I use the same page to display and verify the form, only re-directing on a successful log-in, but I get the impression you don't want to re-write too much.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts