943,718 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 784
  • PHP RSS
Sep 30th, 2008
0

Need Help In PHP

Expand Post »
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.
Reputation Points: 40
Solved Threads: 7
Junior Poster
architact is offline Offline
114 posts
since Apr 2008
Sep 30th, 2008
0

Re: Need Help In PHP

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
Reputation Points: 11
Solved Threads: 2
Junior Poster
einfoway is offline Offline
145 posts
since Nov 2005
Sep 30th, 2008
0

Re: Need Help In PHP

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.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Sep 30th, 2008
0

Re: Need Help In PHP

Thanks guys but problem is I want to do this for a form having 77 fields. Isn't there a simple method.
Reputation Points: 40
Solved Threads: 7
Junior Poster
architact is offline Offline
114 posts
since Apr 2008
Sep 30th, 2008
0

Re: Need Help In PHP

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:
PHP Syntax (Toggle Plain Text)
  1. <language="text/javascript">
  2. function CheckField(field) {
  3. if (field.value == "") {
  4. alert( "Field " + field.name + " is empty" );
  5. form.field.focus();
  6. return false ;
  7. }
  8. return true ;
  9. }
  10. </script>
Then add the following to each field which need validating:
PHP Syntax (Toggle Plain Text)
  1. onsubmit="return checkform(this);"
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'
Last edited by Will Gresham; Sep 30th, 2008 at 3:58 pm.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Sep 30th, 2008
0

Re: Need Help In PHP

Click to Expand / Collapse  Quote originally posted by architact ...
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.
Reputation Points: 11
Solved Threads: 2
Junior Poster
einfoway is offline Offline
145 posts
since Nov 2005
Sep 30th, 2008
0

Re: Need Help In PHP

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:
php Syntax (Toggle Plain Text)
  1. <?
  2. $_SESSION['formfields'] = array();
  3. foreach($_POST as $key=>$value)
  4. {
  5. $_SESSION[$key] = $value;
  6. $_SESSION['formfields'][] = $key;
  7. }
  8. $_SESSION['formfields'] = NULL;
  9. unset($_SESSION['formfields']);
  10. ?>

Then on your form for text fields including text boxes and textareas you consult the following example
php Syntax (Toggle Plain Text)
  1. //text box
  2. <input type="text" name="fname" id="fname" value="<? echo isset($_SESSION['fname'])?$_SESSION['fname']:$originalvalue; ?>" />
  3.  
  4. //text area
  5. <textarea name="description" id="description"><? echo isset($_SESSION['fname'])?$_SESSION['fname']:$originalvalue; ?></textarea>

for select drop downs
php Syntax (Toggle Plain Text)
  1. <select name="selState" id="selState">
  2. <option<? echo isset($_SESSION['selState']) && $_SESSION['selState'] == "AZ"?' selected="selected"':""; ?> value="AZ">AZ</option>
  3. <option<? echo isset($_SESSION['selState']) && $_SESSION['selState'] == "CO"?' selected="selected"':""; ?> value="CO">CO</option>
  4. <option<? echo isset($_SESSION['selState']) && $_SESSION['selState'] == "AK"?' selected="selected"':""; ?> value="AK">AK</option>
  5. <option<? echo isset($_SESSION['selState']) && $_SESSION['selState'] == "ME"?' selected="selected"':""; ?> value="ME">ME</option>
  6. </select>

for multi-select drop downs
php Syntax (Toggle Plain Text)
  1. <select name="selState[]" id="selState">
  2. <option<? echo isset($_SESSION['selState']) && in_array("AZ", $_SESSION['selState'])?' selected="selected"':""; ?> value="AZ">AZ</option>
  3. <option<? echo isset($_SESSION['selState']) && in_array("CO", $_SESSION['selState'])?' selected="selected"':""; ?> value="CO">CO</option>
  4. <option<? echo isset($_SESSION['selState']) && in_array("AK", $_SESSION['selState'])?' selected="selected"':""; ?> value="AK">AK</option>
  5. <option<? echo isset($_SESSION['selState']) && in_array("ME", $_SESSION['selState'])?' selected="selected"':""; ?> value="ME">ME</option>
  6. </select>

for radio buttons and check boxes
php Syntax (Toggle Plain Text)
  1. //check boxes
  2. <input type="checkbox"<? echo isset($_SESSION['chkColors']) && in_array("red", $_SESSION['chkColors'])?' checked="checked"':""; ?> name="chkColors[]" id="chkColorsred" value="red" />&nbsp;<label for="chkColorsred">Red</label><br />
  3. <input type="checkbox"<? echo isset($_SESSION['chkColors']) && in_array("green", $_SESSION['chkColors'])?' checked="checked"':""; ?> name="chkColors[]" id="chkColorsgreen" value="green" />&nbsp;<label for="chkColorsgreen">Green</label><br />
  4. <input type="checkbox"<? echo isset($_SESSION['chkColors']) && in_array("blue", $_SESSION['chkColors'])?' checked="checked"':""; ?> name="chkColors[]" id="chkColorsblue" value="blue" />&nbsp;<label for="chkColorsblue">Blue</label><br />
  5. <input type="checkbox"<? echo isset($_SESSION['chkColors']) && in_array("purple", $_SESSION['chkColors'])?' checked="checked"':""; ?> name="chkColors[]" id="chkColorspurple" value="purple" />&nbsp;<label for="chkColorspurple">Purple</label><br />
  6.  
  7. //radio buttons
  8. <input type="radio"<? echo isset($_SESSION['rdoColors']) && $_SESSION['rdoColors'] == "red"?' checked="checked"':""; ?> name="rdoColors" id="rdoColorsred" value="red" />&nbsp;<label for="rdoColorsred">Red</label><br />
  9. <input type="radio"<? echo isset($_SESSION['rdoColors']) && $_SESSION['rdoColors'] == "green"?' checked="checked"':""; ?> name="rdoColors" id="rdoColorsgreen" value="green" />&nbsp;<label for="rdoColorsgreen">Green</label><br />
  10. <input type="radio"<? echo isset($_SESSION['rdoColors']) && $_SESSION['rdoColors'] == "blue"?' checked="checked"':""; ?> name="rdoColors" id="rdoColorsblue" value="blue" />&nbsp;<label for="rdoColorsblue">Blue</label><br />
  11. <input type="radio"<? echo isset($_SESSION['rdoColors']) && $_SESSION['rdoColors'] == "purple"?' checked="checked"':""; ?> name="rdoColors" id="rdoColorspurple" value="purple" />&nbsp;<label for="rdoColorspurple">Purple</label><br />

