Page 1 of 1

How do i retrieve the data stored in POST??

Posted: Sat Jul 19, 2008 5:01 pm
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>
 

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

Posted: Sat Jul 19, 2008 5:25 pm
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).

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

Posted: Sat Jul 19, 2008 6:07 pm
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.

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

Posted: Sat Jul 19, 2008 7:17 pm
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 />';
    }
}
 

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

Posted: Sun Jul 20, 2008 12:52 pm
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.