Need Help In PHP

Reply

Join Date: Apr 2008
Posts: 109
Reputation: architact is an unknown quantity at this point 
Solved Threads: 7
architact's Avatar
architact architact is offline Offline
Junior Poster

Need Help In PHP

 
0
  #1
Sep 30th, 2008
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...
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 142
Reputation: einfoway is an unknown quantity at this point 
Solved Threads: 2
einfoway's Avatar
einfoway einfoway is offline Offline
Junior Poster

Re: Need Help In PHP

 
0
  #2
Sep 30th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: Need Help In PHP

 
0
  #3
Sep 30th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 109
Reputation: architact is an unknown quantity at this point 
Solved Threads: 7
architact's Avatar
architact architact is offline Offline
Junior Poster

Re: Need Help In PHP

 
0
  #4
Sep 30th, 2008
Thanks guys but problem is I want to do this for a form having 77 fields. Isn't there a simple method.
If you think we fight for money and you fight for honor, then remember everyone fights for the thing they don't have...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: Need Help In PHP

 
0
  #5
Sep 30th, 2008
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:
  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:
  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.
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 142
Reputation: einfoway is an unknown quantity at this point 
Solved Threads: 2
einfoway's Avatar
einfoway einfoway is offline Offline
Junior Poster

Re: Need Help In PHP

 
0
  #6
Sep 30th, 2008
Originally Posted by architact View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Need Help In PHP

 
0
  #7
Sep 30th, 2008
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:
  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
  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
  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
  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
  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
  1. <? session_start(); ?>

When ever you want to clear the form fields from your session variables just run this code.
  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.
“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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Need Help In PHP

 
0
  #8
Sep 30th, 2008
1 correction, this first block:
Originally Posted by R0bb0b View Post
  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:
  1. <?
  2. $_SESSION['formfields'] = array();
  3. foreach($_POST as $key=>$value)
  4. {
  5. $_SESSION[$key] = $value;
  6. $_SESSION['formfields'][] = $key;
  7. }
  8. ?>
“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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Need Help In PHP

 
0
  #9
Sep 30th, 2008
Originally Posted by R0bb0b View Post
for multi-select drop downs
  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
  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>
“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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Need Help In PHP

 
0
  #10
Oct 1st, 2008
Wow I am posting a lot in this thread.

You can make it easier by condensing it down into a function or class.
  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:
  1. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC