Help - Search - Members - Calendar
Full Version: PHP Form Validation
HTMLHelp Forums > Programming > Server-side Scripting
dougmanic
I'm trying to validate a form using PHP, so that if a user enters a empty value in any of the fields (I have text boxes, drop-down options and radio buttons), data will not be submitted and a red error text will appear above the field.

I tried creating a function that will check if each of the required fields are entered. If they are, data will be saved; if not the form will be printed again with the error. But the problem here is that all the input that the user entered disappears and all the options are unselected.

Is there anyway else to do this?
Brian Chandler
QUOTE(dougmanic @ Nov 26 2006, 12:00 PM) *

I'm trying to validate a form using PHP, so that if a user enters a empty value in any of the fields (I have text boxes, drop-down options and radio buttons), data will not be submitted and a red error text will appear above the field.



This won't work. PHP runs on your server, so unless stuff is *submitted* your PHP program will never see it.

QUOTE

I tried creating a function that will check if each of the required fields are entered. If they are, data will be saved; if not the form will be printed again with the error. But the problem here is that all the input that the user entered disappears and all the options are unselected.

Is there anyway else to do this?


Unless you show us this function, it's hard to comment on. But what you ought to be doing is to write a javascript 'onsubmit' handler, that checks fields are completed, and refuses to submit if they aren't.

(Then, of course, the form handler needs to check the submitted data, and respond appropriately if it's no good.)


Darin McGrew
Please see the FAQ entry How can I require that fields be filled in, or filled in correctly?
Curtis
Wow, it's been a while since I've been here - new forums are looking great! biggrin.gif

If you want the front end control that JS allows, plus the power of your server side scripting, asynchronous scripting (Ajax) is an option. If you're new to this topic, you should be aware of the host of accessibility problems posed by using Ajax, though.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.