How do php checkboxes work with a sql database?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
tsp003
Forum Newbie
Posts: 16
Joined: Thu Apr 21, 2011 1:39 am

How do php checkboxes work with a sql database?

Post by tsp003 »

Hi guys, i'm very much a noob who's having trouble with my checkboxes in the following form.

This form obviously creates three checkboxes, but what I need is to store whatever is checked in the database and display it on the page. I've read many tutorials about checkboxes, but i've not been able to make anything work with my current code.

The checkbox code

Code: Select all

<tr>
                                  <td height="39" class="key">
                                                <label for="name">
                                                        <strong><?php echo JText::_('CHECK BOX 1'); ?></strong>:
                                                </label>
                                  </td>
                                  <td>Please check box 1</td>
                                  <td>
                  <input type="checkbox" <? echo $strIsChecked ?> name="checkbox_1"></td>
                          </tr>
                          <tr>
                                  <td height="103" class="key"><strong><?php echo JText::_('CHECK BOX 2'); ?></strong></td>
                                  <td>Please check box 2</td>
                                  <td><input type="checkbox" name="checkbox_2" id="checkbox_2" /></td>
                          </tr>
                                <tr>
                                  <td class="key"><strong><?php echo JText::_('CHECKBOX 3'); ?></strong></td>
                                  <td>Please check box 3</td>
                                  <td><input type="checkbox" name="checkbox_3" id="checkbox_3" /></td>
                          </tr>
                                <tr>
The full form code

Code: Select all

