Page 1 of 1

Arrays and Forms

Posted: Tue Sep 09, 2008 12:42 am
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']));

Re: Arrays and Forms

Posted: Tue Sep 09, 2008 1:53 am
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

Re: Arrays and Forms

Posted: Tue Sep 09, 2008 2:04 am
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?

Re: Arrays and Forms

Posted: Tue Sep 09, 2008 3:07 pm
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>";

Re: Arrays and Forms

Posted: Tue Sep 09, 2008 3:37 pm
by andyhoneycutt
That'll just loop forever. increment $count in your while.

Re: Arrays and Forms

Posted: Tue Sep 09, 2008 3:41 pm
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\" />";
 

Re: Arrays and Forms

Posted: Wed Sep 10, 2008 7:13 am
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);
    }

Re: Arrays and Forms

Posted: Fri Sep 12, 2008 7:57 am
by ECJughead
Still no luck, can someone help?

Re: Arrays and Forms

Posted: Fri Sep 12, 2008 11:17 am
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.

Re: Arrays and Forms

Posted: Fri Sep 12, 2008 1:18 pm
by ECJughead
Thanks ykcor.

Two new errors: unserialize :Error at offset 0 of 6 bytes AND invalid argument supplied for foreach(). Why is this?

Re: Arrays and Forms

Posted: Wed Sep 17, 2008 9:27 am
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'].