Jump to content

Posting multiple values in form


Michan

Recommended Posts

Hi there,

I'm having some problems displaying the values of a multiple option box in PHP. I can return the data from text boxes, radio buttons, etc -- but I can't seem to find a way to display the data produced from a multiple option box.

Here is my code:

[code]<form method="post" action="admin_article_send.php">
<select name="game" multiple="multiple">');
$selectgames = mysql_query('SELECT vg_games.id AS id, vg_games.name AS title, vg_categories.abbreviation AS cat FROM vg_games LEFT JOIN vg_categories ON vg_games.category=vg_categories.catid ORDER BY vg_games.name ASC');
while ($showgames = mysql_fetch_array($selectgames))
{
echo (' <option value="'.$showgames['id'].'">'.$showgames['title'].' ['.$showgames['cat'].']</option>');
}
echo(' </select>
</form');[/code]

This successfully selects the products listed in my database.

[quote]<?PHP

$game = $_POST['game'];

foreach($game as $g)
{ echo $g; }

?>[/quote]

And this should display the selected options on a page.

Here is my error:

[quote]Warning: Invalid argument supplied for foreach() in /home/.kaybill/clubski/www.clubskill.com/csseven/admin_article_send.php on line 17[/quote]

Line 17 is:

[code]foreach($game as $g)[/code]

It is successfully sending other values (such as the value of a text box, and a drop down category list (also plucked from the database)) - but I'm really stuck here.

If anyone could help, it'd be most appreciated.

-Mi
Link to comment
https://forums.phpfreaks.com/topic/29722-posting-multiple-values-in-form/
Share on other sites

[quote author=mgallforever link=topic=117636.msg480056#msg480056 date=1165441150]
[code]
foreach ($game as $g)
{
  echo "$g<br />";
}
[/code]
[/quote]

Thanks, but this still doesn't work. I tried something similar (using | pipes, in preparation to send the data to the database) beforehand, but just couldn't get it to work.

The error is actually in the top line of the code you suggested, and I can't figure out what's causing it.
[quote author=kenrbnsn link=topic=117636.msg480070#msg480070 date=1165441488]
You need to make the name of the selection list an array:
[code]
<select name="game[]" multiple="multiple">
[/code]

Then your script will get the multiple selected values returned.


Ken
[/quote]

Thanks Ken; I actually had the name set to an array earlier, but as I was recieving errors, I tried it without to see the result, and I guess I forgot to change it back.

Even with the "game[]", I am still getting the same error message :(
[quote author=kenrbnsn link=topic=117636.msg480082#msg480082 date=1165442215]
Put
[code]<?php
if (isset($_POST)) echo '<pre>' . print_r($_POST,true) . '</pre>';

?>[/code]

at the start of your script. This will dump the $_POSTed variables. The output should help you.

Ken
[/quote]

Thanks Ken,

It looks like the values aren't actually being sent, as there's no "[game] =>" or anything similar, but all of the other results appear just fine.

There's Javascript running with the form, so is it worth me posting the entire form and results page? (It's pretty hefty, mind you.)

Many thanks.
[quote author=kenrbnsn link=topic=117636.msg480088#msg480088 date=1165442768]
Do a "show source" once you form is displayed. Post the HTML of the generated form.

Ken
[/quote]

Thanks Ken; the code is too long for the forum post, so I've uploaded it here: http://www.stabhead.com/code.html

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.