Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 30th, 2004, 05:43 PM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting results from Select Multiple HTML tag

;)Can anyone tell me what I'm doing wrong, or not doing?
I have tried $_POST['names'], and foreach($names as $allnames);
and I can not get the values to pass to the submit.php form.
Thanks :-)
Code:
<TABLE border="1">
<TH> This is the HEADER</TH>
<tr>
<form method="POST" action="submit.test2.php">
<tr><td><select name="names[]" multiple size="5">
  <option value="Tammy Jones">Tammy Jones</option>
  <option value="Tommy Jones">Tommy Jones</option>
  <option value="Fred Flintstone">Fred Flintstone</option>
  <option value="Heather Locklear">Heather Locklear</option>
</td><tr><td align=center>
 </select><input type="submit" value="Submit" >
</form> </td></tr>
</BODY>
</HTML>
;);)
Reply With Quote
  #2 (permalink)  
Old January 30th, 2004, 06:45 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I don't know if this will help with your PHP, but I think your HTML is faulty...

It seems you end the row and column that contain the select menu before you close the select menu itself.

Try this (remove the crossed-out red and insert the bold red):

Code:
<TABLE border="1">
<TH> This is the HEADER</TH>
<tr>
<form method="POST" action="submit.test2.php">
<tr><td><select name="names[]" multiple size="5">
  <option value="Tammy Jones">Tammy Jones</option>
  <option value="Tommy Jones">Tommy Jones</option>
  <option value="Fred Flintstone">Fred Flintstone</option>
  <option value="Heather Locklear">Heather Locklear</option>
</select></td><tr><td align=center>
 <s></select></s><input type="submit" value="Submit" >
</form> </td></tr>
</BODY>
</HTML>
----------
---Snib---
----------
Reply With Quote
  #3 (permalink)  
Old January 30th, 2004, 06:47 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Another thing: you don't close the table. In this sample you would close it after the last <tr>.

HTH

----------
---Snib---
----------
Reply With Quote
  #4 (permalink)  
Old January 30th, 2004, 06:59 PM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks,
I changed that. makes the code follow nicer but still doesn't tell the array to save the clicked options. I think I'll have to use some javascript...

Don

Reply With Quote
  #5 (permalink)  
Old January 30th, 2004, 09:04 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Quote:
quote:
I have tried $_POST['names'], and foreach($names as $allnames);
and I can not get the values to pass to the submit.php form.
Thanks :-)
Don't forget that you're creating a multidimensional array...

$_POST['names'] becomes an array itself. Also foreach is a control structure, its purpose is to loop, in javascript you'd use for/in (not an exact equivalent but the closest).. but you still have to create the statement as a control structure, or it doesn't do anything useful.

foreach($_POST['names'] as $name)
{
    echo $name;
}

The syntax is:

foreach ($array as $key => $value) ...

Have a look at:
http://www.php.net/manual/en/control-structures.php

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #6 (permalink)  
Old January 30th, 2004, 09:09 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Also, some helpful debugging functions are..

print_r and var_dump

If you can't figure out where your POST data is going, then dump it out..

echo '<pre>';
print_r($_POST);
echo '</pre>';

http://www.php.net/print_r
http://www.php.net/var_dump

print_r provides information about a variable, var_dump provides a slightly more complex analysis.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #7 (permalink)  
Old January 30th, 2004, 10:17 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Hi guys,

Quote:
quote:Originally posted by quesadilla5
Don't forget that you're creating a multidimensional array...

$_POST['names'] becomes an array itself. Also foreach is a control structure, its purpose is to loop, in javascript you'd use for/in (not an exact equivalent but the closest).. but you still have to create the statement as a control structure, or it doesn't do anything useful.

foreach($_POST['names'] as $name)
{
    echo $name;
}

The syntax is:

foreach ($array as $key => $value) ...
Well, the fact that $_POST['names'] is an array is why you can use it in a foreach loop in the first place. The only difference between this:
  foreach($_POST['names'] as $name)

and this:
  foreach($_POST['names'] as $key => $val)

is that the 2nd approach allows you to also get the key of each element in the array, should you need it.

Since the HTML form doesn't explicitly SET the array keys (they're numeric keys created on the fly via the empty bracket notation, i.e. "names[]"), you don't necessarily NEED the actual key value, so omitting it from the foreach() loop is okay.

In fact, it's a wise decision because you don't introduce the overhead of creating a variable to store a value that you won't end up using.


