p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Chapter 3, multiple checkboxes - values don't show!


Message #1 by "Nicolai Thorning" <nicolai_thorning@h...> on Mon, 15 Apr 2002 14:29:05
Hi.

I am working my way trough the book and have come across a problem. The 
values of the selected checkbox(es) wont appear.

This is my html file:

<html>
<head></head>
<body>
<form method=POST action="checkboxes.php">
Have you ever eaten haggis before?
<input name="Choice1" type="Checkbox" value="Haggis">
<br>
Have you ever eaten snails before?
<input name="Choice2" type="Checkbox" value="Snails">
<br>
Have you ever eaten locusts before?
<input name="Choice3" type="Checkbox" value="Locusts">
<br>
<br>
<input type=submit>
</form>
</body>
</html>


This is my php file:

<html>
<head></head>
<body>
<?php
echo "$_POST["Choice1"]<br>";
echo "$_POST["Choice2"]<br>";
echo "$_POST["Choice3"]<br>";
?>
</body>
</html>

As you can see I use the new method suggested in the PHP 4.1.1 version I 
downloaded from php4win. Any suggestions?

Thanks.
Nicolai
Message #2 by "John Arbon" <subscriptions@c...> on Tue, 16 Apr 2002 00:25:09
I may be out to lunch on this suggestion, but what if you were to use only 
single quotes ' around the 'Choice' variables?

JOhn
Message #3 by "John Arbon" <subscriptions@c...> on Tue, 16 Apr 2002 00:29:56
Or...

Does the order of the HTML "inputs" matter?

I ask, because usually I see the "type" first, then the "name"...

ie:

<input type='checkbox' name='Choice1' value='Haggis'>


John
Message #4 by "Nikolai Devereaux" <yomama@u...> on Mon, 15 Apr 2002 16:35:07 -0700
Nah, nothing like that, John.

Here's the problem:

> echo "$_POST["Choice1"]<br>";
> echo "$_POST["Choice2"]<br>";
> echo "$_POST["Choice3"]<br>";

You see that you're closing the string when you quote "Choice1"?  PHP
doesn't know how to parse that.

Do this:

echo $_POST['Choice1'] . '<br>'; // single quotes are optional here.
echo $_POST['Choice2'] . '<br>'; // i use them because they're faster
echo $_POST['Choice3'] . '<br>'; // to parse/process by PHP.*

or this:

echo "$_POST[Choice1]<br>";
echo "$_POST[Choice2]<br>";
echo "$_POST[Choice3]<br>";


See the manual section on string parsing for more information about how PHP
parses strings.  Knowing the quirks comes in handy when you start dealing
with embedding array variables in your strings.

http://www.php.net/manual/en/language.types.string.php#language.types.string
.parsing


Take care,

ni_K_olai

:)

*  About single-quoted strings being faster:  Single quoted strings are not
subject to variable substitution or special character escaping (except for
\'), so there is some extremely small performance difference to using them
if you're not planning on using the benefits of double quoted strings.

Don't stick yourself though.  Here's a common gotcha:

echo '<br>\n'; // prints the \n to the screen, NO NEWLINE!!

Message #5 by "John Arbon" <subscriptions@c...> on Tue, 16 Apr 2002 00:44:31
> 
Nah, nothing like that, John.

Hey!  I was pretty darn close, and on the right track, to boot! *)

John
Message #6 by "Nikolai Devereaux" <yomama@u...> on Mon, 15 Apr 2002 16:43:09 -0700
> Hey!  I was pretty darn close, and on the right track, to boot! *)

I'm sorry!  I was reading most recent messages first and replied when I saw
your message about attribute ordering.

Message #7 by "Nicolai Thorning" <nicolai_thorning@h...> on Tue, 16 Apr 2002 10:48:03
Thank you both for taking the time to reply.

It works now, as you said. Excellent.

Thanks.
Nicolai


  Return to Index