Combining Variables in PHP

This is a discussion on Combining Variables in PHP within the PHP Language forums, part of the PHP Programming Forums category; I am a newbie at PHP and at a loss here. I need to iterate over rows in a database, ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-11-2004
sekdab
 
Posts: n/a
Default 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}";
}
Reply With Quote
  #2 (permalink)  
Old 08-11-2004
Matthias Esken
 
Posts: n/a
Default Re: Combining Variables in PHP

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
Reply With Quote
  #3 (permalink)  
Old 08-11-2004
Tony Marston
 
Posts: n/a
Default Re: Combining Variables in PHP

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" <seldan@lore.cc> wrote in message
news:4b21880f.0408111051.283ac98c@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}";
> }



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:12 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0