<form action="index.php" method="post" name="regJobSeeker" enctype="multipart/form-data">

		<div class="col width-60">
			<fieldset class="tpj_fieldset">
				<legend><?php echo JText::_('USER INFORMATION'); ?></legend>

				<table class="admintable">
				<tr>
					<td class="key">
						<label for="name">
							<?php echo JText::_('USER NAME'); ?> : 
						</label>
					</td>
					<td>
						<strong><?php echo $user->name; ?></strong> - 	<?php
		if(!empty($row->id))
		{
			$link = JRoute::_('index.php?option=com_user&view=user&task=edit');
			?>
			<a href="<?php echo $link; ?>"><?php echo JText::_('CHANGE PASSWORD OR EMAIL'); ?></a>
			<?php
		}
		?>
		
					</td>
				</tr>		
			    </table>
			</fieldset>
			</div>
			
			<div class="col width-60">
			<fieldset class="tpj_fieldset">
				<legend><?php echo JText::_('EXPERIENCE / EDUCATION'); ?></legend>

				<table class="admintable">
				<tr>
					<td class="key">
						<label for="name">
							<strong><?php echo JText::_('CURRENT POSITION'); ?></strong>:
						</label>
					</td>
					<td>
						<input class="inputbox" type="text" name="current_position" id="current_position" size="60" maxlength="100" value="<?php echo $row->current_position; ?>" />
					</td>
				</tr>
				
				<tr>
					<td class="key">
						<label for="name">
							<strong><?php echo JText::_('MAJOR'); ?></strong>:
						</label>
					</td>
					<td>
						<?php 
						$list_major = getSelectMajor('id_major',$row->id_major,'');
						echo $list_major;
						?>
					</td>
				</tr>
				
				<tr>
					<td class="key">
						<label for="name">
							<strong><?php echo JText::_('DEGREE LEVEL'); ?></strong>:
						</label>
					</td>
					<td>
					<?php 
						$list_degree_level = getSelectDegreeLevel('id_degree_level',$row->id_degree_level,'');
						echo $list_degree_level;
					?>	
						
					</td>
				</tr>										
			    </table>
			</fieldset>
			</div>

			
		<div class="col width-60">
		  <fieldset class="tpj_fieldset">
			<legend><?php echo JText::_('DESIRED EMPLOYMENT'); ?></legend>

			<table class="admintable">
		<tr>
			<td width=150 height="28" class="key">
						<label for="name">
							<strong><?php echo JText::_('PRIMARY INDUSTRY'); ?></strong>:
						</label>
					</td>
					<td>
						<?php 
						$list_primary_industry = getSelectIndustry('id_industry1',$row->id_industry1,'');	   					   	echo $list_primary_industry;					
						?>
					</td>
					<td>&nbsp;</td>
					</tr>
				<tr>
					<td height="29" class="key">
						<label for="name">
							<strong><?php echo JText::_('SECONDARY INDUSTRY'); ?></strong>:
						</label>
					</td>
					<td>
						<?php 
						$list_secondary_industry = getSelectIndustry('id_industry2',$row->id_industry2,'');	   					   	echo $list_secondary_industry;					
						?>
					</td>
					<td>&nbsp;</td>
				</tr>
					
				<tr>
					<td height="31" class="key">
						<label for="name">
							<strong><?php echo JText::_('POSITION TYPE'); ?></strong>:
						</label>
					</td>
					<td>
						<?php 
						$list_position_type = getSelectPositionType('id_pos_type',$row->id_pos_type,'');	   					   						echo $list_position_type;					
						?>
					</td>
					<td>&nbsp;</td>
					</tr>
				
					<tr>
					<td height="33" class="key">
						<label for="name">
							<strong><?php echo JText::_('MINIMUM SALARY'); ?></strong>:
						</label>
					</td>
					<td nowrap="nowrap">
						
					    <input class="inputbox" type="text" name="min_salary" id="min_salary" size="40" maxlength="100" value="<?php echo $row->min_salary; ?>" />
						<?php 
						$list_salary_type = getSelectTypeSalary('id_type_salary',$row->id_type_salary,'');	   					   						echo $list_salary_type;					
						?>
					</td>
					<td nowrap="nowrap">&nbsp;</td>
			    </tr>
				
				<tr>
					<td height="33" class="key">
						<label for="name">
							<strong><?php echo JText::_('IN CURRENCY'); ?></strong>:
						</label>
					</td>
					<td nowrap="nowrap">
						
						<input class="inputbox" type="text" name="currency_salary" id="currency_salary" size="10" maxlength="100" value="<?php echo $row->currency_salary; ?>" />
					    
						
					</td>
					<td nowrap="nowrap">&nbsp;</td>
					</tr>
				<tr>
					<td height="37"td class="key">
						<label for="name">
							<strong><?php echo JText::_('UPLOAD FOTO'); ?></strong>:
						</label>
					</td>
					<td nowrap="nowrap">
					  <?php
						$db =& JFactory::getDBO();
						$switch = whichUse($user->id);
						switch($switch)
						{
							case 1:
								$query = "SELECT avatar FROM #__comprofiler WHERE avatarapproved='1' AND user_id='$row->user_id'";
								$db->setQuery($query);
								$i = $db->loadResult();
								if($i)
								{
									$img = JPATH_SITE.DS.'images'.DS.'comprofiler'.DS.$i;
									$pimg = JURI::base().'images/comprofiler/'.$i.'?'.time();
								}
								else
								{
									$img = JPATH_SITE.DS.'images'.DS.'tpjobs'.DS.$row->user_id.'.jpg';
									$pimg = JURI::base().'images/tpjobs/'.$row->user_id.'.jpg?'.time();
								}
							break;
							case 2:
								$query = "SELECT avatar FROM #__community_users WHERE userid='$row->user_id'";
								$db->setQuery($query);
								$i = $db->loadResult();
								if($i)
								{
									$img = JPATH_SITE.DS.$i;
									$pimg = JURI::base().$i.'?'.time();
								}
								else
								{
									$img = JPATH_SITE.DS.'images'.DS.'tpjobs'.DS.$row->user_id.'.jpg';
									$pimg = JURI::base().'images/tpjobs/'.$row->user_id.'.jpg?'.time();
								}
							break;
							default:
								$img = JPATH_SITE.DS.'images'.DS.'tpjobs'.DS.$row->user_id.'.jpg';
								$pimg = JURI::base().'images/tpjobs/'.$row->user_id.'.jpg?'.time();
							break;
						}

						if(file_exists($img)) {
							echo '<img src="'.$pimg.'">';
						}
						else if ($row->id)
						{
							$img = JURI::base().'components/com_tpjobs/images/nophoto.gif';
							echo '<img src="'.$img.'">';
						}
						?>
						<input type="file" name="photo" class="inputbox" />
					</td>
					<td nowrap="nowrap">&nbsp;</td>
				</tr>
				<tr>
				  <td class="key">&nbsp;</td>
				  <td nowrap="nowrap">&nbsp;</td>
				  <td nowrap="nowrap">&nbsp;</td>
				  </tr>
				<tr>
				  <td height="39" class="key">
						<label for="name">
							<strong><?php echo JText::_('CHECK BOX 1'); ?></strong>:
						</label>
				  </td>
				  <td>Please check box 1</td>
				  <td>
                  <input type="checkbox" <? echo $strIsChecked ?> name="checkbox_1"></td>
			  </tr>
			  <tr>
				  <td height="103" class="key"><strong><?php echo JText::_('CHECK BOX 2'); ?></strong></td>
				  <td>Please check box 2</td>
				  <td><input type="checkbox" name="checkbox_2" id="checkbox_2" /></td>
			  </tr>
				<tr>
				  <td class="key"><strong><?php echo JText::_('CHECKBOX 3'); ?></strong></td>
				  <td>Please check box 3</td>
				  <td><input type="checkbox" name="checkbox_3" id="checkbox_3" /></td>
			  </tr>
				<tr>
				  <td class="key">
						<label for="name">
						</label>
				  <td>&nbsp;</td>
				  <td>&nbsp;</td>
				  </tr>
				</table>
		  </fieldset>
			</div>

			<?php HTML_front_tpjobs::showCustom($custom, $row->user_id); ?>
			
				<?php if($row->id > 0){?>
				<input type="button"  value="<?php echo JText::_('EDIT PROFILE BTN'); ?>" onclick="javascript:validateregJobSeeker();"  class="button"/>
			<?php }else{ ?>
				<?php
				$config = & JComponentHelper::getParams('com_tpjobs');	
				$cv_review=$_POST[cv_review]; $sql="INSERT INTO #_tpjobs_jobseeker (cv_review) VALUES('$cv_review')";
				$termid = $config->get('termarticleid');
				$link = JRoute::_("index.php?option=com_content&view=article&id=".$termid);
				?>
				<p>By clicking on 'I accept' below you are agreeing to our <a href="<?php echo $link; ?>" target="_blank">Terms and Conditions</a></p>
				<input type="button"  value="<?php echo JText::_('SET AS JOBSEEKER'); ?>" onclick="javascript:validateregJobSeeker();"  class="button"/>
			<?php } ?>
			
			<input type="hidden" name="option" value="<?php echo $option; ?>" />
			<input type="hidden" name="task" value="savejobseeker" />
			<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
			
			<input type="hidden" name="boxchecked" value="0" />
		    <?php echo JHTML::_('form.token'); ?>
