| | |
Need Help In PHP
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Hello, I am developing a website for which I am designing a signup form, the problem is a user fills the form and miss a field when they go back to enter that field it cleans up all the remainig field and they have to fill the whole form again. Please help me how can I achieve this.
If you think we fight for money and you fight for honor, then remember everyone fights for the thing they don't have...
Their are various way to get this done...
1) User java script to validate the form .. this is for client side validation
2) use Ajax to validate the form and tell user what he has missed on that form this is for server side validation
3) Use sessions to preserve the values
4) Use the Php code on top of the form and from that form call same php page if you find any issues you can show him on that page will form filled with what ever he had filled.
these are just basic ways their are more let me know if i can help you more
1) User java script to validate the form .. this is for client side validation
2) use Ajax to validate the form and tell user what he has missed on that form this is for server side validation
3) Use sessions to preserve the values
4) Use the Php code on top of the form and from that form call same php page if you find any issues you can show him on that page will form filled with what ever he had filled.
these are just basic ways their are more let me know if i can help you more
The suggestions einfoway made are correct, however there are other factors to take into account, for example some people do not have JavaScript enabled so the Javascript validation and AJAX will not work for this so it would not be a good idea to rely on this.
It would be a good idea to employ both JavaScript client-side validation on the form as well as some sort of PHP validation with sessions to remember the values in the fields. That way if the JavaScript validation is bypassed the PHP should pick up any errors.
It would be a good idea to employ both JavaScript client-side validation on the form as well as some sort of PHP validation with sessions to remember the values in the fields. That way if the JavaScript validation is bypassed the PHP should pick up any errors.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
If the validation is as simple as 'is the field empty' then it would be fairly simple to get JavaScript to validate them with a single function, for example,
JavaScript code:
Then add the following to each field which need validating:
However, this assumes that they will click in every field, it will not work if the field is not clicked in.
If the validation is more in-depth than this it would take a lot more.
The same applies to PHP, there is not really a 'simple solution'
JavaScript code:
PHP Syntax (Toggle Plain Text)
<language="text/javascript"> function CheckField(field) { if (field.value == "") { alert( "Field " + field.name + " is empty" ); form.field.focus(); return false ; } return true ; } </script>
PHP Syntax (Toggle Plain Text)
onsubmit="return checkform(this);"
If the validation is more in-depth than this it would take a lot more.
The same applies to PHP, there is not really a 'simple solution'
Last edited by Will Gresham; Sep 30th, 2008 at 3:58 pm.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
•
•
•
•
Thanks guys but problem is I want to do this for a form having 77 fields. Isn't there a simple method.
77 fields is large number i think javascript is a must for client side validation and for server side use sessions (PHP) It should not take that much time to have sessions implemented on all 77 fields. Yes if it has dropdowns and check boxes then you will need to write small logic.
Yes.
On the file that is processing the request you want to make sure that you have session_start(); at the top of the file, and in the case that the form inputs are invalid or blank you use the following line of code:
Then on your form for text fields including text boxes and textareas you consult the following example
for select drop downs
for multi-select drop downs
for radio buttons and check boxes
On the top of your form you also want to put
When ever you want to clear the form fields from your session variables just run this code.
That should cover just about everything, let me know if something doesn't work quite the way it should.
On the file that is processing the request you want to make sure that you have session_start(); at the top of the file, and in the case that the form inputs are invalid or blank you use the following line of code:
php Syntax (Toggle Plain Text)
<? $_SESSION['formfields'] = array(); foreach($_POST as $key=>$value) { $_SESSION[$key] = $value; $_SESSION['formfields'][] = $key; } $_SESSION['formfields'] = NULL; unset($_SESSION['formfields']); ?>
Then on your form for text fields including text boxes and textareas you consult the following example
php Syntax (Toggle Plain Text)
//text box <input type="text" name="fname" id="fname" value="<? echo isset($_SESSION['fname'])?$_SESSION['fname']:$originalvalue; ?>" /> //text area <textarea name="description" id="description"><? echo isset($_SESSION['fname'])?$_SESSION['fname']:$originalvalue; ?></textarea>
for select drop downs
php Syntax (Toggle Plain Text)
<select name="selState" id="selState"> <option<? echo isset($_SESSION['selState']) && $_SESSION['selState'] == "AZ"?' selected="selected"':""; ?> value="AZ">AZ</option> <option<? echo isset($_SESSION['selState']) && $_SESSION['selState'] == "CO"?' selected="selected"':""; ?> value="CO">CO</option> <option<? echo isset($_SESSION['selState']) && $_SESSION['selState'] == "AK"?' selected="selected"':""; ?> value="AK">AK</option> <option<? echo isset($_SESSION['selState']) && $_SESSION['selState'] == "ME"?' selected="selected"':""; ?> value="ME">ME</option> </select>
for multi-select drop downs
php Syntax (Toggle Plain Text)
<select name="selState[]" id="selState"> <option<? echo isset($_SESSION['selState']) && in_array("AZ", $_SESSION['selState'])?' selected="selected"':""; ?> value="AZ">AZ</option> <option<? echo isset($_SESSION['selState']) && in_array("CO", $_SESSION['selState'])?' selected="selected"':""; ?> value="CO">CO</option> <option<? echo isset($_SESSION['selState']) && in_array("AK", $_SESSION['selState'])?' selected="selected"':""; ?> value="AK">AK</option> <option<? echo isset($_SESSION['selState']) && in_array("ME", $_SESSION['selState'])?' selected="selected"':""; ?> value="ME">ME</option> </select>
for radio buttons and check boxes
php Syntax (Toggle Plain Text)
//check boxes <input type="checkbox"<? echo isset($_SESSION['chkColors']) && in_array("red", $_SESSION['chkColors'])?' checked="checked"':""; ?> name="chkColors[]" id="chkColorsred" value="red" /> <label for="chkColorsred">Red</label><br /> <input type="checkbox"<? echo isset($_SESSION['chkColors']) && in_array("green", $_SESSION['chkColors'])?' checked="checked"':""; ?> name="chkColors[]" id="chkColorsgreen" value="green" /> <label for="chkColorsgreen">Green</label><br /> <input type="checkbox"<? echo isset($_SESSION['chkColors']) && in_array("blue", $_SESSION['chkColors'])?' checked="checked"':""; ?> name="chkColors[]" id="chkColorsblue" value="blue" /> <label for="chkColorsblue">Blue</label><br /> <input type="checkbox"<? echo isset($_SESSION['chkColors']) && in_array("purple", $_SESSION['chkColors'])?' checked="checked"':""; ?> name="chkColors[]" id="chkColorspurple" value="purple" /> <label for="chkColorspurple">Purple</label><br /> //radio buttons <input type="radio"<? echo isset($_SESSION['rdoColors']) && $_SESSION['rdoColors'] == "red"?' checked="checked"':""; ?> name="rdoColors" id="rdoColorsred" value="red" /> <label for="rdoColorsred">Red</label><br /> <input type="radio"<? echo isset($_SESSION['rdoColors']) && $_SESSION['rdoColors'] == "green"?' checked="checked"':""; ?> name="rdoColors" id="rdoColorsgreen" value="green" /> <label for="rdoColorsgreen">Green</label><br /> <input type="radio"<? echo isset($_SESSION['rdoColors']) && $_SESSION['rdoColors'] == "blue"?' checked="checked"':""; ?> name="rdoColors" id="rdoColorsblue" value="blue" /> <label for="rdoColorsblue">Blue</label><br /> <input type="radio"<? echo isset($_SESSION['rdoColors']) && $_SESSION['rdoColors'] == "purple"?' checked="checked"':""; ?> name="rdoColors" id="rdoColorspurple" value="purple" /> <label for="rdoColorspurple">Purple</label><br />
On the top of your form you also want to put
php Syntax (Toggle Plain Text)
<? session_start(); ?>
When ever you want to clear the form fields from your session variables just run this code.
php Syntax (Toggle Plain Text)
<? foreach($_SESSION['formfields'] as $value) { $_SESSION[$value] = NULL; unset($_SESSION[$value]); } $_SESSION['formfields'] = NULL; unset($_SESSION['formfields']); ?>
That should cover just about everything, let me know if something doesn't work quite the way it should.
Last edited by R0bb0b; Sep 30th, 2008 at 4:22 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
1 correction, this first block:
is suppose to be:
•
•
•
•
php Syntax (Toggle Plain Text)
<? $_SESSION['formfields'] = array(); foreach($_POST as $key=>$value) { $_SESSION[$key] = $value; $_SESSION['formfields'][] = $key; } $_SESSION['formfields'] = NULL; unset($_SESSION['formfields']); ?>
php Syntax (Toggle Plain Text)
<? $_SESSION['formfields'] = array(); foreach($_POST as $key=>$value) { $_SESSION[$key] = $value; $_SESSION['formfields'][] = $key; } ?>
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
•
•
•
•
for multi-select drop downs
php Syntax (Toggle Plain Text)
<select name="selState[]" id="selState"> <option<? echo isset($_SESSION['selState']) && in_array("AZ", $_SESSION['selState'])?' selected="selected"':""; ?> value="AZ">AZ</option> <option<? echo isset($_SESSION['selState']) && in_array("CO", $_SESSION['selState'])?' selected="selected"':""; ?> value="CO">CO</option> <option<? echo isset($_SESSION['selState']) && in_array("AK", $_SESSION['selState'])?' selected="selected"':""; ?> value="AK">AK</option> <option<? echo isset($_SESSION['selState']) && in_array("ME", $_SESSION['selState'])?' selected="selected"':""; ?> value="ME">ME</option> </select>
php Syntax (Toggle Plain Text)
<select multiple="multiple" name="selState[]" id="selState"> <option<? echo isset($_SESSION['selState']) && in_array("AZ", $_SESSION['selState'])?' selected="selected"':""; ?> value="AZ">AZ</option> <option<? echo isset($_SESSION['selState']) && in_array("CO", $_SESSION['selState'])?' selected="selected"':""; ?> value="CO">CO</option> <option<? echo isset($_SESSION['selState']) && in_array("AK", $_SESSION['selState'])?' selected="selected"':""; ?> value="AK">AK</option> <option<? echo isset($_SESSION['selState']) && in_array("ME", $_SESSION['selState'])?' selected="selected"':""; ?> value="ME">ME</option> </select>
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Wow I am posting a lot in this thread.
You can make it easier by condensing it down into a function or class.
then just call it like:
You can make it easier by condensing it down into a function or class.
php Syntax (Toggle Plain Text)
<? function repopulateData($inputtype, $inputname, $testvalue, $originalvalue = "") { switch($inputtype) { case "radio": echo isset($_SESSION[$inputname]) && $_SESSION[$inputname] == $testvalue?' checked="checked"':""; break; case "checkbox": echo isset($_SESSION[$inputname]) && in_array($testvalue, $_SESSION[$inputname])?' checked="checked"':""; break; case "multi-select": echo isset($_SESSION[$inputname]) && in_array($testvalue, $_SESSION[$inputname])?' selected="selected"':""; break; case "select": echo isset($_SESSION[$inputname]) && $_SESSION[$inputname] == $testvalue?' selected="selected"':""; break; case "text": echo isset($_SESSION[$inputname])?$_SESSION['fname']:$originalvalue; break; } } ?>
then just call it like:
php Syntax (Toggle Plain Text)
repopulateData("checkbox", "fieldname", "checkbox value", ""/*for textareas and text boxes*/);
Last edited by R0bb0b; Oct 1st, 2008 at 12:59 am.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
![]() |
Similar Threads
- Project available for PHP Developer (Web Development Job Offers)
- PHP/MySQL Programmer Position (Web Development Job Offers)
- Are you a PHP and Javascript junkie? It's time you met Emma. (Software Development Job Offers)
- PHP MYSQL Developer Position Available (Web Development Job Offers)
- PHP Web Programmer (Post your Resume)
- PHP / My SQL Web developer (Web Development Job Offers)
Other Threads in the PHP Forum
- Previous Thread: Rewrite advice: What is the best solution to this problem?
- Next Thread: Up loading Excel file in PHP
| Thread Tools | Search this Thread |
ajax apache api archive array autocomplete beginner binary broken cakephp checkbox class cms code cron curl database dataentry date display duplicates dynamic echo email emptydisplayvalue error errors execute explodefunction file files firstoptioninphpdroplist folder form forms function functions google href htaccess html image include insert integration ip java javasciptvalidation javascript joomla keywords limit link login loop mail matching menu mlm multiple mysql number oop paypal pdf php problem query radio random recursion recursive regex remote script search server sessions shot sms soap source space sql subscription syntax system table tag tutorial tutorials update upload url validator variable video web xml youtube





