The scenario:
I’ve made a long form with lots of inputs and drop down selects, and I’ve already set it up to check to see if the form has been submitted and come back (due to errors and such) and use php to echo the values back into the appropriate inputs. Something like:
<input type="text" id="firstname" name="firstname" maxlength="60" value="<?php if(isset($_POST['submit'])){ echo trim($_POST['firstname']);} ?>">
once the form is submitted it goes into the database. Now i need to design a way for the user to come back and edit the information again, and rather than build an entirely new page to read the data from the db and echo it when the form is first loaded, then echo out the $_POST data if the form is submitted, but returned with errors, I’d like to use the same form and when the page loads, check the db to see if there are any entries for the current logged in user, and if so, set the $_POST variables to the values in the db.
I’ve written a quick test form and know that this is possible, but was wondering if it was considered improper to set the $_POST variable for this. The test code i wrote looks something like:
$query = "SELECT * FROM userprofiles WHERE uid = '$uid'";
$result = mysql_query($query);
if(mysql_num_rows($result) <= 0)
{
mysql_close(); //no info in db for user. Yet...
}
else
{
$data = mysql_fetch_array($result);
$_POST['submit'] = true;
$_POST['firstname'] = $data['firstname']; //and so on...
This way I’m able to use the same form without having to modify a ton of code, and build a new page.
I do not see any problem in manually setting the post variables to serve your purpose, you may get to some logic problems if you are not paying attention, but in the end they are still variables. I think it is the same as you would save the
$_POSTin another variable first and work with it.The only difference is that
$_POSTis a super global ( http://php.net/manual/en/language.variables.superglobals.php ).