By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

Array doesn't pass.

P: n/a
Hello all,

I have a form with some checkboxes.
The names of these checkboxes come from an array.
When i click the submit button the resultcode doesn't recognize the names
when i want to check wether or not some checkboxes are ticked.

Assume that i tick checkboxes 100, 150 and 200

Below is some code i've used.(between the ****** lines)
************************************************
if (!isSet($_POST['submit']))
{
ShowForm();
}
else
{
HandleForm();
}

function ShowForm()
{
//normally, these values come out of a database
$question=array();
$question[0]="100";
$question[1]="125";
$question[2]="150";
$question[3]="175";
$question[4]="200";

echo"<form name='form1' method='post' action='".$_SERVER['PHP_SELF']."'>";

for($i=0; $i<5; $i++)
{
echo "<input type='checkbox' name=$question[$i]This is the
question with ID ". $question[$i]."<br/>";
}
echo "<BR><input type='submit' name='submit' value='Show choice'>";
echo"</form>";
}

function HandleForm()
{
for($i=0; $i<5; $i++)
{
if(isset($_POST[$question[$i]]))
{
echo "Question ".$_POST[$question[$i]]." was
ticked.<br/>";
}
}
}
************************************************** ******
Function HandleForm doesn't recognize $_POST[question[$i]]

Can anyone help me solve this problem.

T.i.a.
Regards
Tino Wintershoven
The Netherlands

Nov 4 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Daz
T. Wintershoven wrote:
Can anyone help me solve this problem.
Perhaps you might want consider placing this into the HandleForm
function:
echo "<pre>",print_r($_POST),"</pre>;

This will allow you to see exactly what has been passed back in the
$_POST variable, and what format it's in.

Nov 4 '06 #2

P: n/a
Daz

T. Wintershoven wrote:
Can anyone help me solve this problem.
Another solution might be this:

if (!isSet($_POST['submit']))
{
ShowForm();
}
else
{
HandleForm();
}
// Declare the question array in the global scope.
$question=array();
$question[0]="100";
$question[1]="125";
$question[2]="150";
$question[3]="175";
$question[4]="200";

function ShowForm() {
global $question;

echo"<form name='form1' method='post'
action='".$_SERVER['PHP_SELF']."'>";

for($i=0; $i<5; $i++) {
// This time we are making a unique name for the checkbox
// (i.e question_1, question_2 etc...).
echo "<input type='checkbox' name=$question_$iThis is the "
."question with ID ". $question[$i]."<br/>";
}
echo "<BR><input type='submit' name='submit' value='Show
choice'>";
echo"</form>";

}

function HandleForm() {
global $question;

for($i=0; $i<5; $i++) {
// Create the name for the checkbox we can't to check.
$question_name = "question_".$i;
// This may not be a legit method, but by adding '2' dollar
signs to the beginning
// of the variable, the variable name is correctly parsed as it
should be.
if(isset($_POST[$$question_name])) {
echo "Question ".$_POST[$question[$i]]." was "
."ticked.<br/>";
}
}
}

I haven't tested it, but I hope you get the idea.

Nov 4 '06 #3

P: n/a
Daz wrote:
echo "<pre>",print_r($_POST),"</pre>;
echo "<pre>"; print_r($_POST); echo "</pre>";

Will not print the result value of the print_r() call, which is 1. Or
you could do

echo "<pre>",print_r($_POST, true),"</pre>;

which will make print_r() return a formatted string with the contents
of $_POST instead of the 1 above.
print_r($something, true) *does*not* output anything; the output is
done by the echo.

Nov 4 '06 #4

P: n/a
Daz

Pedro Graca wrote:
Daz wrote:
echo "<pre>",print_r($_POST),"</pre>;

echo "<pre>"; print_r($_POST); echo "</pre>";

Will not print the result value of the print_r() call, which is 1. Or
you could do

echo "<pre>",print_r($_POST, true),"</pre>;

