Jump to content

Form validation without deleting the fields?


NikosDim

Recommended Posts

Hi to everyone

 

I have implemented a html form with some fields like email and phone and I want to validate the data before submitting the form. I want to validate the data in the server side (because javascript can be easily avoided) and so I have write some PHP functions. Currently the form is in a Form.html file and the validation script is in a Validate.php file.

 

If I the validation of the data return false how can I return to my form WITHOUT the fields to be erased?

 

Thanks

Link to comment
Share on other sites

You can do it in 2 different ways

 

Way 1

Whenever a formfield looses focus you do an AJAX-request to some kind of validation.php (GET/POST parameter for the type and value of the field)

If u go that way I'd rather write ur own jQuery Extension for this (kinda easy)

 

Way 2

Use a client-side and a server-side validation. 1 Javascript which watches your fields and when the data is sent your PHP script checks everything again (incase someone just turns off JavaScript or modifies the request)

Link to comment
Share on other sites

One last thing. In my form I have some <select> elements with many many options. In order to make the <select> fields to have the values before submission I have to put a small code snippet like this below in each option?

 

<option value='option_value'<?php echo (isset($_POST['selectname']) && $_POST['selectname'] == 'option_value')?  "selected='selected'" : '' ; ?>> option_text </option>

 

Thanks

Link to comment
Share on other sites

One last thing. In my form I have some <select> elements with many many options. In order to make the <select> fields to have the values before submission I have to put a small code snippet like this below in each option?

 

Yes you have, but I'd rather create option-tags via loop.

Another little tip for you, always put html-attributes in ""-quotes not single ones.

 

// somwhere on top of the script
$options = array(1 => 'Cheeseburger', 2 => 'Hamburger', 3 => 'BigMac');
$selected = $_POST['selectname'];

// inside your <select>
foreach ($options as $val => $option)
echo (isset($selected) && $val == $selected) ? '<option value="'.$val.'" selected="selected">'.$option.'</option>' : '<option value="'.$val.'" >'.$option.'</option>';

Link to comment
Share on other sites

Another little tip for you, always put html-attributes in ""-quotes not single ones.

 

 

Beyond personal preference, what's wrong with single quotes? Note that I prefer double quotes for HTML attributes. However I'll usually use single quotes when those attributes are within a double-quoted PHP string.

Link to comment
Share on other sites

 

Yes you have, but I'd rather create option-tags via loop.

Another little tip for you, always put html-attributes in ""-quotes not single ones.

 

 

Thanks peipst9lker. I created option with a foreach loop as you said. It was easy!!!

I will ask the same thing as cyberRobot did.

What is the difference on putting double quotes in the html-attributes?

Link to comment
Share on other sites

There is no technical difference - it's just to have everything in the same pattern and not mixed up code.

I'm very very used to it since in c/c++ single quotes are char-variables and double quotes are strings (or char[]'s) also it's part of my companys coding standard. It's just stored in my DNA already :D

Link to comment
Share on other sites

 

Yes you have, but I'd rather create option-tags via loop.

Another little tip for you, always put html-attributes in ""-quotes not single ones.

 

 

Thanks peipst9lker. I created option with a foreach loop as you said. It was easy!!!

I will ask the same thing as cyberRobot did.

What is the difference on putting double quotes in the html-attributes?

 

keeping it easy and simple so in somewhile to someone going through your code can understand easily. lets say good coding practice.

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.