Forum Moderators: coopster

Message Too Old, No Replies

Post form radiobox array problem

Problem in php reading from form posted data selection[ ]

         

getrich

4:38 am on Nov 28, 2007 (gmt 0)

10+ Year Member



Hi,
I am having trouble with the below testcase code on one particular server. So I can only think it is an apache or php configuration issue but I have been unable to find anyone else experiencing the same problem through googling.
On submitting, the page should show:
Array ( [field] => This field is ok [option] => Array ( [0] => 1 ) )

but instead only shows:
Array ( [field] => This field is ok [option] => )

I have also tried with file_get_contents('php://input') to see the raw data and it is also not shown so I think PHP is not receiving the data.

If anyone can help I would be very thankful.
Cheers.

Linux 2.6.9-55.0.2.ELsmp #1 SMP
PHP Version 5.2.3
Apache/2.0.52 (CentOS)


<?php
print_r($_POST);
?>
<form method="POST">
Field: <input name="field" value="This field is ok" /><br/>
Option 1: <input type="radio" name="option[]" value="1" checked=""/><br/>
Option 2: <input type="radio" name="option[]" value="2" /><br/>
<input type="submit" />
</form>
<?php
if (!$_POST['option']) {
echo "There is a problem";
} else {
echo "It is working! :) ";
}
?>

Habtom

6:57 am on Nov 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It actually gave me the result you mentioned should be outputting.

Try adding action to the form:

action="<?php echo $_SERVER['php-self']?>"

[edited by: Habtom at 7:06 am (utc) on Nov. 28, 2007]

coopster

10:17 pm on Nov 28, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, getrich.

How about

checked="checked"

in that radio group there?

One note of caution, if you are going to use PHP to setup that form action using PHP_SELF you should always strip_tags() [php.net] for security reasons.

getrich

2:33 am on Nov 29, 2007 (gmt 0)

10+ Year Member



Thanks for your suggestions but it seems to be more complicated than that. Although I did add your suggestions just to be sure, but it made no difference.
Habtom, yes, I expect that most people will not be able to replicate this problem.
Hopefully someone either has the same problem and we can try to focus in on the problem area or someone knows of a fix. My hosting company has not been able to fix it. There 'fix' was to tell me to always use GET instead. lol.
Thanks Coopster but this is just a test case of a large project. I stripped out all the variable checking to simplify it.
Here is another test script for anyone to try and replicate the problem with:

<?php
$data = file_get_contents('php://input');
echo "data:". $data. "<br/>\n";
print_r($_POST);
?>
<form method="POST" action="<?php echo $_SERVER['php-self']?>">
Field: <input name="field" value="This field is ok" /><br/>
Option 1: <input type="checkbox" name="option[]" value="1" checked="checked"/><br/>
Option 2: <input type="checkbox" name="option[]" value="2" /><br/>
Option 3: <input type="checkbox" name="option[]" value="3" checked="checked"/><br/>
<select multiple name="snacks[]">
<option value="coke" selected="selected">CocaCola</option>
<option value="popcorn" selected="selected">Popcorn</option>
<option value="peanuts">Peanuts</option>
</select>
<input type="submit" />
</form>
<?php
if ($_POST['field']) {
if (!$_POST['option']) {
echo "There is a problem";
} else {
echo "It is working! :) ";
}
}
phpinfo(INFO_VARIABLES);
?>

It works for me on a few servers I have tried but does not work on the new hosted server I have just paid for.
Thanks

getrich

8:54 am on Nov 29, 2007 (gmt 0)

10+ Year Member



Found the problem!
It was not with Apache or PHP but Plesk control panel.
I was testing via Plesk Site Preview.
Obviously there is a bug in Plesk that prevents POST Arrays to work.
The site is using Plesk 8.1