Wrox Programmer Forums
|
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2003, 06:13 PM
Registered User
 
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 3 Listbox Multiple problems

I have been stuck on this exercise and none of the variations I have tried based on my research on these forums and the Errata is giving me a clue.

The HTML Page

<FORM method="POST" action="listbox.php">
What fabric do you like best?<br>
<SELECT name="Fabric">
    <OPTION>Cotton</OPTION><br>
    <OPTION>Wool</OPTION><br>
    <OPTION>Silk</OPTION><br>
</SELECT>
<br>
What fabrics do you like least?<br>
<SELECT name="Least" multiple>
    <OPTION value="Least1">Rayon</OPTION><br>
    <OPTION value="Least2">Polyester</OPTION><br>
    <OPTION value="Least3">Linen</OPTION><br>
    <OPTION value="Least4">Dacron</OPTION><br>
</SELECT>
<br>
<br>
<input type="Submit">

</FORM>


The PHP Page

You Like:
<?php
echo $_POST["Fabric"];
?>
<br>
<br>
You Like Least:<br>
<?php
if(isset($_POST["Least1"])) echo $_POST["Least1"];
if(isset($_POST["Least2"])) echo $_POST["Least2"];
if(isset($_POST["Least3"])) echo $_POST["Least3"];
if(isset($_POST["Least4"])) echo $_POST["Least4"];
?>


The first part works just fine, but no selection in the multiple listbox is being displayed on my results page.

Thanks for the clues.

Merl
 
Old December 8th, 2003, 08:55 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, the NAME of the input variable for the least-favorite fabric is "Least". You have four options. The user sees the words "Rayon", "Polyester", "Linen", and "Dacron" as their choices, these are actually submitted as "Least1", "Least2", "Least3", and "Least4", respectively.

This is the fix:

You Like Least:<br>
<?php
if(isset($_POST['Least'])) echo $_POST['Least'];
?>


Now, suppose the user selects "Linen" from the "least" list. This will show up as "Least3", not "Linen". You need to change the value attribute of the option tag for the texts to match:

<select name="LeastFavorite">
  <option value="Linen">Linen</option>
  <option value="Cotton">Cotton</option>
  etc...
</select>


Similarly, I should mention why this isn't an issue with your "favorite" fabric list. The options listed for that select don't explicitly set a value attribute. Therefore, the text between the <option> and </option> tags is taken as the value for that selection.

To be complete, though, you should explicitly set the value of an option in it's value attribute.



For example,

<select name="FavoriteFabric">
  <option value="cotton">I like cotton best</option>
  <option value="polyester">For some reason, I'm fond of polyester</option>
  <option value="linen">I have good taste; I like linen</option>
</select>


Look at the value of $_POST['FavoriteFabric'] when submitting a form with the above <select> input field.



Take care,

Nik
http://www.bigaction.org/
 
Old December 8th, 2003, 08:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also, you should get used to using print_r() or var_dump().

echo "<pre>"; // make output easy to read
print_r($_POST);
echo "</pre>\n";


I make heavy use of this handy function:

function printr($var, $desc = '')
{
   echo "<pre>";
   if ($desc !== '') echo " {$desc}: ";
   print_r($var);
   echo "</pre>\n";
}


And use it like this:

printr($_POST, '$_POST after submitting fabric form');



Take care,

Nik
http://www.bigaction.org/
 
Old December 8th, 2003, 09:54 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Quote:
quote:Originally posted by Merl
 I have been stuck on this exercise and none of the variations I have tried based on my research on these forums and the Errata is giving me a clue.
...[snip]
The first part works just fine, but no selection in the multiple listbox is being displayed on my results page.
My test scripts seem to suggest (at least with MSIE 6 and Mozilla) that no data is passed to the server from the select-multiple field unless the name of that field is appended with array brackets. One test suggested that without the brackets the field behaved like a select-one field.

Code:
<form method='post' action='/test.php'>

<select name='least1[]' multiple='multiple'> 
    <option value="least1">Rayon</option> 
    <option value="least2">Polyester</option> 
    <option value="least3">Linen</option> 
    <option value="least4">Dacron</option> 
</select> 
<input type='submit' name='do_action' value='post' />

<?php
    if (isset($_POST["do_action"]))
    {

        foreach ($_POST["least1"] as $key => $value)

            echo "\$least1[$key] = '{$value}';<br />\n";

        # Or you can just as easily dump the variables this way:
        # echo "<pre>";
        # var_dump($_POST["least1"]);
        # echo "</pre>";

        # echo "<pre>";
        # var_dump($_POST);
        # echo "</pre>";
    }    
?>
...notice the difference here: name='least1[]' the empty braces will create a multi-demensional array in the $_POST["least1"] variable on the server-side.

Thus creating variable output that looks like this.. (all four selected):

array(2) {
["least1"]=>
array(4) {
    [0]=>
    string(6) "least1"
    [1]=>
    string(6) "least2"
    [2]=>
    string(6) "least3"
    [3]=>
    string(6) "least4"
}
["do_action"]=>
string(4) "post"
}

