Validate Required Fields and Reload Data

Hello all! I’m fairly new to PHP and I’m hoping you all can help with this scenario. It involves one site: Hotline.htm

The website contains a form which includes text fields, text areas, checkboxes, and dropdowns. The PHP in the file is meant to merely compile the data and send it via email.

Now, the customer wants input validation. Two text fields and one text area field are required. These fields are:
FirstName (text)
LastName (text)
QuestionSeven (textarea)

Ultimately, I’d like to be able to ensure that information is in those fields, I just haven’t found the right approach. JavaScript allows me to use pop-ups to instantly let users know, but if JavaScript is turned off on the client, it would be pointless.

I can successfully validate the data with PHP and display the error messages. However, after clicking “Submit”, all the form fields are empty. I’d love to be able to validate the initial submission, then reload the form with the error messages and the data that was submitted. I can do this with simple input text fields, but I don’t know how the set the values of textarea, checkboxes, or dropdowns.

(Form example, I just went to submit this post, and received a message notifying me of the errors, and the form appears with all my information still in it.)

Please let me know the best way to approach this scenario!

Thanks!
Toni

You can retain the user’s input in the value attribute of the input tag, for example:

[php]echo ‘<input type=“text” name=“firstName” value"’. $firstName . ‘">’;[/php]

then in the PHP section (usually on top) have it do the validation on the form and do something like the following (before the validation) :

[php]$firstName = ( isset($_POST[‘firstName’]) ) ? $_POST[‘firstName’] : NULL; [/php]

textarea is also easy :

[php]echo ‘’ . $content . ‘;’[/php]

A checkbox would be a little trickier, but it could be done…I forget how to do it, but I know it would involve an if statement. I must be getting old. :smiley:

Drop down would be something like this:
[php]echo ‘’ . $country . ‘’;[/php]

Where $country would have a default country instead of NULL.

That way you won’t have any undefined errors, of course you could easily do this in JavaScript though you wouldn’t have graceful degradation if the user turns JavaScript off. :wink: An you can still do this and JavaScript and have the best of both worlds. ;D

Sponsor our Newsletter | Privacy Policy | Terms of Service