phpapprentice Posted November 4, 2008 Share Posted November 4, 2008 I need help in keeping the data into the form. I have a form that validates fields. If a user enter John for firstname field and miss the lastname. The error message that will appear is "Please enter Lastname" and a go back link is also displayed. But when I clicked the go back link, the text that was entered(Jonh-for the firstname) disappeared. How can I keep the data into the form? Please help. Link to comment https://forums.phpfreaks.com/topic/131398-solved-how-to-keep-the-data-that-was-entered-into-a-form/ Share on other sites More sharing options...
rhodesa Posted November 4, 2008 Share Posted November 4, 2008 The only way i know of, is to store the values into the session and then check for them when you are making the form... the other option is to do the validation with JavaScript before the form is submitted Link to comment https://forums.phpfreaks.com/topic/131398-solved-how-to-keep-the-data-that-was-entered-into-a-form/#findComment-682381 Share on other sites More sharing options...
phpapprentice Posted November 4, 2008 Author Share Posted November 4, 2008 How can I do it using session? Link to comment https://forums.phpfreaks.com/topic/131398-solved-how-to-keep-the-data-that-was-entered-into-a-form/#findComment-682385 Share on other sites More sharing options...
DarkWater Posted November 4, 2008 Share Posted November 4, 2008 Don't use sessions for this. Use something like: <?php if (isset($_POST['submit'])) { //do whatever processing, pretend $firstname was set to firstname and $lastname was set to lastname } ?> //display form: <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <input type="text" name="firstname" value="<?php echo (isset($firstname) ? $firstname : ''); ?>" /> <input type="text" name="lastname" value="<?php echo (isset($lastname) ? $lastname : ''); ?>" /> <input type="submit" name="submit" value="Submit" /> </form> Handle the form on the same page. Link to comment https://forums.phpfreaks.com/topic/131398-solved-how-to-keep-the-data-that-was-entered-into-a-form/#findComment-682386 Share on other sites More sharing options...
phpapprentice Posted November 4, 2008 Author Share Posted November 4, 2008 below is my form. where will i add the code in here. the following form has an action='http://sample.work.com/pages/tok/add.php' *********************************************** <html> <body> <form name='test' action='http://sample.work.com/pages/tok/add.php' method='POST' ><br /> <table cellspacing="0" cellpadding="2" border="0" style="width: 415px; height: 356px;"><tbody> <tr><td>Firstname:</td><td><input name="FirstName" /></td></tr> <tr><td>Lastname:</td><td><input name="LastName" /></td></tr> <tr><td>Address</td><td><textarea rows="5" cols="25" name="Address"></textarea></td></tr> <tr><td>City:</td><td><input name="City" /></td></tr> <tr><td>Zip Code</td><td><input name="ZipCode" /></td></tr> <tr><td>Home Phone</td><td><input name="HomePhone" /></td></tr> <tr><td>Work Phone</td><td><input name="WorkPhone" /></td></tr> <tr><td>Cell Phone</td><td><input name="CellPhone" /></td></tr> <tr><td>Email Address</td><td><input name="Email" /></td></tr> <tr><td>Emergency Contact Name:</td><td><input name="EmergencyContactName" /></td></tr> <tr><td>Emergency Contact Telephone:</td><td><input name="EmergencyContactPhone" /></td></tr> <tr><td>Noyce Program at:</td><td><input name="NoyceProgramAt" /></td></tr> </tbody></table> <br /> Who are you? <input type="checkbox" name="person1[]" value="true" />IHE Personnel <input type="checkbox" name="person2[]" value="true"/> K-12 Personnel <input type="checkbox" name="person3[]" value="true"/> Current Noyce Scholar <input type="checkbox" name="person4[]" value="true"/> Noyce Alumnus <input type="checkbox" name="person5[]" value="true"/> Other, EXPLAIN <input name="Other" /> <br /><br /> If Noyce Scholar, are you currently receiving Noyce Funding? <input type="radio" name="ScholarReceivingFunding" value="yes"/>Yes <input type="radio" name="ScholarReceivingFunding" value="no"/>No <br /><br /> Are you <input type="checkbox" name="MathematicsForm[]" value="true"/>Mathematics <input type="checkbox" name="BiologyForm[]" value="true"/>Biology <input type="checkbox" name="ChemistryForm[]" value="true"/>Chemistry <input type="checkbox" name="GeosciencesForm[]" value="true" />Geosciences <input type="checkbox" name="PhysicsForm[]" value="true"/>Physics<br /><br /> Are you <input type="radio" name="status" value="Undergraduate" />Undergraduate <input type="radio" name="status" value="CredentialCandidate" />Credential Candidate <input type="radio" name="status" value="CredentialTeacher" />Credentialed Teacher <br /><br /> If you are currently teaching, please let us know which school and school district in which you are teaching and indicate in what capacity: <br /> School <input name="School" /> District <input name="District" /> <br /><br /> <input type="radio" name="status2" value="StudentTeaching"/>Student Teaching <input type="radio" name="status2" value="InternTeaching"/>Intern Teaching <input type="radio" name="status2" value="CredentialedTeacher"/>Credentialed Teacher <br /><br /> What meals should we reserve for you?<br /> Friday<input type="checkbox" name="FridayDinner[]" value="true" />Dinner <br /> Saturday <input type="checkbox" name="SaturdayBreakfast[]" value="true" />Breakfast <input type="checkbox" name="SaturdayLunch[]" value="true" />Lunch <input type="checkbox" name="SaturdayDinner[]" value="true" />Dinner <br /> Sunday <input type="checkbox" name="SundayBreakfast[]" value="true" />Breakfast <input type="checkbox" name="SundayLunch[]" value="true" />Lunch <br /><br /> Food Request (Click all that apply) <br /> <input type="checkbox" name="Vegetarian[]" value="true" />Vegetarian <input type="checkbox" name="Fish[]" value="true" />Fish <input type="checkbox" name="Chicken[]" value="true" />Chicken <br /> <br /> Two nights of housing are provided by the grant.<br /> Please indicate for which nights you need housing: <br /> <input type="checkbox" name="FridayNightHousing[]" value="true" />Friday Night <input type="checkbox" name="SaturdayNightHousing[]" value="true" />Saturday Night <br /> <br /> Provided housing is at double occupancy.<br /> Single occupancy is possible, for an additional $90/person/2 nights, and is not covered by the grant. <br /> Please check here <input type="checkbox" name="singleroom[]" value="true"/> if you are interested in a single occupancy room. <br /> We cannot guarantee single occupancy rooms until we confirm room availability with the hotel, after all registrations are received. <br /> After confirming with the hotel we will notify you, and will include instructions on where to mail your check. <br /> The check should be received before January 5, 2009.<br /> <br /> Do you have any roommate? If so, please fill out: <br /> Roommate Name:</td><td><input name="RoommateName" /><br /> Roommate Email:</td><td><input name="RoommateEmail" /> <br /> If not, are you <input type="radio" name="Gender" value="male" />Male <input type="radio" name="Gender" value="female" />Female <br /> <br /> Travel: <br /><br /> You will be reimbursed for travel based on the information we emailed to your PI. <br /> Please check with your PI for details. We encourage you to carpool whenever possible. <br /> If you will be reimbursed for car travel, please make sure to bring with you your <br /> <li>driver’s license and</li> <li>proof of automobile insurance</li>with you. <br /> <br /> Please press the submit button below to register <br /> <br /><br /> <input type="submit" name="submit" value="Submit Form!" /> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/131398-solved-how-to-keep-the-data-that-was-entered-into-a-form/#findComment-682395 Share on other sites More sharing options...
phpapprentice Posted November 4, 2008 Author Share Posted November 4, 2008 I used the following code: It works, but the text Firstname is displaying in the textbox. How can I change it. What I want is to enter data into the textbox and keep that data <?php if (isset($_POST['submit'])) { $FirstName = FirstName; //do whatever processing, pretend $firstname was set to firstname and $lastname was set to lastname } ?> //display form: <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <input type="text" name="FirstName" value="<?php echo (isset($FirstName) ? $FirstName : ''); ?>" /> <input type="text" name="lastname" value="<?php echo (isset($LastName) ? $LastName : ''); ?>" /> <input type="submit" name="submit" value="Submit" /> </form> Link to comment https://forums.phpfreaks.com/topic/131398-solved-how-to-keep-the-data-that-was-entered-into-a-form/#findComment-682460 Share on other sites More sharing options...
DarkWater Posted November 4, 2008 Share Posted November 4, 2008 Change that 3rd line to: $FirstName = $_POST['FirstName']; Link to comment https://forums.phpfreaks.com/topic/131398-solved-how-to-keep-the-data-that-was-entered-into-a-form/#findComment-682461 Share on other sites More sharing options...
phpapprentice Posted November 4, 2008 Author Share Posted November 4, 2008 wow that was awesome. thank you very much. appreciate it. thanks Link to comment https://forums.phpfreaks.com/topic/131398-solved-how-to-keep-the-data-that-was-entered-into-a-form/#findComment-682463 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.