Combining Variables in PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sekdab

    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><bod y>";
    echo "<form method=\"post\" value=\"$PHP_SE LF\">";

    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}";
    }
  • Tony Marston

    #2
    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_global s is
    now off by default.

    --
    Tony Marston

    This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL



    "sekdab" <seldan@lore.cc > wrote in message
    news:4b21880f.0 408111051.283ac 98c@posting.goo gle.com...[color=blue]
    >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><bod y>";
    > echo "<form method=\"post\" value=\"$PHP_SE LF\">";
    >
    > 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}";
    > }[/color]


    Comment

    • Matthias Esken

      #3
      Re: Combining Variables in PHP

      sekdab wrote:
      [color=blue]
      > Sorry if this sounds confusing, let me know if any more information is
      > needed. Thanks for the help![/color]

      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

      Comment

      Working...