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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting the id from an input field in php

Status
Not open for further replies.

GoPo

Technical User
Dec 9, 2003
11
0
0
BE
I have created a form that is dynamicly made from databse values that are returned with a select query.
The form excists out of 2 input fields that have a non dynamic name but a dynamic id ie: echo"<input type=text name=teama id=".$row['team_id']>";
How can i fetch just the id from the html tag in php?

---------------------------------------
 
I'm not sure if an html form passes the id variable when the form is submitted. You could rerun the query on the page that the form is submitted to to get the same values or when you get the values in the first place create a session array to hold the values.

MrBelfry
 
actually, I think this is a pretty bad idea for compatiablity reasons - if I remember my mozilla-speak correctly, id and name should be identical - one is used by mozilla and one by IE for exactly the same purpose. splitting them up is a bad idea.

I think that you use the markup language incorrectly - if I understand your code correctly, your code should looks something like this:

echo"<input type=text name=teama id=teama value=".$row['team_id']>";

so that team_id resides in the field's value (assuming team id is it's name ?)

if it's a row number, then you may want to consider using a <select> object with options defined as the teams' names and ids ... that way you also force the user to select a valid choice, and you won't get values like '1!' (which will cause you problems on the posting side of your script, since you'll have to clean up the input - btw, cleaning should be done anyway).
 
I think I understand what you are trying to do--it is fairly common. You are dynamically generating input fields like this:

<input type=text name=teama id="14">

I'm assuming you don't put the id (14) in the value attribute because you actually do want the user to input some value--right?

All you are going to have to work with in the PHP (or ASP, etc) is the element NAME and VALUE. In the client (using javascript for example) you can access all the attributes.

So, one way I handle this is using a common prefix followed by the ID to name the fields. For example:

<input type=text name="TEAM_14_teama">

This allows you in your PHP (or ASP,etc) to iterate through the array of form elements looking for element names that begin with "TEAM_". When you find one, you can split the element name into an array using "_" as the seperator. You'll know that ary[0] = "TEAM" (junk to you), ary[1] = the ID, and ary[2] = the team name. Of course the value will be the user-inputted value.

I have used this method successfully many times. Will this work for you?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top