thesaleboat Posted May 12, 2009 Share Posted May 12, 2009 Ok, so I have a form for employee information, and it gets stored in the database after they submit their info... I need to be able to repopulate that info back into the form so that they can update it if they need to, how do I go about doing that? Please help ! Here is the mysql query for the employee's info... $employee_info = mysql_query("SELECT * FROM `employee_info` WHERE `employee_info`.`myusername` = '$myusername'") or die(mysql_error()); and here is a segment of code from the form itself... <input type="text" name="hire_date" id="hire_date" value="<?php echo isset($_POST['hire_date']) ? $_POST['hire_date'] : '';?>" size="34" maxlength="50" /> Quote Link to comment https://forums.phpfreaks.com/topic/157839-solved-how-to-repopulate-form-fields/ Share on other sites More sharing options...
allworknoplay Posted May 12, 2009 Share Posted May 12, 2009 You don't want to be using POST arrays to populate the form fields, you want to use the results from the SQL query.. <input type="text" name="hire_date" id="hire_date" value="<?php echo $rows['hire_date']; ?>" size="34" maxlength="50" /> Quote Link to comment https://forums.phpfreaks.com/topic/157839-solved-how-to-repopulate-form-fields/#findComment-832518 Share on other sites More sharing options...
JonnoTheDev Posted May 12, 2009 Share Posted May 12, 2009 Not quite. You may want the post value there if there are form errors <input type="text" name="hire_date" id="hire_date" value="<?php echo isset($_POST['hire_date']) ? $_POST['hire_date'] : $rows['hire_date'];?>" size="34" maxlength="50" /> Quote Link to comment https://forums.phpfreaks.com/topic/157839-solved-how-to-repopulate-form-fields/#findComment-832523 Share on other sites More sharing options...
thesaleboat Posted May 12, 2009 Author Share Posted May 12, 2009 Thats just what i was about to ask... thanks guys for the help! Quote Link to comment https://forums.phpfreaks.com/topic/157839-solved-how-to-repopulate-form-fields/#findComment-832529 Share on other sites More sharing options...
radi8 Posted May 12, 2009 Share Posted May 12, 2009 You are using the $_POST variable incorrectly. This element has no value until the form is processed or reloaded by a form event (i.e. pressing a button to process the data). With this type of processing, you appear to be using 2 views: initial population of the form elements and then processed form element after changes made. In order to facilitate this, I have had to use a standard page variable and then push the value from the applicable source (database fetch or $_POST) into that variable to be displayed. Also, I am assuming that the user name is being populated on the page via a url such as: www.mypage.com/index.php?username=myusername, or as a $_SESSION variable as so: <?php if(isset($_POST['myusername'] && !empty($_POST['myusername']){ $user = $_POST['myusername']; $hiredate = $_POST['hire_date']; } else{ $user = $_GET['myusername']; // Or if using $_SESSION variables, the code is $user = $_SESSION['myusername']; $hiredate=''; } ?> Now do the db stuff and populate the next form variable (hiredate): <?php $employee_info = mysql_query("SELECT * FROM `employee_info` WHERE `employee_info`.`myusername` = '$myusername'") or die(mysql_error()); // retrieve the array $empl = mysql_fetch_row($employee_info); $hiredate=$empl[1]; ?> <?php <input type="text" name="hire_date" id="hire_date" value="<?php echo strlen($hire_date)>0 ? $hire_date : '';?>" size="34" maxlength="50" /> ?> Quote Link to comment https://forums.phpfreaks.com/topic/157839-solved-how-to-repopulate-form-fields/#findComment-832540 Share on other sites More sharing options...
thesaleboat Posted May 12, 2009 Author Share Posted May 12, 2009 Yes I am using session variables to apply myusername to that user. Question... so that code will have to be placed before every form variable?and concerning the last piece of code you put in, I don't think you can have <?php within another like so... <?php <?php ?> ?> Are you storing the rows in an array so they can be selected in order? Quote Link to comment https://forums.phpfreaks.com/topic/157839-solved-how-to-repopulate-form-fields/#findComment-832593 Share on other sites More sharing options...
JonnoTheDev Posted May 12, 2009 Share Posted May 12, 2009 The above code is not needed at all. 2 simple facts 1. If you are editing data from the database then the field will be prefilled with the database value 2. If the user resubmits the data and there is a form error you want the value of the $_POST array to remain in the field value rather than the database value. The initial shorthand if statement does just this Quote Link to comment https://forums.phpfreaks.com/topic/157839-solved-how-to-repopulate-form-fields/#findComment-832796 Share on other sites More sharing options...
thesaleboat Posted May 13, 2009 Author Share Posted May 13, 2009 Thanks neil, but I have another question for you, how does : $rows['state'] work? Do I need to assign rows and then use them as you have said, or do i need to create a global variable rows or something? It's not working is the reason i ask, do I need to place php code above the form so that I can use the shorthand if statements you showed me? Quote Link to comment https://forums.phpfreaks.com/topic/157839-solved-how-to-repopulate-form-fields/#findComment-833282 Share on other sites More sharing options...
JonnoTheDev Posted May 13, 2009 Share Posted May 13, 2009 This comes from your SQL query so: $employee_info = mysql_query("SELECT * FROM employee_info WHERE employee_info.myusername = '$myusername'") or die(mysql_error()); $rows = mysql_fetch_assoc($employee_info); // you can then use the result set i.e print $rows['state']; Quote Link to comment https://forums.phpfreaks.com/topic/157839-solved-how-to-repopulate-form-fields/#findComment-833286 Share on other sites More sharing options...
thesaleboat Posted May 14, 2009 Author Share Posted May 14, 2009 Works perfectly! thanks neil Quote Link to comment https://forums.phpfreaks.com/topic/157839-solved-how-to-repopulate-form-fields/#findComment-834178 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.