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

Form array help 1

Status
Not open for further replies.

Guern

Programmer
Jun 22, 2001
91
GB
Hi,

I use PHP primarily and have some form variables (text fields) on a form as follows

ip[1372]
ip[5436]
ip[7618]

I've trying to update these dynamically when a user clicks a text box. The number in the brackets is a key for the php array, not the number of elements in the array, which from the above example would be 3.

When the form is submitted to php script, I just foreach through. I'm aware you can't use named keys in javascript arrays, but I've tried also addressing them directly as 0, 1, 2, etc, but can't figure out how to set them

I've been trying

document.formtest.ip[0].value='';
document.formtest.ip[1372].value='';
document.formtest.elements["failip[]"][$i].value="0.0.0.0"
document.formtest.elements["failip[$i]"].value="0.0.0.0"

and various other permutations.

Can anyone point me in the right direction?

Thanks.




Paul Smith
Microtech Limited
 
Hi

Code:
[s]document[teal].[/teal]formtest[teal].[/teal]ip[teal][[/teal][purple]1372[/purple][teal]].[/teal]value[teal]=[/teal][green][i]''[/i][/green][teal];[/teal][/s]

document[teal].[/teal]formtest[teal].[/teal]elements[teal][[/teal][green][i]'ip[1372]'[/i][/green][teal]].[/teal]value[teal]=[/teal][green][i]''[/i][/green][teal];[/teal]

[gray]// or trying to reproduce your strange reference[/gray]

$ip[teal]=[/teal][green][i]'whatever'[/i][/green]
failip[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Array[/color][teal]()[/teal]
failip[teal][[/teal][green][i]'whatever'[/i][/green][teal]]=[/teal][purple]1372[/purple]
document[teal].[/teal]formtest[teal].[/teal]elements[teal][[/teal][green][i]'ip['[/i][/green][teal]+[/teal]failip[teal][[/teal]$ip[teal]]+[/teal][green][i]']'[/i][/green][teal]].[/teal]value[teal]=[/teal][green][i]''[/i][/green]
Guern said:
I'm aware you can't use named keys in javascript arrays
Then I am wrong. I just used one...

Feherke.
 
Thank you very much. That works fine. I had just read through a few javascript tutorials and the names keys bit was explained there. Obviously either incorrect or I got the wrong end of the stick (most likely).

Once again, thanks for your help.

Paul Smith
Microtech Limited
 
Hi

Paul said:
I had just read through a few javascript tutorials and the names keys bit was explained there. Obviously either incorrect or I got the wrong end of the stick (most likely).
Actually such things are just [tt]Object[/tt]s, so my above code is not logically correct. But you can see such codes all over the web and usually are mentioned as arrays. However, you can do the same, for example, with a [tt]Date[/tt] too :
Code:
failip[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]()[/teal]
failip[teal][[/teal][green][i]'whatever'[/i][/green][teal]]=[/teal][purple]1372[/purple]
Becomes clear that is not a native [tt]Array[/tt] when using JSON array declaration :
Code:
failip[teal]=[[/teal] [green][i]'whatever'[/i][/green][teal]:[/teal] [purple]1372[/purple] [teal]][/teal]
Because you have to declare it as an object :
Code:
failip[teal]=[/teal][teal]{[/teal] [green][i]'whatever'[/i][/green][teal]:[/teal] [purple]1372[/purple] [teal]}[/teal]
Depends on what tutorial are you reading, the authors points of view may vary.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top