NikosDim Posted July 25, 2012 Share Posted July 25, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/ Share on other sites More sharing options...
Pikachu2000 Posted July 25, 2012 Share Posted July 25, 2012 Google 'php sticky forms'. Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364163 Share on other sites More sharing options...
peipst9lker Posted July 25, 2012 Share Posted July 25, 2012 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) Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364168 Share on other sites More sharing options...
hakimserwa Posted July 25, 2012 Share Posted July 25, 2012 this guy explained this better atleast easy to understand. look for this section or read the whole post. """Prevent Inputs from Clearing After an Error"" http://buildinternet.com/2008/12/how-to-validate-a-form-using-php-part-2-streamline-using-arrays/ Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364176 Share on other sites More sharing options...
peipst9lker Posted July 25, 2012 Share Posted July 25, 2012 Echo the sent GET/POST variables (escape them to avoid XSS-vulnerabilities) into the value-attribute and you're done? Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364186 Share on other sites More sharing options...
hakimserwa Posted July 25, 2012 Share Posted July 25, 2012 Echo the sent GET/POST variables (escape them to avoid XSS-vulnerabilities) into the value-attribute and you're done? we learn new things every day can you please show as how? Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364189 Share on other sites More sharing options...
peipst9lker Posted July 25, 2012 Share Posted July 25, 2012 Very very very basic example <form action="example.php" method="POST"> <input type="text" name="example" value="<?php echo (isset($_POST['example'])) ? htmlentities($_POST['example']) : ''; ?>" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364193 Share on other sites More sharing options...
NikosDim Posted July 25, 2012 Author Share Posted July 25, 2012 Thank you very much for your answers. I will try what you told me. I hope everything will go well! Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364220 Share on other sites More sharing options...
hakimserwa Posted July 25, 2012 Share Posted July 25, 2012 welcome Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364320 Share on other sites More sharing options...
NikosDim Posted July 25, 2012 Author Share Posted July 25, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364376 Share on other sites More sharing options...
peipst9lker Posted July 26, 2012 Share Posted July 26, 2012 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364493 Share on other sites More sharing options...
cyberRobot Posted July 26, 2012 Share Posted July 26, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364542 Share on other sites More sharing options...
NikosDim Posted July 26, 2012 Author Share Posted July 26, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364686 Share on other sites More sharing options...
peipst9lker Posted July 27, 2012 Share Posted July 27, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1364793 Share on other sites More sharing options...
hakimserwa Posted August 1, 2012 Share Posted August 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266203-form-validation-without-deleting-the-fields/#findComment-1366044 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.