Is there a way to make a form name a variable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cocotheflyingnut
    New Member
    • Nov 2007
    • 3

    Is there a way to make a form name a variable?

    I'm building a chart, and it's going through a for loop, which creates new text boxes. I want to be able to save the information put in each text box in some sort of variable array.

    Right now it's only letting me save the very last one in a variable I've named $memo. I thought that if I echoed $memo within the loop it would print out each text box's input, but it's only taking the last one and printing that to the screen the amount of textboxes that I have.

    This is basically the code in the for loop which is in a form:[code=php]
    {
    <input type="text" size="5" name="memo" id="memo" value="" />

    $memo = $_POST['memo'];
    echo $memo;

    }
    <input type="submit" name="Save" value="Save" />[/code]

    Is there a way to set up my text input so that I can do some sort of variable array, like [code=php]
    <input type="text" size="5" name=$memo[$i] id="memo" value="" />
    $i++;[/code]

    Any suggestions? Thanks!

    -Coco
    Last edited by pbmods; Nov 27 '07, 10:23 PM. Reason: Added CODE tags.
  • Lumpy
    New Member
    • Oct 2007
    • 69

    #2
    I am not sure if I completely understand what you are wanting to do, but anyway to answer what I think your question is, yes you can do something like that.

    You would want to setup your memo variable something like this....

    [CODE=php]

    $memo[] .= $_POST['memo'];

    [/CODE]

    And that will create the array variable $memo and add each entry after the last one. So the first key in the array would be 0, or $memo[0].

    Then to call the variables in the input section, you would simply use an echo statement something like....

    [CODE=php]

    echo '<input type="text" size="5" name="'.$memo[$i].'" id="memo" value="" />';

    [/CODE]

    Than each loop through, you will get a different name for that input, as long as you continue to increase $i.

    Hope this helps!

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      To nitpick on Lumpy's post:

      [code=php]
      $memo[] .= $_POST['memo'];
      [/code]

      is technically not correct and will generate an E_NOTICE error, because this statement attempts to concatenate a variable that does not exist.

      [code=php]
      $memo[] = $_POST['memo'];
      [/code]
      is the correct method (and produces the expected result).

      Comment

      • cocotheflyingnut
        New Member
        • Nov 2007
        • 3

        #4
        That's exactly what I want to do, but for some reason it isn't working. I want to echo out the $memo[] array of what was typed in each text box. Any suggestions to what I have done up? Right now it's not echoing out anything.


        [PHP]<?
        $memo[] = $_POST['memo'];

        for($j=0;$j<=6; $j++){
        echo '
        <form name="saving" method="post" action="">
        <td valign="top" height="50" class="Text"><p align="center">
        <input type="text" size="5" name="'.$memo[$j].'" id="memo" value="" /></td>';

        if(isset( $Save )){

        echo $memo[$j];


        }

        }
        echo '</tr>

        <input type="submit" name="Save" value="Save" />
        </form>';
        ?>[/PHP]

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Coco.

          Try this instead:
          [code=php]
          ?>
          <form name="saving" method="post" action="">
          <table>
          <tr>
          <?php
          for( $j = 0; $j < 6; ++$j )
          {
          ?>
          <td valign="top" height="50" class="Text">
          <p align="center">
          <input type="text" size="5" name="memo[]" value="" />
          </p>
          </td>
          <?php
          }
          ?>
          </tr>
          </table>
          [/code]

          When the User submits the form, $_POST['memo'] will contain an array.

          Comment

          • cocotheflyingnut
            New Member
            • Nov 2007
            • 3

            #6
            Thank you! That made my program work perfectly!

            JOSH

            Comment

            Working...