Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 9th, 2006, 5:26 PM   #1
k4pil
Programmer
 
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 3 k4pil is on a distinguished road
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!
k4pil is offline   Reply With Quote
Old Feb 9th, 2006, 6:30 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 10th, 2006, 5:01 AM   #3
k4pil
Programmer
 
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 3 k4pil is on a distinguished road
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 (??????
k4pil is offline   Reply With Quote
Old Feb 10th, 2006, 5:37 AM   #4
k4pil
Programmer
 
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 3 k4pil is on a distinguished road
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?
k4pil is offline   Reply With Quote
Old Feb 10th, 2006, 6:22 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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).
    ....
}
It's hard for me to believe you're actually reading any explanatory or tutorial material on this stuff. If you'd like to get a visual idea of how this is working, change your form method to GET and watch what happens at the end of the URL in the address box.
__________________
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
DaWei is offline   Reply With Quote
Old Feb 10th, 2006, 9:30 AM   #6
Lich
Professional Programmer
 
Lich's Avatar
 
Join Date: May 2005
Location: Detroit
Posts: 254
Rep Power: 4 Lich is on a distinguished road
Send a message via AIM to Lich Send a message via MSN to Lich
[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]
__________________
--John Cruz
Web Developer
www.cruzweb.net
Lich is offline   Reply With Quote
Old Feb 10th, 2006, 10:32 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 10th, 2006, 3:01 PM   #8
k4pil
Programmer
 
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 3 k4pil is on a distinguished road
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 3:22 PM.
k4pil is offline   Reply With Quote
Old Feb 10th, 2006, 3:16 PM   #9
Eryk
Programmer
 
Join Date: Jul 2005
Posts: 62
Rep Power: 3 Eryk is on a distinguished road
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.
Eryk is offline   Reply With Quote
Old Feb 10th, 2006, 3:33 PM   #10
k4pil
Programmer
 
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 3 k4pil is on a distinguished road
can you help me a lil by telling me how i would cycle through the POST form?
k4pil is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC