Page 1 of 1

Forms, arrays and retrieval

Posted: Fri Jul 25, 2003 4:43 pm
by mikusan
My question is related to how checkboxes work, however, while checkboxes were easy to manage I have found the need to do the following to keep my code organized but could not get the results i wanted:

Code: Select all

<input type="Text" name="newsїcomp_name]">
<input type="Text" name="newsїname]">
to keep it short those are my two input fields, as you see if you have many sections in one form it would be nice to carry them as arrays throughout so that news[name] is different from say user[name].

Then my attempt is to catch this information from the form and use associative arrays to do so:

Code: Select all

$post_news = $_POST['news'];
echo($post_news['email']);
But this seems to just give me blank pages... can anyone give me a suggestion? am i getting something wrong here... or is it right and i better check my other 400 lines of code?

Thanks guys, i appretiate it. Thanks in advance... i don't want to post a thank to bump this thread up for no reason, so accept it in advance :)

Posted: Sat Jul 26, 2003 10:59 am
by twigletmac
On the page you submit to put:

Code: Select all

echo '<pre>';
print_r($_POST);
echo '</pre>';
to see exactly what's in the $_POST array.

Mac

Posted: Mon Jul 28, 2003 4:42 am
by mikusan
Well i know what's in the $_POST array, i typed it in ;)
thing is that i am trying to retrieve from the form an array with all the values submitted to me. If i wasn't clear enough please tell me.

Posted: Mon Jul 28, 2003 5:15 am
by twigletmac
mikusan wrote:Well i know what's in the $_POST array, i typed it in ;)
thing is that i am trying to retrieve from the form an array with all the values submitted to me. If i wasn't clear enough please tell me.
I was suggesting that you used that code to check to make sure that things are being passed through correctly as I understood from your initial post that there may be a problem with accessing the data.

In order to retrieve the information you need to be 100% sure of how it is being passed. print_r() allows you to see the structure of the array and each element within it.

Mac

Posted: Mon Jul 28, 2003 6:12 am
by mikusan
Hehe okay here is the output:

Code: Select all

Array
(
    [news] => Array
        (
            [comp_name] => secret
            [name] => myself
            [email] => 123@hotmail.com
        )
)
As expected, now how do i retrieve this array keeping the associative reference intact...

Posted: Mon Jul 28, 2003 8:39 am
by twigletmac
Just because you're expecting something to work one way doesn't mean that's how it's working and whether variables are being passed correctly should always be the first thing you check.

Since all that seems fine you should be able to do:

Code: Select all

$post_news = $_POST['news']; 
echo '<pre>';
print_r($post_news);
echo '</pre>';
however, this kind of thing wasn't working for you before so something else must be going on. Do you do any processing of the $_POST array before that code?

Mac

Posted: Mon Jul 28, 2003 5:54 pm
by mikusan
Thanks a lot twigletmac, i have found my problem was somewhere else in my code, and that my code was right. That's why i couldn't figure out why the heck my expectation and what i saw did not match the result. It seems that's been my trend lately as i just use vim to code all of my code.

Great help thanks i appretiate it.