problem: value of arrays using checkboxes on form

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 14103 Views - Last Post: 23 June 2011 - 09:55 AM Rate Topic: -----

#1 crosby133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-June 11

problem: value of arrays using checkboxes on form

Posted 21 June 2011 - 07:17 PM

I've tried everything and nothing is working. So overwhelmed and frustrated I don't know what to do... I have a (mock) order form in a single file and I'm trying to extract dollar values from the elements in my arrays (don't know how) and then total them. I'm using radio buttons and checkboxes. None of my loops are working and I'm pretty much lost. If you can't tell, I'm teaching myself PHP and trying to actually learn something!

<body>
<?phpif (isset($_POST['extras']) && is_array($_POST['extras']) && (isset($_POST['color'])) {	
foreach($_POST['extras'] as $key=>$value)
echo "<table border=\"2\">";echo " <tr> <td>item</td> <td>cost</td> </tr>";
echo " <tr> <td>".$color."</td><td>".$(how do i get cost of color)."</td> </tr>";
echo "<tr>";    echo "<td>".$value."</td><td>".$(how do i get cost of value)."</td>";
echo "</tr>";echo " <tr> <td>Your total is:</td> <td>".$(how do i get total cost)."</td> </tr>";
echo "</table>";
?>
<form action='' method='post'>    
<table border="1">
<tr><td colspan="2" align="center" scope="col">Select your travel kit</td></tr>
<tr><td><input type="radio" name="travel[]" value="Short Trip" />Short trip $15.99</td>    
<td><input type="radio" name="travel[]" value="Long trip" />Long Trip $28.00</td></tr>    
<td><input type="radio" name="travel[]" value="Overnight" />Overnight $10.00</td></tr>
</table> 
<br> 
<table border="1">
<tr><td colspan="2" scope="col">Pick your extras ($2.00 ea.)</td></tr>
<tr><td><input type="checkbox" name="extras[]" value="brush" />Brush</td>    
<td><input type="checkbox" name="extras[]" value="shampoo" />Shampoo</td></tr>  
<tr><td><input type="checkbox" name="extras[]" value="toothpaste" />Toothpaste</td>
<td><input type="checkbox" name="extras[]" value="rinse" />Rinse</td></tr>  
<tr><td colspan="2">
<input type="submit" name="submit" value="Submit Your Order" /></td></tr>
</table>
</form>
</body>


Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: problem: value of arrays using checkboxes on form

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: problem: value of arrays using checkboxes on form

Posted 21 June 2011 - 07:37 PM

You need to change the value attribute of your radio to the price.
<input type="radio" name="travel[]" value="20.23" />


You PHP tag needs to be written properly too. (Line #2)

Your foreach statement only really needs to be this
foreach($_POST['extras'] as $value)


Separate your HTML from PHP.
Was This Post Helpful? 1
  • +
  • -

#3 crosby133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-June 11

Re: problem: value of arrays using checkboxes on form

Posted 21 June 2011 - 08:18 PM

When I take your advice (thank you by the way) then I only get the dollar value displayed...Is there a way I can get the name of the checkbox in one column and the dollar value in another column. Maybe I'm going about this in the wrong way...I'm not really sure what to do!

When it comes to separating my PHP and HTML, i'm actually trying to have a single file redux so i think I have to keep it the way it is? I don't know...

I was totally understanding PHP until I got to arrays, but considering arrays are the most important part of PHP I guess I don't really get it at all!
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: problem: value of arrays using checkboxes on form

Posted 21 June 2011 - 08:42 PM

You can't get it the way you want it just like that. It'll take a bit more of an unorthodox approach.

What I did was made a hidden field for each radio button. Each hidden field's name attribute was the same as the value of it's corresponding radio button. The value for the hidden field was set to the price.

So basically we'll access $_POST with the value of the selected radio button which will give us our pricing.

P.S. Avoid spaces in your value and name attributes when doing this.

Example
<?php
if(isset($_POST['submit']))
{
	foreach($_POST['travel'] as $radio)
		echo "$radio: {$_POST[$radio]}<br />";
}
?>
<!DOCTYPE html>
<html>
<body>
	<form enctype="application/x-www-form-urlencoded" action="" method="post">
    	<input type="radio" name="travel[]" value="Long_Trip" /> 1<br />
        <input type="hidden" name="Long_Trip" value="50.00" />
        
		<input type="radio" name="travel[]" value="Short_Trip" /> 2<br />
        <input type="hidden" name="Short_Trip" value="20.00" />
		<input type="submit" name="submit" />
    </form>
</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,467
  • Joined: 08-August 08

Re: problem: value of arrays using checkboxes on form

Posted 21 June 2011 - 09:10 PM

Why are you using an html array? Why not do something like this?
<form action='array.php' method='post'>
<input type="radio" name="travel" value="Short Trip" />Short trip $15.99
<input type="radio" name="travel" value="Long Trip" />Long trip $28.00
<input type="radio" name="travel" value="Overnight" />Overnight $10.00
<input type="submit" value="Submit">
</form>

array.php:
<?php
var_dump($_POST);
$value = array("Short Trip"=>15.99, "Long Trip"=>28, "Overnight"=>10);
if(isset($_POST['travel'])) {
	echo "<br />The value of the trip is: $".$value[$_POST['travel']];
}
?>

Was This Post Helpful? 1
  • +
  • -

#6 crosby133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-June 11

Re: problem: value of arrays using checkboxes on form

Posted 22 June 2011 - 12:09 AM

Hey, thanks for the help! Definitely got me going on the right track/I'm actually starting to understand arrays! I've really cleaned up my code and almost have everything working...

Can anyone provide me with any knowledge on trying to get value names (and helping me find why my for loop won't work)? (See code notes)

So, so grateful for the help!!

<?php
/*This stuff is only here because I want to make sure
there are 2 decimal places in the final numbers since
I'm dealing in "money" values*/
$total = number_format ($total,2);
$value = number_format ($value,2);
$additional = number_format ($additional,2);

/*I think I should define the value of "total" here, right?
$total = $value + $additional isn't working...not recognizing
that they are real number values (I think). So do I need to use
(int) anywhere?*/

$value = array("Short Trip"=>'15.99', "Long Trip"=>'28.00', "Overnight"=>'10.00');
if(isset($_POST['travel'])) {

$extras = array("brush"=>'1.50', "shampoo"=>'1.50', "toothpaste"=>'1.50', "cream rinse"=>'1.50');
if (isset($_POST['extras'])) {
foreach ($_POST['extras'] as $additional) {

/*Table of output results. I know that I need a for loop around the $additional
value to make sure all possible checkbox values run and create new table rows.
But, everytime I put a for loop in there, everything stops working!
I don't know if I'm just not closing the loop right or ...?*/

echo "<table border =\"2\">
    <tr><td>Item</td><td>Charges</td></tr>
    <tr><td>".$value."</td>/*trying (unsuccessfully) to get the actual value name here: Short Trip, etc.*/
    <td> $".$value[$_POST['travel']]."</td></tr>
    <tr><td>".$additional."</td>/*Same story, looking to post the actual name*/
    <td> $".$additional."</td></tr>
    <tr><td>Your total</td> <td>".$total."</td></tr> /*Trying to produce a total here, if only it worked!*/
    </table>";
    /*Everything else is working fine, so thats good!*/
}
}
}
?>


<html>
<body>
<form action="" method="post">

 <table border="2">
  <tr>
    <td colspan="2" align="center" scope="col">Stay Information</td>
  </tr>
  <tr>
    <td><input type="radio" name="travel" value="Short Trip" />Short trip $15.99</td>
    <td><input type="radio" name="travel" value="Long Trip" />Long trip $28.00</td>
  </tr>
  <tr>
    <td><input type="radio" name="travel" value="Overnight" />Overnight $10.00</td>
  </tr>
</table>
<table border="2">
<tr>
<td colspan="2" scope="col">What will you need?($1.50 each)</td>
  </tr>
  <tr>
<td><input type="checkbox" name="extras[]" value="1.50" />Brush</td>
<td><input type="checkbox" name="extras[]" value="1.50" />Shampoo</td></tr>
  <tr>
<tr><td><input type="checkbox" name="extras[]" value="1.50" />Toothpaste</td>
<td><input type="checkbox" name="extras[]" value="1.50" />Cream Rinse</td></tr>
  </tr>

<tr><td colspan="2">
<input type="submit" value="Submit"></td></tr>
</table>
</form>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,467
  • Joined: 08-August 08

Re: problem: value of arrays using checkboxes on form

Posted 22 June 2011 - 05:49 AM

Why did you get rid of the array for travel, but not extras? You should do them both the same way.
Was This Post Helpful? 1
  • +
  • -

#8 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: problem: value of arrays using checkboxes on form

Posted 22 June 2011 - 05:52 AM

Lines #5 - 7
Where was $total, $value and $additional initialized?
Was This Post Helpful? 0
  • +
  • -

#9 crosby133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-June 11

Re: problem: value of arrays using checkboxes on form

Posted 22 June 2011 - 08:13 AM

Not quite sure what you mean by initialized...I just was trying to set the numbers to always show .00 (or whatever) as change... My code partially works now so I thougt i was doing ok? LOL

I've been working nonstop to try to show the names of the values as well as the numbers, but i just cannot figure it out. would creating a second array help my problem?
Was This Post Helpful? 0
  • +
  • -

#10 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: problem: value of arrays using checkboxes on form

Posted 22 June 2011 - 08:22 AM

Try removing the spaces from the value of your radio elements.
Was This Post Helpful? 0
  • +
  • -

#11 crosby133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-June 11

Re: problem: value of arrays using checkboxes on form

Posted 22 June 2011 - 12:05 PM

Yep, tried that. Still doesn't work though. I've been tinkering with the code all day and NOTHING seems to be working! Can anyone see what is making my code go so wrong?

Thanks
Was This Post Helpful? 0
  • +
  • -

#12 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: problem: value of arrays using checkboxes on form

Posted 22 June 2011 - 12:13 PM

To debug your script properly do this

  • Place this at the top of your script
    error_reporting ( E_ALL );
    
    This will output all errors, warnings, notices, etc....
  • Use var_dump to view the contents of variables, arrays, etc...
    var_dump($_POST['extras'], $_POST['travel']);
    
    You can dump the entire POST if you so desire. Practice placing var_dump just before where the variable or object is being accessed.

Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: problem: value of arrays using checkboxes on form

Posted 22 June 2011 - 04:38 PM

Quote

Not quite sure what you mean by initialized


Initialized means given an initial value, like

$total = 0.0;

Was This Post Helpful? 0
  • +
  • -

#14 crosby133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-June 11

Re: problem: value of arrays using checkboxes on form

Posted 22 June 2011 - 10:36 PM

OK, last-ditch effort here...I've debugged my script, I've tried to find all the errors, I've re-read the same chapter on arrays in my PHP book 5 times (really, 5!) and obviously, I am missing something. Clearly I am missing a function or loop or something to be able to add all of the selected values with the name and number value showing. I know its a lot to ask, but could someone either tell me WHERE the missing function should go or just show me the missing function and i'll trial/error figure out where to put it? I refuse to be defeated in my PHP learning quest by this stubborn array!

<?php
/*This stuff is only here because I want to make sure
there are 2 decimal places in the final numbers since
I'm dealing in "money" values*/
$total = number_format ($total,2);
$value = number_format ($value,2);
$additional = number_format ($additional,2);

/*I think I should define the value of "total" here, right?
$total = $value + $additional isn't working...not recognizing
that they are real number values (I think). So do I need to use
(int) anywhere?*/

$value = array("Short Trip"=>'15.99', "Long Trip"=>'28.00', "Overnight"=>'10.00');
if(isset($_POST['travel'])) {

$extras = array("brush"=>'1.50', "shampoo"=>'1.50', "toothpaste"=>'1.50', "cream rinse"=>'1.50');
if (isset($_POST['extras'])) {
foreach ($_POST['extras'] as $additional) {

/*Table of output results. I know that I need a for loop around the $additional
value to make sure all possible checkbox values run and create new table rows.
But, everytime I put a for loop in there, everything stops working!
I don't know if I'm just not closing the loop right or ...?*/

echo "<table border =\"2\">
    <tr><td>Item</td><td>Charges</td></tr>
    <tr><td>".$value."</td>/*trying (unsuccessfully) to get the actual value name here: Short Trip, etc.*/
    <td> $".$value[$_POST['travel']]."</td></tr>
    <tr><td>".$additional."</td>/*Same story, looking to post the actual name*/
    <td> $".$additional."</td></tr>
    <tr><td>Your total</td> <td>".$total."</td></tr> /*Trying to produce a total here, if only it worked!*/
    </table>";
    /*Everything else is working fine, so thats good!*/
}
}
}
?>


<html>
<body>
<form action="" method="post">

 <table border="2">
  <tr>
    <td colspan="2" align="center" scope="col">Stay Information</td>
  </tr>
  <tr>
    <td><input type="radio" name="travel" value="Short Trip" />Short trip $15.99</td>
    <td><input type="radio" name="travel" value="Long Trip" />Long trip $28.00</td>
  </tr>
  <tr>
    <td><input type="radio" name="travel" value="Overnight" />Overnight $10.00</td>
  </tr>
</table>
<table border="2">
<tr>
<td colspan="2" scope="col">What will you need?($1.50 each)</td>
  </tr>
  <tr>
<td><input type="checkbox" name="extras[]" value="1.50" />Brush</td>
<td><input type="checkbox" name="extras[]" value="1.50" />Shampoo</td></tr>
  <tr>
<tr><td><input type="checkbox" name="extras[]" value="1.50" />Toothpaste</td>
<td><input type="checkbox" name="extras[]" value="1.50" />Cream Rinse</td></tr>
  </tr>

<tr><td colspan="2">
<input type="submit" value="Submit"></td></tr>
</table>
</form>
</body>
</html>



Thanks for all the input I've already received...you guys have helped me get this far!
Was This Post Helpful? 0
  • +
  • -

#15 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: problem: value of arrays using checkboxes on form

Posted 23 June 2011 - 03:41 AM

First you need to post the results of var_dump for us.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2