Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating an array from a form 2

Status
Not open for further replies.

kzn

MIS
Jan 28, 2005
209
GB
Hi I am working my way through a book and can not get the following to work. Please can some one help me. Thanks.

The Form Page

<html>
<head>
<title>Html form</title>
</head>
<body>
<form action="handleform.php" method="post">
First Name <input type="text" name="$array[firstname]" size="20" ><br>
Last Name <input type="text" name="array[lastname]" size="40"><br>
E-Mail <input type="text" name="array[e-mail]" size="60"><br>
Comments <textarea name="array[comments]" rows="5" cols="40"></textarea><br>
<input type="submit" name="submit" value="Submit!">
</form>
</body>
</html>


The Handler Page

<html>
<head>
<title>Form Results</title>
</head>
<body>
<?php

// This page receives and handles the data generated by "form.html".

$array["name"] = $array["firstname"]."
" .$array["lastname"];
print ("Your full name is $array[name].<br>\n");
?>
</body>
</html>
 
This one does not make sense in the PHP. PHP is not able to retrieve what you wanted.
Can you please specify how's the structure of your $array, what data is in there, and what would you like to achieve from this?

Thank you.

But this one make sense:

<form action="handleform.php" method="post">
First Name <input type="text" name="firstname[]" size="20" ><br>
Last Name <input type="text" name="lastname[]" size="40"><br>
E-Mail <input type="text" name="email[]" size="60"><br>
Comments <textarea name="comments[]" rows="5" cols="40"></textarea><br>
<input type="submit" name="submit" value="Submit!">
</form>


Then in your PHP:

if (isset ($_POST["firstname"]))
{
for ($i=0; i<count($_POST["firstname"]); $i++)
{
$name = $_POST["firstname"][$i]." ".$_POST["lastname"][$i];
print("Your full name is $name.<br>\n");
}
}
 
Actually, the OP's original code:
Code:
<html>
<head>
<title>Html form</title>
</head>
<body>
<form action="handleform.php" method="post">
First Name <input type="text" name="$array[firstname]" size="20" ><br>
Last Name <input type="text" name="array[lastname]" size="40"><br>
E-Mail <input type="text" name="array[e-mail]" size="60"><br>
Comments <textarea name="array[comments]" rows="5" cols="40"></textarea><br>
<input type="submit" name="submit" value="Submit!">
</form>
</body>
</html>
would make sense if the line
Code:
First Name <input type="text" name="[b][red]$[/red][/b]array[firstname]" size="20" ><br>
were changed to
Code:
First Name <input type="text" name="array[firstname]" size="20" ><br>
Notice that I removed the dollar sign from the word "array".

Now the handler page should be changed from:
Code:
<html>
<head>
<title>Form Results</title>
</head>
<body>
<?php

// This page receives and handles the data generated by "form.html".

$array["name"] = $array["firstname"]."
" .$array["lastname"];
print ("Your full name is $array[name].<br>\n");
?>
</body>
</html>
to something like
Code:
<html>
<head>
<title>Form Results</title>
</head>
<body>
<?php

// This page receives and handles the data generated by "form.html".

$array['name'] = $_POST['array']['firstname'] . '
' . $_POST['array']['lastname'];
print ('Your full name is '. $array['name'] . "<br>\n");
?>
</body>
</html>
The original code was assuming the register_globals was set to "on" in the PHP.INI file. This hasn't been the default for at least 2 years, so your book must be old (or the author used an old reference to write it). Please see for more information.

If I were writing code like this in the "real" world, I would combine the code into one script, so that when any errors are found with the input, the form can be redisplayed witht the appropriate error messages and the use doesn't need to press the "Back" button (which can be confusing).

A reminder to all posters, when you post code, PLEAE remember to surround your code with the TGML tags [red][ignore]
Code:
[/ignore][/red]
.

Ken
 
Thanks Kenrbnsn, you were spot on. I have spent hours trying to work that out. The book is a bit old, Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top