php - How to pre-populate a web form with data from a database -
i have form (actually 4 different forms on 4 consecutive pages, same effect) users fill in sign website. there many (100+) items in form, including text boxes, ranges, checkboxes.
once user has signed want them able edit preferences via simple 'edit' link on custom homepage. idea shown sign-up forms again, time pre-populated using data linked user account database.
is simple 'edit' link in question containing post value pass form pages:
http://www.mysite.com/something.php?edit=true
and checking value in php on form page, , setting values in each form item if edit true
?
the reason im double-checking on because seems awful lot of code go through each item in form , set value whatever in database , dont want find out later down line there quicker , less programming intensive way of doing this!
in case helps find alternative solution, im using pdo, user identified way of session variable containing (unique) email address, , form names identical database column names.
i put form inside separate php file can reused different php scripts.
inside separate php file set input values post variables.
i.e. (for example we'll call file myform.php)
<form action="<?php echo htmlspecialchars($_server['request_uri'], ent_quotes); ?>" method="post"> <input type="text" name="first_name" value="<?php echo @$_post['first_name']; ?>" > <input type="text" name="last_name" value="<?php echo @$_post['last_name']; ?>" > <input type="text" name="username" value="<?php echo @$_post['username']; ?>" > <input type="submit" name="submit" value="<?php echo $submit_value; ?>" > </form>
and on php side edit page have (this assumes db fields match input field names)
<?php foreach($db_row_fields $field_key => $field_value) { $_post[$field_key] = $field_value; } $submit_value = "update"; include 'myform.php';
obviously fields need out of db.
for sign need set $submit_value want post variables empty
$submit_value = "sign up"; include 'myform.php';
while using 2 different php scripts handle sign , editing make validation , form processing code reusable possible.
Comments
Post a Comment