Linking Checkbox value to a table record in PHP
Out of the seven days in a week we will display all the days and a checkbox for each day. User has to submit the choice of the days to the system. Next time when they visit then the same checked day status will be displayed and user can check or uncheck any days.
In a hotel you can display a menu to the guest and ask them to select their favorite dishes. We can save the user choice and display the selected items to the same guest in his next visit. Guest can alter the choice and update the record.
Here we will try with the code for selecting weekdays from a days list of checkboxes. We will select the days and on update the record will be stored in a table. While displaying the weekdays we will display by taking the values from a table.
Inside our days field we store the selection with day number separated by comma. For example we have kept Monday, Wednesday, Saturday as selection of userid smo then the field days will store value 1,3,6 . So let us try to collect this value and create an array.
You can understand that we have used split command to create an array which stores the day selection of userid smo. Now we will collect all the weekdays and its numbers from the table plus_weekdays and display then as checkboxes. Here before displaying we will do a small check to know whether this weekday number is there inside our $day_array or not by using in_array function. This is to know whether the weekday is previously selected by user or not. If selected previously then we will keep the checkbox checked by adding attribute checked inside the checkbox tag. For this we will use one variable $st which we will assign empty string or checked value by checking the presence inside array using in_array function. Here is the complete code.
As we have used two mysql tables.
The sql dump of the tables along with the config file and readme file is kept inside the zip file for you to download..
Subscribe to our YouTube Channel here
This article is written by plus2net.com team. https://www.plus2net.com
|▼ Web Forms & handling data in PHP|