which will make print_r() return a formatted string with the contents
of $_POST instead of the 1 above.
print_r($something, true) *does*not* output anything; the output is
done by the echo.
Hi pedro.

I'm sorry, but you are incorrect in one of your statements.
echo "<pre>",print_r($_POST),"</pre>; will work just fine, I recommend
you try it as I use it all the time. Please note, my use of commas as
opposed to periods.

Best wishes.

Daz.

Nov 4 '06 #5

P: n/a
Daz

Daz wrote:
Pedro Graca wrote:
Daz wrote:
echo "<pre>",print_r($_POST),"</pre>;
echo "<pre>"; print_r($_POST); echo "</pre>";

Will not print the result value of the print_r() call, which is 1. Or
you could do

echo "<pre>",print_r($_POST, true),"</pre>;

which will make print_r() return a formatted string with the contents
of $_POST instead of the 1 above.
print_r($something, true) *does*not* output anything; the output is
done by the echo.

Hi pedro.

I'm sorry, but you are incorrect in one of your statements.
echo "<pre>",print_r($_POST),"</pre>; will work just fine, I recommend
you try it as I use it all the time. Please note, my use of commas as
opposed to periods.

Best wishes.

Daz.
Sorry Pedro. I misread your post.

Nov 4 '06 #6

P: n/a
Daz

Daz wrote:
T. Wintershoven wrote:
Can anyone help me solve this problem.

Perhaps you might want consider placing this into the HandleForm
function:
echo "<pre>",print_r($_POST),"</pre>;

This will allow you to see exactly what has been passed back in the
$_POST variable, and what format it's in.
Please note I accidentally missed a double quote from the end of my
eacho statement. It should have read:

echo "<pre>",print_r($_POST),"</pre>";

Nov 4 '06 #7

P: n/a
Daz wrote:
echo "<pre>",print_r($_POST),"</pre>; will work just fine
Yes, except that it will print an extra "1" (without the quotes).
The extra "1" is the return value of the print_r() call.

Nov 4 '06 #8

P: n/a
"T. Wintershoven" <tw***********@casema.nlwrote in message
news:s7********************@casema.nl...
Hello all,

I have a form with some checkboxes.
The names of these checkboxes come from an array.
When i click the submit button the resultcode doesn't recognize the names
when i want to check wether or not some checkboxes are ticked.

Assume that i tick checkboxes 100, 150 and 200

Below is some code i've used.(between the ****** lines)
************************************************
if (!isSet($_POST['submit']))
{
ShowForm();
}
else
{
HandleForm();
}

function ShowForm()
{
//normally, these values come out of a database
$question=array();
$question[0]="100";
$question[1]="125";
$question[2]="150";
$question[3]="175";
$question[4]="200";

echo"<form name='form1' method='post' action='".$_SERVER['PHP_SELF']."'>";

for($i=0; $i<5; $i++)
{
echo "<input type='checkbox' name=$question[$i]This is the
^^^^^^^^^^^^^
"$question[$i]" will print literally 'array[1]', not '100' like you
intended. If you want it to work, you gotta denote the array reference with
curly braces:

echo "<input type='checkbox' name='{$question[$i]}'This is the...."

Now you'll get '100', '125', etc, which gets us to the second problem:
having plain numbers as field names. Well, perhaps it's not a problem, but
it is something I wouldn't do. The only explanation I can offer for this is
"just because". I'd throw in some alphabets, just in case, name them 'q100',
'q125', etc...

question with ID ". $question[$i]."<br/>";
}
echo "<BR><input type='submit' name='submit' value='Show choice'>";
echo"</form>";
}

function HandleForm()
{
for($i=0; $i<5; $i++)
{
if(isset($_POST[$question[$i]]))
Here you reference correctly the field names 100, 125, etc.. but the reason
they are not found is the fields are actually named array[0]..., like I
explained earlier...
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Nov 6 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.