Home > Archive > PHP Language > May 2006 > How to POST an array from a form?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to POST an array from a form?
|
|
| Jack L. 2006-05-24, 6:58 pm |
| Hello group.
I have read that if one wants to create an array in a form, then one should
type something like:
<form>
for($i = 0; i < 3; i++) {
<input name="firstname[]" type="text">
}
</form>
This could turn out useful if one wanted to access an array instead of
multiple variables (eg "lastname", "age", etc.), or have access to all three
first names (using print $firstname[] somewhere else in the code), typed by
the user.
But it seems as if PHP5 doesn't like this, at least when I want to print the
'name' variable, as shown in the code shown below. After typing the name in
the form, PHP returns "Hello, Array". If I change $_POST['name'] to
$_POST['name[0]'], I just get "Hello, ". What is wrong?
<?php
if (array_key_exists('name',$_POST)) {
print "Hello, ". $_POST['name'];
} else {
print<<<_HTML_
<form method="post" action="$_SERVER[PHP_SELF]">
Your name: <input type="text" name="name[]">
<br/>
<input type="submit" value="Click Me">
</form>
_HTML_;
}
?>
--
Mvh. / Best regards,
Jack L.,
Copenhagen, EU
| |
| David Haynes 2006-05-24, 9:57 pm |
| Jack L. wrote:
> Hello group.
>
> I have read that if one wants to create an array in a form, then one
> should type something like:
>
> <form>
> for($i = 0; i < 3; i++) {
> <input name="firstname[]" type="text">
> }
> </form>
>
> This could turn out useful if one wanted to access an array instead of
> multiple variables (eg "lastname", "age", etc.), or have access to all
> three first names (using print $firstname[] somewhere else in the code),
> typed by the user.
>
> But it seems as if PHP5 doesn't like this, at least when I want to print
> the 'name' variable, as shown in the code shown below. After typing the
> name in the form, PHP returns "Hello, Array". If I change $_POST['name']
> to $_POST['name[0]'], I just get "Hello, ". What is wrong?
>
> <?php
> if (array_key_exists('name',$_POST)) {
> print "Hello, ". $_POST['name'];
> } else {
> print<<<_HTML_
> <form method="post" action="$_SERVER[PHP_SELF]">
> Your name: <input type="text" name="name[]">
> <br/>
> <input type="submit" value="Click Me">
> </form>
> _HTML_;
> }
> ?>
I think you are confusing an array with a string (although they can
often be used interchangeably in loosely-typed languages).
In the case of your form, the <input type="text"> requires a string not
an array. A <select multiple> would require an array as an example of
where arrays are needed. (radio buttons too)
So, your form should be changed as follows:
Your name: <input type="text" name="name">
You can also call isset() on the $_POST value instead of
array_key_exists(). isset will probably be faster since it is a boolean
on a specific array entry.
if( isset($_POST['name']) ) {
print "Hello, ".$_POST['name'];
} ...
-david-
| |
| Geoff Berrow 2006-05-25, 3:58 am |
| Message-ID: <4474f3c0$0$15782$14726298@news.sunsite.dk> from Jack L.
contained the following:
><form>
>for($i = 0; i < 3; i++) {
> <input name="firstname[]" type="text">
>}
></form>
>
>This could turn out useful if one wanted to access an array instead of
>multiple variables (eg "lastname", "age", etc.), or have access to all three
>first names (using print $firstname[] somewhere else in the code), typed by
>the user.
>
>But it seems as if PHP5 doesn't like this,
It loves it. You're just doing it wrong.
Firstly calling it 'firstname' is confusing. Call it 'names'. Your
loop generates three text boxes all called names[] This tells PHP to
put the output into an array. An array can be thought of as a chest of
drawers. Each element or drawer can contain data. With empty square
brackets PHP will assign numbers to the drawers starting from zero so
that you can access the contents.
The problem is, this will then end up as an array within an array (the
$_POST array)
To simplify things you could do this[1]
$namearray=$_POST['names']
So for your three boxes,
print $namearray[0]; //prints the contents of the first box
print $namearray[1]; //prints the contents of the second box
print $namearray[2]; //prints the contents of the third box
To print the whole lot together with spaces in between
print $namearray[0]." ".$namearray[1]." ".$namearray[2];
or
print implode(" ",$namearray);
--
Geoff Berrow 0110001001101100010000000110
0011011010110110010001101111011001110010
11
1001100011011011110010111001110101011010
11
| |
| Jack L. 2006-05-25, 8:02 am |
| Jack L. wrote:
> Hello group.
>
> But it seems as if PHP5 doesn't like this, at least when I want to
> print the 'name' variable, as shown in the code shown below. After
> typing the name in the form, PHP returns "Hello, Array". If I change
> $_POST['name'] to $_POST['name[0]'], I just get "Hello, ". What is
> wrong?
I got it working, ie now it's showing the submitted data properly, although
I'm not sure why it didn't work before...but for those who are interested:
<?php
$names = 3;
if (array_key_exists('question',$_POST)) {
$this_question = $_POST['question'];
foreach($this_question as $key => $value) {
print $value . "<br>";
}
} else {
print<<<_HTML_
<form method="post" action="$_SERVER[PHP_SELF]">
_HTML_;
for ($i = 0; $i < $names; $i++)
{
print<<<_HTML_
<p>Your name: <input type="text" name="question[]"></p>
_HTML_;
}
print<<<_HTML_
<br/>
<input type="submit" value="Click Me">
</form>
_HTML_;
}
?>
--
Mvh. / Best regards,
Jack L.,
Copenhagen, EU
| |
| Jack L. 2006-05-25, 6:58 pm |
| Geoff Berrow wrote:
> Message-ID: <4474f3c0$0$15782$14726298@news.sunsite.dk> from Jack L.
> contained the following:
>
> Firstly calling it 'firstname' is confusing. Call it 'names'. Your
> loop generates three text boxes all called names[] This tells PHP to
> put the output into an array. An array can be thought of as a chest of
> drawers. Each element or drawer can contain data. With empty square
> brackets PHP will assign numbers to the drawers starting from zero so
> that you can access the contents.
>
Okay, got that :)
> The problem is, this will then end up as an array within an array (the
> $_POST array)
>
I'm a bit , why is this a "problem"? After all, the suggestion below
is as I had thought about.
> To simplify things you could do this[1]
> $namearray=$_POST['names']
>
> So for your three boxes,
> print $namearray[0]; //prints the contents of the first box
> print $namearray[1]; //prints the contents of the second box
> print $namearray[2]; //prints the contents of the third box
>
> To print the whole lot together with spaces in between
> print $namearray[0]." ".$namearray[1]." ".$namearray[2];
>
Thanks for your reply, however! :)
--
Mvh. / Best regards,
Jack L.,
Copenhagen, EU
| |
| Jack L. 2006-05-25, 6:58 pm |
| Hi David...
> I think you are confusing an array with a string (although they can
> often be used interchangeably in loosely-typed languages).
>
No, I was not confusing an array with a string. I'm asking this question as
my goal is to write a small questionnaire site, in which the admin can
choose the number of questions to ask, thus the form input is dynamic, and I
would like to have the questions in an array.
>
> You can also call isset() on the $_POST value instead of
> array_key_exists(). isset will probably be faster since it is a
> boolean on a specific array entry.
>
> if( isset($_POST['name']) ) {
> print "Hello, ".$_POST['name'];
> } ...
>
That's a good advise. Thanks for your help! :)
--
Mvh. / Best regards,
Jack L.,
Copenhagen, EU
| |
| David Haynes 2006-05-25, 6:58 pm |
| Jack L. wrote:
> Hi David...
>
>
> No, I was not confusing an array with a string. I'm asking this question
> as my goal is to write a small questionnaire site, in which the admin
> can choose the number of questions to ask, thus the form input is
> dynamic, and I would like to have the questions in an array.
>
>
> That's a good advise. Thanks for your help! :)
>
>
It's always helpful if you tell us what you intend. Perhaps this code
example will help:
<?php
$questions = array(
'What is your name?',
'What is your quest?',
'What is the terminal wing velocity of a swallow?'
);
if( isset($_POST['answers']) ) {
$i = 0;
foreach( $questions as $question ) {
printf("%s, answer = %s<br>\n", question, $_POST['answers'][$i++]);
}
exit;
}
?>
<form method="post" action="<?php echo $_SERVER[PHP_SELF];?>">
<?php
foreach( $questions as $question ) {
printf("%s: ", $question);
printf("<input type=\"text\" name=\"answers[]\">\n");
printf("<br />\n");
}
?>
<input type="submit" value="Click Me">
</form>
-david-
| |
| Geoff Berrow 2006-05-25, 6:58 pm |
| Message-ID: <44762cdb$0$15784$14726298@news.sunsite.dk> from Jack L.
contained the following:
>
>I'm a bit , why is this a "problem"? After all, the suggestion below
>is as I had thought about.
I'm used to teaching introductory programming to students. They have
problems with the concept of simple arrays. Arrays within arrays freak
them out completely.
--
Geoff Berrow 0110001001101100010000000110
0011011010110110010001101111011001110010
11
1001100011011011110010111001110101011010
11
| |
| David Haynes 2006-05-25, 6:58 pm |
| David Haynes wrote:
> It's always helpful if you tell us what you intend. Perhaps this code
> example will help:
>
> <?php
> $questions = array(
> 'What is your name?',
> 'What is your quest?',
> 'What is the terminal wing velocity of a swallow?'
> );
>
> if( isset($_POST['answers']) ) {
> $i = 0;
> foreach( $questions as $question ) {
> printf("%s, answer = %s<br>\n", question, $_POST['answers'][$i++]);
small typo, this should be:
printf("%s, answer = %s<br>\n", $question, $_POST['answers'][$i++]);
> }
> exit;
> }
> ?>
> <form method="post" action="<?php echo $_SERVER[PHP_SELF];?>">
> <?php
> foreach( $questions as $question ) {
> printf("%s: ", $question);
> printf("<input type=\"text\" name=\"answers[]\">\n");
> printf("<br />\n");
> }
> ?>
> <input type="submit" value="Click Me">
> </form>
>
> -david-
>
|
|
|
|
|