Hello all,

I'm trying to get through this tutorial, but running into a brick wall. I'm not very versed with PHP, so the error may be simple.


I'm trying to call the data that the user inputted to the PHP form, but it's only displaying blank lines for me. I've read multiple tutorials online, but was not sure if all of the 'names' for the checkboxes were supposed to be the same or where to include the '[]' for each name. For each feature, I want it to display 'on' if it's checked and nothing if it's not checked, just to keep what I have in the HTML (i.e. "Rust: ".

Line 12 of the PHP code is where the issue begins as far as not knowing what to code.

Here's my HTML:

<form action="cart.php" method="get">
<html>
<head>
<title>Jason's Demo Form</title>
</head>
<body>
Welcome to Jason's Widget Emporium.  Select the following items to customize your 

widget.<p>
Engrave your widget with a personal message:<p>
First Line <input type="text" name="firstline" /><BR>
Second Line <input type="text" name="secondline" />

<br>
Choose your color:<br>
<input type="radio" name="color" value="red">Red<br>
<input type="radio" name="color" value="green">Green<br>
<input type="radio" name="color" value="blue">Blue<br>

<br>
Additional Features to Include:<br>
<input type="checkbox" name="f[]" value="better">20% Better (Add $10) <br>
<input type="checkbox" name="f[]" value="rust">Rust Proofing (Add $20) <br>
<input type="checkbox" name="f[]" value="gift">Gift Wrapping <br>

<br>
Enter any special processing instructions here:<br>
<textarea rows=5 cols=50 name="special"></textarea>
<p>
<input type="submit" name="submit" value="Add to Shopping Cart">
</body>
</html>
</form>

And here's the PHP:

<html>
<head>
<title>Shopping cart test</title>
</head>
<body>
Line One is: <?php echo $_GET["firstline"]; ?>  <br> 
Line Two is: <?php  echo $_GET["secondline"]; ?> <br>
Color is: <?PHP
$selected_radio = $_GET['color'];
print $selected_radio;
?><br>
Better is: 
<?php
$better=$_GET['better'];
foreach ($f1 as $better)
{
echo "on";
}
?><br>
Rust Proofing is:<br>
Gift Wrapping is: <br>  
Special instructions are:  <br>
</body>
</html>

Any help is greatly appreciated :)

Checkboxes works like this.

A Checkbox, is assigned a name and a value. When you send checkbox data to a php page, the url would show name=value as part of the url.

For a checkbox, the name is required while the value is optional. Let's look at this in cases.

Case 1: name and value
In this case, you would have to change your code as

HTML

<input type="checkbox" name="better" value="better">20% Better (Add $10) <br>
<input type="checkbox" name="rust" value="rust">Rust Proofing (Add $20) <br>
<input type="checkbox" name="gift" value="gift">Gift Wrapping <br>

PHP Code

Better is: <?php echo $_GET['better'] ?> <br>
Rust Proofing is: <?php echo $_GET['rust'] ?><br>
Gift Wrapping is: <?php echo $_GET['gift'] ?><br>

Using the above, the output would be (If better and rust are checked and gift is not checked) $_GET['better'] = "better"; $_GET['rust']="rust" and $_GET['gift'] would be blank Case 2: name and no value

The HTML code would be

<input type="checkbox" name="better">20% Better (Add $10) <br>
<input type="checkbox" name="rust">Rust Proofing (Add $20) <br>
<input type="checkbox" name="better">Gift Wrapping <br>

The PHP code remains the same

In this case the output would be ( If better and rust are checked and gift is not checked) $_GET['better']="on"; $_GET['rust']="on" and $_GET['gift'] would be blank. Case 3: name as array and value given

The HTML code would be

<input type="checkbox" name="f[]" value="better">20% Better (Add $10) <br>
<input type="checkbox" name="f[]" value="rust">Rust Proofing (Add $20) <br>
<input type="checkbox" name="f[]" value="gift">Gift Wrapping <br>

The PHP code here is a bit complex, the data would be transfered to the other page as an array in f[] so a foreach loop will be used to get the data.

echo "Additional Features:";
foreach ($f as $value)
{
   echo $value.", ";
}

In this case the output would be (If the better and rust are selected and gift is not) Additional Features: better, rust .

I hope this long post of mine helps you understand the nuances of using Checkboxes. In your question, it is Case 2 that you are looking for.

All the best!!!

Thank you so much! You solved my issue and taught me something as well!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.