Form input name from a variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • monion
    New Member
    • Sep 2009
    • 18

    Form input name from a variable

    One more question...
    I have pulled my query line by line into my php form. Each line/row will have an input field. How can I dynamically assign a variable to the "name" field, for instance if I want the name= to be the value of the item_no.


    My updated code:

    Code:
    <form action="inventorymaster_form.php" method="post" name="inventorymaster_form" align="left" class="style3" bgcolor="#ffffff" onSubmit="return verifyForm(this);">
     
    <legend>INVENTORYMASTER</legend>
    <table border="1" class="mainmenu" border="0" cellpadding="0" cellspacing="0">
    <?php
    $i=0;
    while ($i<$num) {
    $f1=mysql_result($result,$i,"id");
    $f2=mysql_result($result,$i,"Vendor");
    $f3=mysql_result($result,$i,"item_no");
    $f4=mysql_result($result,$i,"description");
    ?>
    <tr>
    <td> &nbsp &nbsp <?php echo $f1;?>&nbsp &nbsp </td>
    <td> &nbsp &nbsp <?php echo $f2;?>&nbsp &nbsp </td>
    <td> &nbsp &nbsp <?php echo $f3;?>&nbsp &nbsp </td>
    <td> &nbsp &nbsp <?php echo $f4;?>&nbsp &nbsp </td>
    <td align="center"> <input type='text' [B]name=$f3 [/B]value="<?php echo "$i";?>">
       
    </tr>
    <?php
    $i++;
    }
    ?>
    </table>
    </form>
    Thanks again,
    MO
    Last edited by Dormilich; Sep 21 '09, 09:57 PM. Reason: Please use [code] tags when posting code
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    I am a little confused at what you want. First thing though:
    Line 18 has name=$f3
    Should be name="<?php echo $f3; ?>" like you ahve for the others.

    Second, why don't you include your output (echo) in a loop - like for example in your while loop? Would that be what you want?

    Comment

    • prabirchoudhury
      New Member
      • May 2009
      • 162

      #3
      yap ,TheServant right . i am adding few more thing

      1. echo the php variables in right way
      2. space between two var in while condition while ($i < $num)
      3. mysql_result() function is slower than mysql_fetch_row (), mysql_fetch_arr ay(), mysql_fetch_ass oc() and mysql_fetch_obj ect().


      cheers

      :)

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hey.

        You can assign array indexes to the name of a input tag.

        For example:
        Code:
        <input type='text' name='value[1]' value='first' /><br />
        <input type='text' name='value[2]' value='second' /><br />
        <input type='text' name='value[3]' value='third' /><br />
        <input type='text' name='value[4]' value='fourth' />
        Submitted to PHP, the $_POST['value'] element would be an array
        Code:
        Array
        (
            [1] => first
            [2] => second
            [3] => third
            [4] => fourth
        )
        You could create input boxes for whichever columns you needed updated, using the name of the column as they input name, and then put the ID of the column in as the array index.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by TheServant
          Second, why don't you include your output (echo) in a loop - like for example in your while loop? Would that be what you want?
          It *is* inside his loop. Take a closer look ;-)
          Originally posted by prabirchoudhury
          1. echo the php variables in right way
          2. space between two var in while condition while ($i < $num)
          #1. There is no "right" way to echo variables. Only "preferred" ways. (Given that it is syntactically correct, of course. (Which it is))
          Sure, the method he uses is usually considered a bit sloppy, but it is in no way "wrong".

          #2. This doesn't matter in the slightest, and is to a matter of personal preference. Many programmers actually prefer not to leave white-spaces in their conditional statements.

          Not trying to sound argumentative here, but...

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            Originally posted by Atli
            It *is* inside his loop. Take a closer look ;-)
            My mistake. Thanks Atli ;)

            Comment

            Working...