All that being said, there should really be no reason why your code doesn't work -- that is, it works for me when I use this simple test script (basically a copy/paste of your original code with only slight modifications and a lot of beautification/reformatting):



<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table border="1">
  <tr>
    <th> This is the HEADER</th>
  </tr>
  <tr>
    <td>
      <select name="names[]" multiple size="5">
        <option value="Tammy Jones">Tammy Jones</option>
        <option value="Tommy Jones">Tommy Jones</option>
        <option value="Fred Flintstone">Fred Flintstone</option>
        <option value="Heather Locklear">Heather Locklear</option>
      </select>
    </td>
  </tr>
  <tr>
    <td align=center>
     <input type="submit" value="Submit" >
    </td>
  </tr>
</table>
</form>
<pre>
<?php
if (isset($_POST['names']))
{
    foreach($_POST['names'] as $name)
    {
        echo "Name is $name\n";
    }
}

?>


Also, notice how much easier proper indentation and formatting makes it to verify that all your HTML tags are properly nested -- that is, your tags don't overlap boundaries.

In your original code, you started the <select> tag in one table cell, and closed it in a completely separate row. That's really really really bad style, and should be avoided at all costs.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #8 (permalink)  
Old February 2nd, 2004, 10:30 AM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for all your comments.
I am really looking to see why when the "Submit" button is pushed it's not passing the "Value of the Array" to the submit.php form action like it should. I'm only getting one word "Array" as the value of the names Array.
Code:
  <TABLE border="1">
   <TH> This is the HEADER</TH>
    <tr>
     <tr><td><form method="POST" action="submit.test2.php"><select name="names[]" multiple size="5">
        <option value="Tammy Fay">Tammy Fay</option>
        <option value="Angy Everheart">Angy Everheart</option>
        <option value="Bridget Hall">Bridget Hall</option>
        <option value="Christy Turlington">Christy Turlington</option>
        <option value="Claudia Shiffer">Claudia Shiffer</option>
        <option value="Estella Warren">Estella Warren</option>
        <option value="Naomi Campbell">Naomi Campbell</option>
        <option value="Stephanie Seymore">Stephanie Seymore</option>
        <option value="Sybil Bucks">Sybil Bucks</option>
        <option value="Tyra Banks">Tyra Banks</option>
        <option value="Yasmeen Ghauri">Yasmeen Ghauri</option>
     </select></td><tr>
    <td align=center><input type="submit" value="Submit" ></form></td>
   </tr>
  </TABLE>


Reply With Quote
  #9 (permalink)  
Old February 2nd, 2004, 12:25 PM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

WOWI Finally got it to work.
It required me to implode the value after the post and I was able to set it and insert into the DB.
 Thanks for ya'lls help.
Code:
<html>
 <body>
 <?php
  require("log.inc");

  $dojo = implode(",", $names);
  //echo ("<p>");
  print ("INSERT INTO logtest set SSYS = '$dojo' ");

  ?>

 <META HTTP-EQUIV="REFRESH" CONTENT = "4; URL=http://hs01sd12/hs/thankyou.html"> ?>

 </body>
</html>
Reply With Quote
  #10 (permalink)  
Old February 2nd, 2004, 04:03 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, well, I'm glad you got it working.

Bear in mind, of course, that the question you originally asked had nothing to do with what your problem really was. It would've helped a lot to have a more detailed description of your problem and/or some source code to show what you were really trying to do with it.

If you have an array variable, you can't just convert it to string like this:

$names = array("Naomi Campbell", "Stephanie Seymore", "Tyra Banks");

echo "My names are $names."

This will echo "My names are Array." since PHP can't convert arrays to strings on-the-fly.

So the problem you were REALLY having wasn't so much that you weren't receiving array input from the user, it was that you didn't know how to create a SQL query string from an array of values.



Take care,

Nik
http://www.bigaction.org/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML tag from C# or ASP.NET tag from javascript angshujit ASP.NET 2.0 Basics 3 February 16th, 2007 10:07 AM
Quest: How to select the second tag?! gnesland XSLT 4 November 28th, 2006 11:09 AM
HTML tag vs Body Tag CFGerry BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 October 7th, 2005 07:13 AM
can't read value from select tag using javascript kumar_kumar Javascript 2 February 9th, 2005 11:18 PM
Dynamic Select Tag fs22 Javascript How-To 1 December 15th, 2004 08:32 PM



All times are GMT -4. The time now is 09:00 AM.


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