How do i retrieve the data stored in POST??

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
shotos
Forum Newbie
Posts: 10
Joined: Thu Jun 12, 2008 5:54 pm

How do i retrieve the data stored in POST??

Post by shotos »

Hi,
i created a form with method post and am now having problems retrieving the data.
Any help will be welcomed.
here is the form.

Code: Select all

 
<form method="POST" action="create_checklist2.php">
<table border="0" align="center" cellpadding="10" cellspacing="10">
<?php
for ($num = 1; $num <= $num_Items; $num++)
{

print("<tr>");
print("<td>");
print("$num. Check Item $num :<input type=text name=check_item_$num size=20></td>"); 
print("</tr>");
print("<tr>");
print("<td>");
print("Select accompanying Checklist Element :<select name=check_element_$num >");
print("<option value= radio>Radio Buttons</option>");
print("<option value= checkbox>Checkboxes</option>");
print("<option value= textbox>Test Boxes</option>");
print("<option value= drop_menu>Drop-down Menu</option>");
print("</td>");
print("</tr>");
print("<tr>");
print("<td>");
print("Enter Labels for the Chosen Checklist element");
print("</td>");
print("</tr>");
print("<tr>");
print("<td align=center>");
print("Label 1:<input type=text name=label_$num size=20></td>"); 
print("</tr>");
print("<tr>");
print("<td align=center>");
print("Label 2:<input type=text name=label_$num size=20></td>"); 
print("</tr>");
print("<tr>");
print("<td align=center>");
print("label 3:<input type=text name=label_$num size=20></td>"); 
print("</tr>");
print("<tr>");
print("<td>&nbsp</td>\n");
print("</tr>\n");
}
?>
<tr>
<td align="center"><input type="submit" name="submit" value="Submit">
</td>
</table>
</form>
 
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: How do i retrieve the data stored in POST??

Post by Apollo »

You can get this with $_POST[$variableName], for example: $_POST['check_item_1'], $_POST['check_element_1'], etc.

If it doesn't work, try $HTTP_POST_VARS instead of $_POST, especially if you're using an old version of PHP (before 4.1).

Note that inside functions, you may need to define "global $HTTP_POST_VARS" to get access to the global $HTTP_POST_VARS array (this doesn't hold for $_POST, which is a superglobal).
shotos
Forum Newbie
Posts: 10
Joined: Thu Jun 12, 2008 5:54 pm

Re: How do i retrieve the data stored in POST??

Post by shotos »

is there a way i can get the information back with a variable?
example: $_POST['check_item_$num']
cuz i intend on looping it.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: How do i retrieve the data stored in POST??

Post by shiznatix »

if there is nothing else in the $_POST variable then you can do something like this:

Code: Select all

 
foreach ($_POST as $var)
{
    echo $var;
}
 
but I would recommend changing the name of the select list from check_element_$num to check_element[]. If you do that then you have much more control over the post variable incase you want to add something else to be posted. If you do that then you can do this:

Code: Select all

 
if (isset($_POST['check_element']) && is_array($_POST['check_element']))
{
    foreach ($_POST['check_element'] as $key => $val)
    {
        echo $key.' => '.$val.'<br />';
    }
}
 
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: How do i retrieve the data stored in POST??

Post by Apollo »

shotos wrote:is there a way i can get the information back with a variable?
example: $_POST['check_item_$num']
cuz i intend on looping it.
Sure. Well you'd need to use double quotes, otherwise $num won't be evaluated. But basically $_POST[<anything that evaluates to a post variable name>] will do.
Post Reply