Also have a look at my HTML FAQ located at:
http://p2p.wrox.com/topic.asp?TOPIC_ID=4028

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 8th, 2003, 10:36 PM
Registered User
 
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your message was very helpful Nic, however I still can make only one choice in "Least"

My current test looks like this

What fabrics do you like least?<br>
<br>
<SELECT name="Least" multiple>
    <OPTION value="Rayon">Rayon</OPTION><br>
    <OPTION value="Polyester">Polyester</OPTION><br>
    <OPTION value="Linen">Linen</OPTION><br>
    <OPTION value="Dacron">Dacron</OPTION><br>
</SELECT>
<br>
<br>
<input type="Submit">

</FORM>

You Like Least:<br>
<br>
<?php
if(isset($_POST["Least"])) echo $_POST["Least"];
?>

Which only returns the last selection from the list. I thick that the isset variable is part of the problem here, but simply removing it still only returns the last selection. I have tried several variation on using multiple "if..... echo...." formats and arrays and still get index errors or submit only returns the last selection.

Looking at your example Rich, I had already tried building the array in that fashion and with the current php method did not get the correct results. I sort of see where your suggested php method is going, but I don't yet understand just what you are doing there. This is my first real programing language and at this point in my learning I really need to know if any variation on the books method will work or not.

Thanks for the good info on HTML standards. I will definitely start using those standards in my real projects, but I must admit I am just a bit lazy on using the most current complete standards on exercises I will delete in a few days. 8^)

Thanks for the help

Merl
 
Old December 9th, 2003, 12:32 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Quote:
quote:Originally posted by Merl


Which only returns the last selection from the list. I thick that the isset variable is part of the problem here, but simply removing it still only returns the last selection. I have tried several variation on using multiple "if..... echo...." formats and arrays and still get index errors or submit only returns the last selection.

Looking at your example Rich, I had already tried building the array in that fashion and with the current php method did not get the correct results. I sort of see where your suggested php method is going, but I don't yet understand just what you are doing there. This is my first real programing language and at this point in my learning I really need to know if any variation on the books method will work or not.
You are only getting the last selection on the list because you aren't appending the empty array bracket syntax on the name of the field and because of this PHP doesn't create a multi-dimensional array from the post data. multiple='multiple' only tells the browser that the user can make multiple choices. For you to truly get all of the selected choices you need to treat the field as an array on both the client-side and the server-side. By doing: name='Least[]'

<SELECT name="Least[]" multiple>
    <OPTION value="Rayon">Rayon</OPTION><br>
    <OPTION value="Polyester">Polyester</OPTION><br>
    <OPTION value="Linen">Linen</OPTION><br>
    <OPTION value="Dacron">Dacron</OPTION><br>
</SELECT>

For example if the user chooses options 1 and 4:

Rayon and Dacron:
Option 1 would be available in PHP as the following two dimensional array:
$_POST["Least"][0] == "Rayon";

And option 4 would be available in PHP as the following two dimensional array:
$_POST["Least"][1] == "Dacron";

You see what's happening? The options are numbered offset from zero and increments for each option chosen. Option 1 isn't going to be "Polyester" and Option 2 isn't going to be "Linen"... etc.

The book's example isn't that great. They ouput every possible numeric indice without checking if it first exists... this is sloppy programming.

Here's a better example:

echo "You Like Least:<br />";

foreach ($_POST["least"] as $value)
{
    echo $value."<br />";
}

Now you have an example that only outputs indices present in the sub-array. foreach takes and iterates through the array till it reaches the last indice. In this example it assigns the value of each indice in the $value variable. Alternatively you can split the array into key value pairs...

foreach ($_POST["least"] as $key => $value)

$key will contain the value of the array indice, for this example it would contain 0 and 1.

An even better approach would be to test for is_array and isset first, which the book doesn't do at all:

if (isset($_POST["least"]) && is_array($_POST["least"]))
{
//foreach statement...
}

// perhaps an else statement containing an error message..

I hope that makes more sense.

: )
Rich





:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 9th, 2003, 12:38 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Oh and also you don't need to place a <br> after an <option> element. It doesn't do anything and could even break on some browsers.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 9th, 2003, 12:40 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Quote:
quote:
and could even break on some browsers.
No *pun* intended!

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 9th, 2003, 12:35 PM
Registered User
 
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Now that all made sense, thanks a bunch.
 
Old December 26th, 2004, 12:22 AM
Registered User
 
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Making the name of the listbox an array ("least[]") was a big help, and the PHP recognizes it, but, I was doing JavaScript on that listbox control - and when I added the "[]", I got an error. What's the trick around this?





Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple Listbox values in another listbox terryv Excel VBA 0 June 27th, 2007 07:01 AM
Multiple Selection of Listbox jasminecyriac ASP.NET 2.0 Basics 0 June 22nd, 2006 05:20 AM
Listbox Problems tready Access VBA 2 March 16th, 2006 03:21 PM
C# ListBox Working With Multiple Selections pro-logic C# 1 October 9th, 2005 02:15 AM
listbox recordset multiple pages vaguy02 VBScript 1 June 21st, 2005 06:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.