Arrays and Forms

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

Post Reply
ECJughead
Forum Newbie
Posts: 13
Joined: Fri Mar 07, 2008 9:14 am

Arrays and Forms

Post by ECJughead »

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']));
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Arrays and Forms

Post by jaoudestudios »

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

Re: Arrays and Forms

Post by ECJughead »

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

Re: Arrays and Forms

Post by ECJughead »

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>";
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Arrays and Forms

Post by andyhoneycutt »

That'll just loop forever. increment $count in your while.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Arrays and Forms

Post by andyhoneycutt »

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

Re: Arrays and Forms

Post by ECJughead »

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

Re: Arrays and Forms

Post by ECJughead »

Still no luck, can someone help?
ykcor
Forum Newbie
Posts: 1
Joined: Fri Sep 12, 2008 11:14 am

Re: Arrays and Forms

Post by ykcor »

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

Re: Arrays and Forms

Post by ECJughead »

Thanks ykcor.

Two new errors: unserialize :Error at offset 0 of 6 bytes AND invalid argument supplied for foreach(). Why is this?
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Arrays and Forms

Post by andyhoneycutt »

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'].
Post Reply