By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

Form input name from a variable

P: 18
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:

Expand|Select|Wrap|Line Numbers
  1. <form action="inventorymaster_form.php" method="post" name="inventorymaster_form" align="left" class="style3" bgcolor="#ffffff" onSubmit="return verifyForm(this);">
  2.  
  3. <legend>INVENTORYMASTER</legend>
  4. <table border="1" class="mainmenu" border="0" cellpadding="0" cellspacing="0">
  5. <?php
  6. $i=0;
  7. while ($i<$num) {
  8. $f1=mysql_result($result,$i,"id");
  9. $f2=mysql_result($result,$i,"Vendor");
  10. $f3=mysql_result($result,$i,"item_no");
  11. $f4=mysql_result($result,$i,"description");
  12. ?>
  13. <tr>
  14. <td> &nbsp &nbsp <?php echo $f1;?>&nbsp &nbsp </td>
  15. <td> &nbsp &nbsp <?php echo $f2;?>&nbsp &nbsp </td>
  16. <td> &nbsp &nbsp <?php echo $f3;?>&nbsp &nbsp </td>
  17. <td> &nbsp &nbsp <?php echo $f4;?>&nbsp &nbsp </td>
  18. <td align="center"> <input type='text' name=$f3 value="<?php echo "$i";?>">
  19.  
  20. </tr>
  21. <?php
  22. $i++;
  23. }
  24. ?>
  25. </table>
  26. </form>
Thanks again,
MO
Sep 21 '09 #1
Share this Question
Share on Google+
5 Replies


TheServant
Expert 100+
P: 1,168
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?
Sep 21 '09 #2

prabirchoudhury
100+
P: 162
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_array(), mysql_fetch_assoc() and mysql_fetch_object().


cheers

:)
Sep 22 '09 #3

Atli
Expert 5K+
P: 5,058
Hey.

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

For example:
Expand|Select|Wrap|Line Numbers
  1. <input type='text' name='value[1]' value='first' /><br />
  2. <input type='text' name='value[2]' value='second' /><br />
  3. <input type='text' name='value[3]' value='third' /><br />
  4. <input type='text' name='value[4]' value='fourth' />
Submitted to PHP, the $_POST['value'] element would be an array
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [1] => first
  4.     [2] => second
  5.     [3] => third
  6.     [4] => fourth
  7. )
  8.  
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.
Sep 22 '09 #4

Atli
Expert 5K+
P: 5,058
@TheServant
It *is* inside his loop. Take a closer look ;-)
@prabirchoudhury
#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...
Sep 22 '09 #5

TheServant
Expert 100+
P: 1,168
@Atli
My mistake. Thanks Atli ;)
Sep 22 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.