PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
ECJughead
Forum Newbie
Posts: 13 Joined: Fri Mar 07, 2008 9:14 am
Post
by ECJughead » Tue Sep 09, 2008 12:42 am
I'm using the following code snippet to pass an array from a form to a processing page. I'm getting back no data...can anyone assist? It's appreciated
Code: Select all
"<select name=\"ing[]\" size=\"1\">";
$ing_set = @mysql_query('SELECT id, ingredient FROM ingredient');
if (!$ing_set) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
while ($row = mysql_fetch_array($ing_set)) {
echo "<option name='ing' value=\"{$row['id']}\">{$row['ingredient']}</option>";
Code: Select all
$data=base64_encode(serialize($ing));?>
<input type="hidden" name="ing" value="<? echo $data ?>">
In the process page I have:
Code: Select all
$ing=unserialize(base64_decode($_POST['data']));
jaoudestudios
DevNet Resident
Posts: 1483 Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey
Post
by jaoudestudios » Tue Sep 09, 2008 1:53 am
You are overwriting the information with your hidden field! You dont need the hidden field.
The information will be sent by the select as an array! You dont need to use [] if the user can only select 1 option
ECJughead
Forum Newbie
Posts: 13 Joined: Fri Mar 07, 2008 9:14 am
Post
by ECJughead » Tue Sep 09, 2008 2:04 am
Then I guess I left out a vital piece of the puzzle. At this time, the SELECT is looping 3 times along with 2 other data points in the form. That's why I'm asking about utilizing arrays to pass the entries through to a process page. Is this how it's done?
ECJughead
Forum Newbie
Posts: 13 Joined: Fri Mar 07, 2008 9:14 am
Post
by ECJughead » Tue Sep 09, 2008 3:07 pm
Can anyone help?
Code: Select all
$count = 1;
while ($count <=3) {
echo "<tr><td>";
echo $count . ":" .
"<select name=\"ing[]\" size=\"1\">";
$ing_set = @mysql_query('SELECT id, ingredient FROM ingredient');
if (!$ing_set) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
while ($row = mysql_fetch_array($ing_set)) {
echo "<option name='ing' value=\"{$row['id']}\">{$row['ingredient']}</option>";
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Tue Sep 09, 2008 3:37 pm
That'll just loop forever. increment $count in your while.
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Tue Sep 09, 2008 3:41 pm
To answer your question, you can basically do it the way you are, yeah. But you'll want to write to a new array and pass that as the final.
Code: Select all
$ing = array();
while ($row = mysql_fetch_array($ing_set)) {
echo "<option name='ing' value=\"{$row['id']}\">{$row['ingredient']}</option>";
$ing[] = $row;
}
$data = base64_encode(serialize($ing));
echo "<input type=\"hidden\" name=\"data\" value=\"$data\" />";
ECJughead
Forum Newbie
Posts: 13 Joined: Fri Mar 07, 2008 9:14 am
Post
by ECJughead » Wed Sep 10, 2008 7:13 am
Thanks Andy. I'm using the following to test as the array passes into the process page and I'm still getting undefined index. Any thoughts? Anyone?
Code: Select all
$ing=unserialize(base64_decode($_POST['data']));
foreach ($ings as $ing){
print_r($ing);
}
ECJughead
Forum Newbie
Posts: 13 Joined: Fri Mar 07, 2008 9:14 am
Post
by ECJughead » Fri Sep 12, 2008 7:57 am
Still no luck, can someone help?
ykcor
Forum Newbie
Posts: 1 Joined: Fri Sep 12, 2008 11:14 am
Post
by ykcor » Fri Sep 12, 2008 11:17 am
Code: Select all
$ing=unserialize(base64_decode($_POST['data']));
foreach ($ing as $ings){
print_r($ings);
}
i think its coming undefined cuz $ings isent the array, $ing is.
ECJughead
Forum Newbie
Posts: 13 Joined: Fri Mar 07, 2008 9:14 am
Post
by ECJughead » Fri Sep 12, 2008 1:18 pm
Thanks ykcor.
Two new errors: unserialize :Error at offset 0 of 6 bytes AND invalid argument supplied for foreach(). Why is this?
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Wed Sep 17, 2008 9:27 am
verify that you actually have $_POST data to work with, print_r $_POST and see if you come up with anything, at least $_POST['data'].