Jump to content

automatic validation of form with php


archer9134

Recommended Posts

Hi, i am still trying to learn html and php, but i am also trying to create a website which contains a registration form. I want the validation of each field of that form to be performed automatically. So using onBlur, whenever the user steps out of a textbox field, i want to execute a php function located in another file, which will check the value of the field and display a note if the value is invalid.

I want to do this using as less javascript as possible.

I am not sure if what i am asking is possible but if it is i would like to know the easiest way to do this.

 

I would appreciate any help i can get.

Thanks for your time.

Link to comment
Share on other sites

You can do this, but as PHP acts on the server and not the browser, you would need to return the form to the server to check the validation after every onBlur event.  Javascript is the best to use for form validation prior to sending to the server for a final more thorough check.

Link to comment
Share on other sites

yes but i want some checks to be done by php, like checking if the username exists in the database or validating the email. Maybe i could check only these two fields with form submission to php and the rest with javascript. But still i don't know how i can call a specific php function using onBlur.

Link to comment
Share on other sites

What you do is use Javascript to check the completed fields on the form with basic validation using onBlur (completed/incomplete, numbers, email format etc).  Once these criteria are all satisfied, you then pass all the form values back to the server to check against your DB using PHP.

 

Javascript / client side should not be used for referencing against a DB, just for checking the basics as to whether the form fields are filled in correctly.

 

You need to write some code to connect to your DB in PHP and check the values from the form (sent back using POST method) and then produce a success / fail output to be sent back to the browser.

 

There are loads of good tutorials around this, just use google to find them.

 

Once again, use Javascript to validate the FORMAT of the input, and use PHP to validate against your DB.

 

A hint for the future though, make sure you "clean" the user inputs prior to doing any sort of DB work to prevent SQL injection etc

Link to comment
Share on other sites

You can look into using AJAX for checking these. It will allow you to have JS call a PHP script (which can then check for a specific username/email/etc in the database.)

 

Using a JS framework such as jQuery or prototype will help you dive into the JS world easily ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.