Jump to content

$_POST array variables


robcrozier

Recommended Posts

ok, im trying to retrieve some variables which were created using a form. Each variable was initialized like so:
[code]

<input name="page_name<? $i ?>" type="text" class="style183" size="40">

[/code]

then if the form is submitted i'm trying to access the variables like this:
[code]

for ($i = 1; $i <= (($_SESSION['no_of_pages'])+2); $i++)
{
    $page_name[$i] = $_POST['page_name[$i]'];
    echo "".$page_name[$i];
}

[/code]

Its the $_POST['page_name[$i]']; bit that i think may be wrong. 

Can anyone shed any light on this?

Cheers!
Link to comment
https://forums.phpfreaks.com/topic/26047-_post-array-variables/
Share on other sites

Not sure I really get it but

I think it should be:

[code]for ($i = 0; $i <= (($_SESSION['no_of_pages'])+2); $i++)
{
    $page_name = $_POST['page_name[$i]'];
    echo "".$page_name;
}
[/code]

why use $i on page_name?
Arrays are zero based. Start at 0.

Hope this helps you. If not, sorry that I might of misunderstood your code.
Link to comment
https://forums.phpfreaks.com/topic/26047-_post-array-variables/#findComment-119104
Share on other sites

Going back to the OP's original code. If the input line is
[code]
<input name="page_name<? $i ?>" type="text" class="style183" size="40">
[/code]
as was stated, then none of the solutions will work since the above statement will not really work as intended. The correct line would be either
[code]
<input name="page_name<?php echo $i ?>" type="text" class="style183" size="40">
[/code]
or
[code]
<input name="page_name[<?php echo $i ?>]" type="text" class="style183" size="40">
[/code]

I personally prefer the second form, since then the incoming values can be referenced as values in the $_POST['page_name'] array:
[code]<?php
for ($i=0;$i<count($_POST['page_name']);$i++) {
//
//  do your code
//
}
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/26047-_post-array-variables/#findComment-119121
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.