On the top of your form you also want to put
php Syntax (Toggle Plain Text)
  1. <? session_start(); ?>

When ever you want to clear the form fields from your session variables just run this code.
php Syntax (Toggle Plain Text)
  1. <?
  2. foreach($_SESSION['formfields'] as $value)
  3. {
  4. $_SESSION[$value] = NULL;
  5. unset($_SESSION[$value]);
  6. }
  7. $_SESSION['formfields'] = NULL;
  8. unset($_SESSION['formfields']);
  9. ?>



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.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Sep 30th, 2008
0

Re: Need Help In PHP

1 correction, this first block:
Click to Expand / Collapse  Quote originally posted by R0bb0b ...
php Syntax (Toggle Plain Text)
  1. <?
  2. $_SESSION['formfields'] = array();
  3. foreach($_POST as $key=>$value)
  4. {
  5. $_SESSION[$key] = $value;
  6. $_SESSION['formfields'][] = $key;
  7. }
  8. $_SESSION['formfields'] = NULL;
  9. unset($_SESSION['formfields']);
  10. ?>
is suppose to be:
php Syntax (Toggle Plain Text)
  1. <?
  2. $_SESSION['formfields'] = array();
  3. foreach($_POST as $key=>$value)
  4. {
  5. $_SESSION[$key] = $value;
  6. $_SESSION['formfields'][] = $key;
  7. }
  8. ?>
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Sep 30th, 2008
0

Re: Need Help In PHP

Click to Expand / Collapse  Quote originally posted by R0bb0b ...
for multi-select drop downs
php Syntax (Toggle Plain Text)
  1. <select name="selState[]" id="selState">
  2. <option<? echo isset($_SESSION['selState']) && in_array("AZ", $_SESSION['selState'])?' selected="selected"':""; ?> value="AZ">AZ</option>
  3. <option<? echo isset($_SESSION['selState']) && in_array("CO", $_SESSION['selState'])?' selected="selected"':""; ?> value="CO">CO</option>
  4. <option<? echo isset($_SESSION['selState']) && in_array("AK", $_SESSION['selState'])?' selected="selected"':""; ?> value="AK">AK</option>
  5. <option<? echo isset($_SESSION['selState']) && in_array("ME", $_SESSION['selState'])?' selected="selected"':""; ?> value="ME">ME</option>
  6. </select>
forgot the multiple="multiple" in the select tag too
php Syntax (Toggle Plain Text)
  1. <select multiple="multiple" name="selState[]" id="selState">
  2. <option<? echo isset($_SESSION['selState']) && in_array("AZ", $_SESSION['selState'])?' selected="selected"':""; ?> value="AZ">AZ</option>
  3. <option<? echo isset($_SESSION['selState']) && in_array("CO", $_SESSION['selState'])?' selected="selected"':""; ?> value="CO">CO</option>
  4. <option<? echo isset($_SESSION['selState']) && in_array("AK", $_SESSION['selState'])?' selected="selected"':""; ?> value="AK">AK</option>
  5. <option<? echo isset($_SESSION['selState']) && in_array("ME", $_SESSION['selState'])?' selected="selected"':""; ?> value="ME">ME</option>
  6. </select>
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 1st, 2008
0

Re: Need Help In PHP

Wow I am posting a lot in this thread.

You can make it easier by condensing it down into a function or class.
php Syntax (Toggle Plain Text)
  1. <?
  2. function repopulateData($inputtype, $inputname, $testvalue, $originalvalue = "")
  3. {
  4. switch($inputtype)
  5. {
  6. case "radio":
  7. echo isset($_SESSION[$inputname]) && $_SESSION[$inputname] == $testvalue?' checked="checked"':"";
  8. break;
  9. case "checkbox":
  10. echo isset($_SESSION[$inputname]) && in_array($testvalue, $_SESSION[$inputname])?' checked="checked"':"";
  11. break;
  12. case "multi-select":
  13. echo isset($_SESSION[$inputname]) && in_array($testvalue, $_SESSION[$inputname])?' selected="selected"':"";
  14. break;
  15. case "select":
  16. echo isset($_SESSION[$inputname]) && $_SESSION[$inputname] == $testvalue?' selected="selected"':"";
  17. break;
  18. case "text":
  19. echo isset($_SESSION[$inputname])?$_SESSION['fname']:$originalvalue;
  20. break;
  21. }
  22. }
  23. ?>

then just call it like:
php Syntax (Toggle Plain Text)
  1. repopulateData("checkbox", "fieldname", "checkbox value", ""/*for textareas and text boxes*/);
Last edited by R0bb0b; Oct 1st, 2008 at 12:59 am.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Rewrite advice: What is the best solution to this problem?
Next Thread in PHP Forum Timeline: Up loading Excel file in PHP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC