Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Server-side Scripting _ PHP Form Validation

Posted by: dougmanic Nov 25 2006, 10: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.

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?

Posted by: Brian Chandler Nov 26 2006, 05:23 AM

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.)



Posted by: Darin McGrew Nov 26 2006, 12:21 PM

Please see the FAQ entry http://www.htmlhelp.com/faq/html/forms.html#required-fields

Posted by: Curtis Dec 28 2006, 04:49 AM

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.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)