</form>
All i understand is that checkboxes are not as straighforward as they appear, i've read in some articles that they need to be already checked to start with and i'm complete lost when it comes to the submit button.

Could someone explain to me - in simple terms - how checkboxes in php work with a sql database, i'm completely lost.

Thanks in advance
Last edited by tsp003 on Tue Aug 30, 2011 4:20 am, edited 1 time in total.
User avatar
getmizanur
Forum Commoner
Posts: 71
Joined: Sun Sep 06, 2009 12:28 pm

Re: ow do php checkboxes work with a sql database?

Post by getmizanur »

are using joomla?
tsp003
Forum Newbie
Posts: 16
Joined: Thu Apr 21, 2011 1:39 am

Re: How do php checkboxes work with a sql database?

Post by tsp003 »

Sorry! Should have said earlier, yes I am using Joomla :)
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Re: How do php checkboxes work with a sql database?

Post by ok »

Checkboxes are HTML form fields. They, themselves, have nothing to do with PHP.

In short. Checkboxes are placed inside an HTML form. When the form is submitted to your PHP page, all the form data is placed inside two main variables:
$_GET - For GET requests
$_POST - For POST requests

So, lets say you have a simple form like this:

Code: Select all

<form action="http://localhost/myphp.php" method="get">
  <input type="checkbox" name="checkbox_2" id="checkbox_2" />
  <input type="submit" value="Submit!" />
</form>
Then after you submit this form to your PHP page, you will get the following data in $_GET when the checkbox isn't checked:

Code: Select all

array(0) { }
And this when it is checked:

Code: Select all

array(1) { ["checkbox_2"]=> string(2) "on" }
Based on this, you can update the DB.

I think Joomla does this for you, so you might want to search in the help for Joomla.
Post Reply