Retrieving checkboxes values from mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
batowiise
Forum Commoner
Posts: 28
Joined: Fri Dec 17, 2010 7:11 am

Retrieving checkboxes values from mysql

Post by batowiise »

Hi All,

I have been able to insert a checkbox value into my database. The problem i am now facing is how to retrieve the value of the checkbox in the form of a TICK.

Your help will be much appreciated.

Regards.
sockpuppet
Forum Newbie
Posts: 22
Joined: Tue Jan 18, 2011 8:38 am

Re: Retrieving checkboxes values from mysql

Post by sockpuppet »

Use an IF statement.

if the value of the tick box is what is in the database then echo "checked" into the <input> html.
batowiise
Forum Commoner
Posts: 28
Joined: Fri Dec 17, 2010 7:11 am

Re: Retrieving checkboxes values from mysql

Post by batowiise »

This is my sample code and its not working.

Code: Select all

$sql =  "SELECT * FROM samoa WHERE samoa.entryid = '$entryid'"; 

$result = mysql_query($sql);

if ($row = @mysql_fetch_array($result, MYSQL_ASSOC)) {  
        
        print "<form name=\"formname\" method=\"post\" action=\"$_SERVER[PHP_SELF]\">"; 

 echo" <table width=\"881\" border=\"1\" align=\"center\">
  <tr>
      <td colspan=\"1\">Status:</td>> 
      <td><input name=\"status\" id=\"status\" value=\"$row[status]\" READONLY/></td>
  </tr>
  <tr>
      <td><input type=\"checkbox\" name=\"final\" [b]value=\"final\" [/b]id=\"final\" CHECKED/></td>
  </tr>
</table>";
can you please help me out.

regards.
sockpuppet
Forum Newbie
Posts: 22
Joined: Tue Jan 18, 2011 8:38 am

Re: Retrieving checkboxes values from mysql

Post by sockpuppet »

Ok, first use ' with strings it will mean that you don't have to escape each " with a \ when writing html.

This should work, buts its untested.
batowiise wrote:This is my sample code and its not working.

Code: Select all

$sql =  "SELECT * FROM samoa WHERE samoa.entryid = '$entryid'"; 

$result = mysql_query($sql);

if ($row = @mysql_fetch_array($result, MYSQL_ASSOC)) {  
        
        print "<form name=\"formname\" method=\"post\" action=\"$_SERVER[PHP_SELF]\">"; 

 echo" <table width=\"881\" border=\"1\" align=\"center\">
  <tr>
      <td colspan=\"1\">Status:</td>> 
      <td><input name=\"status\" id=\"status\" value=\"$row[status]\" READONLY/></td>
  </tr>
  <tr>";

if($row['final'] == "final") { 
     $checked = "CHECKED"; 
} else { 
     $checked = ""; 
}

print "<td><input type=\"checkbox\" name=\"final\" value=\"final\" id=\"final\" " . $checked . "/></td>
  </tr>
</table>";
can you please help me out.

regards.
batowiise
Forum Commoner
Posts: 28
Joined: Fri Dec 17, 2010 7:11 am

Re: Retrieving checkboxes values from mysql

Post by batowiise »

That was a cracker. It did solve my problem. Thanks.
Post Reply