Jump to content

Fill A Html Form With Values From A Php Script


Guest Magnus
 Share

Recommended Posts

Guest Magnus

Just started with the PHP language, so my php knowledge is very limited.I have a small login form say frmLogInSmall.html with input fields for Firstname, Lastname and Password. Furthermore there are 3 radiobuttons Insert, Update and Delete.The form's submit calls a php script UserOptions.php.The script validates the radiobutton pressed (Insert,Update,Delete) and shall take appropriate actions.In all 3 situations the user will be directed to a new and more detailed login form frmLogInBig "header ('Location: http://www.frmLogInBig.html)" but on entering the new form the action to takeshall be different.Choosing Insertthe script activates the detailed form and all the input fields are blank. That works fine.So far so good.Choosing Update or Deletethe script also activates the detailed form, but before it connects to the MySQL database and retrieves the user from a table andreturns a result set with user data which i put in a variable $row= mysql_fetch_array($result) in the script. I can access all the fields in the variable $row with the $row[FieldName_1], $row[FieldName_2]etc. So far it works. What i want to do is quiet simply to fill out the fields of the form with the retrieved data so when the detailed form displayes,the input fields of the form are filled from my variables in the script.I have no idea of know how to do it.Hope someone can assist.Best regardsMagnusthe script UserOptions.php looks something like this<?php/*----------------------------------------------------------------- *//* Opens the connection to the database *//*----------------------------------------------------------------- */include('ConnectMySQL.php');$UserPassword=$_POST[Password];if ($_POST[userAction]=="Insert") { header('Location: http://www.frmLogInBig.html); #Opens the page form with all the input fields blank. }else { #also to open the same form but the fields have values retrieved from database header('Location: http://www.frmLogInBig.html); $row=FunctionGetPerson($USerPassword);//The function returns a result set and assigns to variable $row # $row[FieldName_1] How do i assign the value to the relevant field in the form ? # $row[FieldName_2] How do i assign the value to the relevant field in the form ? }?>

Edited by Magnus
Link to comment
Share on other sites

Instead of having two different locations to redirect the user to, two 'versions' of the same big login form, codense all of the form handling into one. An idea I found very useful when first learning PHP was to construct relevant portions of an HTML block in the php part of a page and then simply output it in a skeleton HTML page. So, when you hit submit on your first page, the form action sends you to the second login handler.If you name your radio buttons, you can check which one is checked:

if($_POST["insert"]) {create text block or redirect for new user, like this:$display_block = "<p>Please fill out the rest of the registration form:</p><br /><form method=\"POST\" action=\"".$_SERVER["PHP_SELF"]."\">< - form fields, each named -></form>"Then check if the user is seeing this page because they are new and finishing registration.  First, check for required fields:} else if((!$_POST["new required form field"]) || (!$_POST["new required form field 2"]) etc.) {redirect them back to this form and try again:header("Location:".$_SERVER["PHP_SELF"]."); } else if(($_POST["new required form field"]) && (!$_POST["new required form field 2"]) etc.) {all fields from new form are here, so process it in this bit, and create the relevant $display_block} else if($_POST["update"]) {create display block here} else if($_POST["delete"]) {deal with that here}?><html><?php echo $display_block; ?></html>

Posting the form to itself is the easiest way of checking required fields. Just an idea.The other way to have the forms prefilled out is just to name your fields and they will be ready in the GET or POST array (use POST for login info). So, if you have a field called username, it'll be available in your next script as $_POST["username"] and you can just drop that directly into your new HTML form:<form method="POST" action="register.php"><input type="text" name="username" value="<?php echo '$_POST["username"]'; ?> />

Edited by chibineku
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...