473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Array doesn't pass.

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
8 1464
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
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: Gactimus | last post by:
I made the program below. It outputs the smallest number in the array. What I would like to know is how do I output the array location. I am at a loss. For example, since the smallest number in...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
12
by: Uncle | last post by:
I am an untrained hobbyist. Everything about programming I have learned from the internet. Thank you all for your gracious support. This is what I have: #define CONST_CHAR 0 void some_func(...
10
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
24
by: Kavya | last post by:
int main (){ int a={{1,2,3},{4,5,6}}; int (*ptr)=a; /* This should be fine and give 3 as output*/ printf("%d\n",(*ptr)); ++ptr;
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
7
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h>...
15
by: Jess | last post by:
Hi, If I have an array of pointer like: char* a = {"a","b","c"}; then it works fine. Since "a" is effectively "a" char**, I tried the following, which doesn't work: char** a =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.