Forms, arrays and retrieval

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
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Forms, arrays and retrieval

Post 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 :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post 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...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

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