PHP Update Form

In PHP Update Form post, I would explain how you can display an HTML form that is already filled or populated. We often need it to update some previous data. Lets first see the code, then we would move to some points that you must understand about creating update form in php:

PHP Update Form

// Lets assume following are the earlier values that we want to update
$password = 'mypassword';
$credit_card = 'Visa';
$city = 'Lahore';
$subjects = array("Algorithms", "OOP");
$languages = array("Java", "PHP");

// To populate these values in form, we must set these some attribute of form input controls, see below

Update Student Info.

/> Algorithms
/> Data Structures

/>Master Card
/>American Express

Analyse following points:
  • For text, password and textarea fields, we only need to set 'value' attribute of form input tag to display it as populated. In above code, we did it for 'name' and 'passowrd' field. (Its not good practice to display password as populated in real applications.)
  • The case differs for radio buttons, checkboxes, and option list. To display a radio button or checkbox already selected, we must write 'checked="checked"' attribute. So to display an earlier data, we must add some PHP code with each radio button to determine what was the previous value e.g. Visa, so that we can render 'checked="checked"' for that radio button. In case of checkboxes, as multiple selection is allowed, so we have used an array to check whether a particular element exist inside the array. The 'subjects' array contains two elements 'Algorithms' and 'OOP'. So these two checkboxes would be displayed as checked.
Here is the output generated by above HTML/PHP code: