![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
How to use a checkbox and pass info using PHP??
Hi there.
I have a table; <table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Product ID</font></th>
<th><font face="Arial, Helvetica, sans-serif">Product name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Product Description</font></th>
<th><font face="Arial, Helvetica, sans-serif">Supplier</font></th>
<th><font face="Arial, Helvetica, sans-serif">Cost</font></th>
<th><font face="Arial, Helvetica, sans-serif">Add to account</font></th>
</tr>
<?
$i=0;
while ($i < $num) {
$productID=mysql_result($result,$i,"productID");
$prodName=mysql_result($result,$i,"prodName");
$prodDesc=mysql_result($result,$i,"prodDesc");
$supplier=mysql_result($result,$i,"supplier");
$cost=mysql_result($result,$i,"cost");
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo $productID; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $prodName; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $prodDesc; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $supplier; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $cost; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href = "custAddProd.php?productID=<?echo $productID;?>&id=<?echo $id?>"><div align="center">+<INPUT TYPE="checkbox" NAME="quantity" VALUE="button name"></div></font></a></td>
</tr>As you can see, it consists of columns for productID, productName, supplier... This table exists on a page with constomer details. When the user clicks in the add to account column the user is taken to a different page where the customer data and product data is passed. All this work without a problemo! Where i am stuck - as you can see a mess in the add product column. I am trying to add the functionality where the user can pick more then one product using checkboxes. I then want these to be passed on to the next page. How can i do this???? I have no idea...totally stuck. Hope someone can help. Thanks in advance and regards! |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
If a checkbox is checked, it's passed to the server like any other form element (its name is the key name in the post array). If it isn't checked, it doesn't get sent. Here's where that is_set or empty comes in handy, again.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
Can you just explain that a little bit??
So i CAN use the checkbox by adding the $productID=$_POST['productID']; ?? How do i get the array element?? Loop? $i=0; while (?????? |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
Why isnt this working?
<tr>
<form align = "center" action="custAddProd.php" method="post"><!-- Form for the checkboxes -->
<td><font face="Arial, Helvetica, sans-serif"><? echo $productID; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $prodName; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $prodDesc; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $supplier; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $cost; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href = "custAddProd.php?productID=<?echo $productID;?>&id=<?echo $id?>"><div align="center">+</div></font></a><INPUT TYPE="checkbox" NAME="product_id" VALUE="<?echo"$productID";?>"></td>
</tr>
<?
$i++;
}
echo "</table>";?>
<a href = "custAddProd.php?id=<?echo "$id";?>"><input type="Submit"></a>
</form>When i click on submit the $id dosnt get passed. Is it bad design? |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
On the server side,
if (!empty ($_POST ["product_id"]))
{
// Checkbox checked
$_POST ["product_id"] will contain the value of the field
...
}
else
{
// Checkbox not checked
$_POST ["product_id"] will be empty (not set).
....
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Professional Programmer
|
[PHP]<a href = "custAddProd.php?id=<?echo "$id";?>"[/PHP]
Should be [PHP] <input type="Submit">[/PHP] and make id a hidden field in your form that will go through when your form is submitted. Then somewhere in custAddProd.php add: [PHP]$id = $_POST["id"][/PHP] |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Run this and watch your address bar and page.
[php] <?php if (!empty ($_GET ["CheckBox"])) echo $_GET ["CheckBox"]."<br/>"; else echo "I_don_be_checked<br/>"; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Checkbox Check</title> </head> <body> <form action="cbox.php" method="get" name="myForm"> <input name="ButtonWidget" type="submit" value="Submit"> <input name="CheckBox" type="checkbox" value="I_be_Checked"> </form> </body> </html> [/php] With a "post" method, same effect, you just won't see it in the address bar.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
Ok.
A little variation of this is what i need...but still can't seem to get it working. <form action="cbox.php" method="get" name="myForm">
$i=0;
while ($i < $num) {
$productID=mysql_result($result,$i,"productID");
<INPUT NAME="<?echo "$productID";?>" TYPE="checkbox" VALUE="<echo"$productID";?>
$i++;
}
</form>As you can see, there is a list of checkboxes, each holding a value output from my database. I now need to pass this to the next page and access the data and make a list of the checkboxes ticked. All connecting to the database has been ommited, but it is all working fine. Hope this makes sense, and someone understands. Thanks again Last edited by k4pil; Feb 10th, 2006 at 4:22 PM. |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jul 2005
Posts: 62
Rep Power: 4
![]() |
The checkboxes checked will be the only ones passed over from the form submition. Therefore, for the most part, you can just cycle through the POST form. Although I'd do a different value that is global throughout them, that way you can see which ones are which.
|
|
|
|
|
|
#10 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
can you help me a lil by telling me how i would cycle through the POST form?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|