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

PHP array values with Javascript 1

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
Hi All,
I am pretty much a newbie at PHP but work in Javascript a lot.
Questions have come up in the Javascript forum a number of times recently regarding dynamically generated form fields where that names contain the [] characters like:
<input type="text" name="myField[]">
And they want to be able to address the fields via Javascript but it has trouble with the [] in the field names.

I believe somewhere in the past I read that on the PHP side there was a workaround so that these names could be rendered Javascript friendly without impairing the function on the PHP side. I have searched around and not been able to find any references to such a workaround but would like to find it so that people can be pointed in the right direction for future questions. The [] characters are considered reserved characters in Javascript and it causes all sorts of trouble.

Anyone have more detail on getting around this? I have to refresh the little PHP I knew as it is and so I cannot even pose the question well as a PHP question but would appreciate if someone knows the answer or has a link to a site explaining how to deal with this situation.
A lot of people get frustrated asking a question in the Javascript forum that get's an answer telling them they have to re-write their PHP code but no solid information on how.

Thanks.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Use the attribute "id".

If you have a form field formatted as:

[tt]<input type="text" name="thefield[0]" id="thefield_0"...>[/tt]

you can use the id value in your object references in JavaScript:

document. ... .thefield_0.value






Want the best answers? Ask the best questions! TANSTAAFL!
 
Simple enough especially since I always suggest they use the ID field and DOM methods to reach them for compatibility anyway.

Guess I was looking too hard for a more complex PHP side answer while the obvious stood there hiding in plain sight.

Thanks.

It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top