471,050 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Combining Variables in PHP

I am a newbie at PHP and at a loss here. I need to iterate over rows
in a database, and display them as editable fields in HTML. I can do
this okay. However, I then need to be able to take a 'snapshot' of
all changes made and update a DB table with a single "Submit" button.
Can this be done? Below is a simple example of what I'm trying to do.

I know it is bad form to try and combine two variables into one, but
do not know what else to do. I cannot get arrays to work with this.
The below code will error out with "$color undefined" Any way to
'combine' two variables as can be done in Korn/Bash?

Sorry if this sounds confusing, let me know if any more information is
needed. Thanks for the help!

<?php
#------------------------------------
# Display Form
#------------------------------------
echo "<html><body>";
echo "<form method=\"post\" value=\"$PHP_SELF\">";

for ($i = 0; $i < 3; $i++) {
echo "<input type=\"text\" name=\"color${i}\">";
}

echo "<input type=\"submit\" name=\"btn\" value=\"Submit\">";
echo "</form></body></html>";
#------------------------------------
# Process Form
#------------------------------------
if (isset($btn)) {
echo "You chose:\n";
echo "${color}{$i}";
}
Jul 17 '05 #1
2 2925
You are getting the "undefined variable" message because you are using
$variable instead of $_POST['variable']. This is because register_globals is
now off by default.

--
Tony Marston

http://www.tonymarston.net
"sekdab" <se****@lore.cc> wrote in message
news:4b**************************@posting.google.c om...
I am a newbie at PHP and at a loss here. I need to iterate over rows
in a database, and display them as editable fields in HTML. I can do
this okay. However, I then need to be able to take a 'snapshot' of
all changes made and update a DB table with a single "Submit" button.
Can this be done? Below is a simple example of what I'm trying to do.

I know it is bad form to try and combine two variables into one, but
do not know what else to do. I cannot get arrays to work with this.
The below code will error out with "$color undefined" Any way to
'combine' two variables as can be done in Korn/Bash?

Sorry if this sounds confusing, let me know if any more information is
needed. Thanks for the help!

<?php
#------------------------------------
# Display Form
#------------------------------------
echo "<html><body>";
echo "<form method=\"post\" value=\"$PHP_SELF\">";

for ($i = 0; $i < 3; $i++) {
echo "<input type=\"text\" name=\"color${i}\">";
}

echo "<input type=\"submit\" name=\"btn\" value=\"Submit\">";
echo "</form></body></html>";
#------------------------------------
# Process Form
#------------------------------------
if (isset($btn)) {
echo "You chose:\n";
echo "${color}{$i}";
}

Jul 17 '05 #2
sekdab wrote:
Sorry if this sounds confusing, let me know if any more information is
needed. Thanks for the help!


Yes, it is confusing. :-)

Let's start with a working example with arrays:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<?php
#------------------------------------
# Process Form
#------------------------------------
if (isset($_POST['btn'])) {
foreach ($_POST['color'] as $key => $value) {
echo "Color $key = $value<br>";
}
}

#------------------------------------
# Display Form
#------------------------------------
echo "<form method='post' value='$_SERVER[PHP_SELF]'>";
for ($i = 0; $i < 3; $i++) {
echo "<input type='text' name='color[$i]'>";
}
?>
<input type='submit' name='btn' value='Submit'>
</form>
</body>
</html>
Regards,
Matthias
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Magnus Lie Hetland | last post: by
2 posts views Thread by Brian | last post: by
3 posts views Thread by Unregistered | last post: by
2 posts views Thread by Confessor | last post: by
3 posts views Thread by hurry | last post: by
7 posts views Thread by odysseus654 | last post: by
reply views Thread by leo001 | last post: by

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.