Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dumb question

Status
Not open for further replies.

gagz

Programmer
Joined
Nov 21, 2002
Messages
333
Location
US
I can't find docs on this syntax anywhere, can someone help me out?
Code:
<input type=&quot;text&quot; name=&quot;report_txt[]&quot; value=&quot;Water&quot;>

specifically the name syntax. is that some sort of group of data tied to one variable? I have multiple textboxes that way with the same name. how do I access in on the back end when the form in processed?

Thanks,
Gagz
 
First off, all text boxes inputs need to have different names if you're going to be tying it in with a database.

Second, I don't think it'll like those brackets [ ]. You probably should get rid of those.

Third. To access the database, do like so.

<form name=&quot;questions&quot; action=&quot;submit.php&quot;>
<input type=&quot;text&quot; name=&quot;userName&quot;>
<input type=&quot;button&quot; name=&quot;Something&quot;
</form>

You need to use the &quot;ACTION attribute in the Form tag

And this question would've been better off in the HTML Forums.... not in PHP.

Hope that helps ya!

-Ken

 
If you are wondering what the use of the brackets are for, (as in name=&quot;report_txt[]&quot;) ... they are used in the names of html forms in order to group data from the form into an array in the php page that processes the submission of the form, instead of single variables of a specified name.

it can be useful in all kinds of circumstances, especially with the for..each loop. you can read more about for...each loops and arrays in the php manual.
 
How do I access the text boxes when the form is processed?

You use the square bracket syntax when you want to allow multiple selection with checkboxes. It is not much different from a multi-select <select> menu.
PHP will give you the variable as an array:
Code:
# HTML
<input type=&quot;checkbox&quot; name=&quot;myVar[]&quot; value=&quot;Water&quot;>
<input type=&quot;checkbox&quot; name=&quot;myVar[]&quot; value=&quot;Fire&quot;>
<input type=&quot;checkbox&quot; name=&quot;myVar[]&quot; value=&quot;Wood&quot;>
<input type=&quot;checkbox&quot; name=&quot;myVar[]&quot; value=&quot;Metal&quot;>
# after POST:
$_POST['myVar'] is an array with all the selections.
Even if there is just 1 box checked, it will be an array.
 
Thanks you guys hit it perfect. Am I to assume that the order they appear on the page is the order they will be filled in the array?
 
They should be.

But you can always fill in the array values to get explicit ordering:

<input type=&quot;checkbox&quot; name=&quot;myVar[0]&quot; value=&quot;Water&quot;>
<input type=&quot;checkbox&quot; name=&quot;myVar[1]&quot; value=&quot;Fire&quot;>
<input type=&quot;checkbox&quot; name=&quot;myVar[2]&quot; value=&quot;Wood&quot;>
<input type=&quot;checkbox&quot; name=&quot;myVar[3]&quot; value=&quot;Metal&quot;>



Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top