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, ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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}"; } |
|
|||
|
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 |
|
|||
|
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}"; > } |