Jump to content

Checkbox used to update DB... Best choice?


Grant Holmes

Recommended Posts

Ima Noobie, so really appreciate any/all help!

 

I've searched this forum and googled a bunch. I don't seem to find exactly what I need, or don't think the answer fits, so if this has been answered 200 times, I apologize in advance.

===

 

I'm successfully displaying the status of a TINYINT field (1 or 0) named "Active", which I want to use as an "active" or "inactive" status and that display is successfully checking a box: [this section part of a row loop]

<?php

// Set the variable $foo from the database or from form data.

$checked = $Active ? "checked" : "";

echo "<input type=\"checkbox\" name=\"foo\" $checked>";

?>

 </td>

<td valign="top" width="150">

<input name="Active[<?php echo $row['id'];?>]" id="checkbox[<?php echo $row['id'];?>]" type="checkbox" value="<? echo $row['id']; ?>">

</TD>

</tr>

<?

++$i;

}

echo "</table>";

?>

============================

So, when displayed, checked is "1", unchecked is "0". I want to be able to then check or uncheck that box to change status and save the change. Do I have to use a separate form or page? Seems I should be able to this here. This status on this screen is the ONLY part of the record I wish to change.

 

I hope this is explained well. Iguess the question is then two-fold. Can I do the above, using a checkbox to update the db, or would it be better/easier to use a radio button?

Link to comment
Share on other sites

A checkbox will work just as well as any other form input. If the checkbox is clicked, it will have a value of "on" if not, it will not even be submitted to php as being a set variable. So, you can just check if it exists to decide whether or not to make a change in the db.

Link to comment
Share on other sites

I will be honest and come clean here. I am not gifted in things requiring high detail and intricacy. Thus, you could easily extrapolate that I basically suck at writing code. When I see stuff others have written, I understand the simpler stuff. I have read tutorials and waded into books only to put them down 20 minutes later totally confused. I equate this with BARELY passing algebra at ANY level I've ever taken it.

 

Sooooo.... what I'm using is snipets of stuff I've had written for various projects, part of a PHP tutorial over there, and some lines "borrowed" from other sources. So, if you told me to write a script from scratch to access a DB and show ONE line from that, or shoot me. Well, get the gun.

 

That said, I'm totally out of my element and at this moment can't afford to hire a pro do it quickly. :-( Sigh.

 

How's that for coming clean? TMI?

Link to comment
Share on other sites

<?php
if (isset($_POST['bar']))
{
   //Then form was submitted.
   if (isset($_POST['foo']))
      //checkbox is checked. 
   else
      //checkbox isn't checked.
}
// Set the variable $foo from the database or from form data.
$checked = $Active ? "checked" : "";
echo "<form method=POST><input type=\"checkbox\" name=\"foo\" $checked>";
?>
 </td>
<td valign="top" width="150">
<input name="Active[<?php echo $row['id'];?>]" id="checkbox[<?php echo $row['id'];?>]" type="checkbox" value="<? echo $row['id']; ?>">
<input name="bar" type=SUBMIT>
</form>
</TD>
</tr>
<?
++$i;
}
echo "</table>";
?>

 

I don't know what kind of db you are using, but where I put the comments you can just call your db to see what the value was and negate it.

Link to comment
Share on other sites

lemmin,

 

Thank you for your posting. I'm not sure where to place this in my code. I've tried 3-4 places and they all break the page. I'm using a normal MySQl database if that's what you're asking. In case I'm mucking up something else, I'm posting the whole page below. At this time, this returns the 'right' information to the screen. It shows a check box that shows the status of the db. This is where my "are radio buttons" the best thing to use. There I could just click on/off and submit.

 

Again, your help is much appreciated!!

---------------------------------------------------

<?

include("dbinfo.inc.php");

mysql_connect(mysql,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

         

$query="SELECT * FROM birthdays WHERE Event='suspects'";

$result=mysql_query($query);

 

$num=mysql_numrows($result);

 

mysql_close();

 

?>

 

<table border="1" cellspacing="0" cellpadding="0">

 

<?

$i=0;

while ($i < $num) {

$Active=mysql_result($result,$i,"Active");

$firstname=mysql_result($result,$i,"Contact_Info_FirstName");

$lastname=mysql_result($result,$i,"Contact_Info_LastName");

$city=mysql_result($result,$i,"Contact_Info_City");

$state=mysql_result($result,$i,"Contact_Info_State");

$zip=mysql_result($result,$i,"Contact_Info_ZipCode");

$country=mysql_result($result,$i,"Contact_Info_Country");

$Bfirstname=mysql_result($result,$i,"Birthday_Info_FirstName");

$Blastname=mysql_result($result,$i,"Birthday_Info_LastName");

$Bcity=mysql_result($result,$i,"Birthday_Info_City");

$Bstate=mysql_result($result,$i,"Birthday_Info_State");

$Bzip=mysql_result($result,$i,"Birthday_Info_ZipCode");

$Bcountry=mysql_result($result,$i,"Birthday_Info_Country");

$id=mysql_result($result,$i,"id");

 

?>

</form>

<tr>

<td valign="top">

<STRONG> <? echo "$firstname $lastname"; ?></STRONG>...

 </TD>

<TD width="200" valign="top">

<? echo "$city, $state $country"; ?><input type="hidden" name="ud_id" value="<? echo $id; ?>">

 </TD>

<TD width="200" valign="top"> <B>Active?:

<?php

// Set the variable $foo from the database or from form data.

$checked = $Active ? "checked" : "";

echo "<input type=\"checkbox\" name=\"foo\" $checked>";

?>

 </td>

 

 

</TD>

</tr>

<?

++$i;

}

echo "</table>";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.