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

Referrencing NAME=ARRAY[NAME] in Javascript

Status
Not open for further replies.

rhowes

IS-IT--Management
Jun 6, 2001
140
ZA
Hi

I am using INPUT names like NAME=ARRAY[FIRSTNAME]

How do I referrence that in Javascript. If I use NAME=A_VAR it works fine
and I referrence with

document.form.a_var.value

But with an array

document.form.array[name].value returns an error.

Thanks.
Richard

 
Try
replace the "name" with the corrisponding array element or number. or numeric var
document.form.array[1].value
or
var my_name = 2;
document.form.array[my_name].value
 
Thanks for the advice, but still no joy. My input field is:

<input type=&quot;text&quot; name=&quot;contact_details[pr_name_first]&quot; value=&quot;<?php echo($contact_details[pr_name_first]); ?>&quot;>

(pr_name_first is the name of my field in MySQL as well. If I use your suggestion:

document.form.contact_details[1].value

I get this error:

document.form.contact_details.1 is null or not an object

If I use:

var my_name = 2;
document.form.array[my_name].value

I get error:

document.form.contact_details[...] is null or not an object

I seem to remember reading somewhere that there is a special way of referrencing arrays in Javascript (in the context I am doing), but I cant remember what.

Any other ideas?
Thanks
Richard



 
All php refs must be placed within ?php tags (error in your code at name pair).
All JavaScript variables (if PHP initialized) must be established prior to any JavaScript code, otherwise your JS array vars will be empty.
i.e. <?php echo code to connect to DB and load up $data=mysql_fetch_array($result) ?> ...then <script language=&quot;JavaScript&quot;>
Code:
=<?php echo $data[first_name] ?>;]</script>
I hope this helps sometimes I am off-base.  It is so important to &quot;dot the i's and cross the t's&quot;
please post your errors
 
how 'bout trying this instead:

$sql=&quot;SELECT * FROM contacts&quot;;
$res=mysql_query($sql,$con) or die(&quot;SQL: $sql&quot;);
While ($row=mysql_fetch_array($res)) {
$pr_name_first=$row['pr_name_first'];
//
print &quot;<input type=\&quot;text\&quot;&quot;;
print &quot; name=\&quot;contact_details[pr_name_first]\&quot;&quot;;
print &quot; value=\&quot;$pr_name_first\&quot;>&quot;;
//
// whatever else u want
//
}

I've done it like this to create a module that creates an online &quot;programmable&quot; questionaire and all the form fields you add for each question are referenced as arrays like:
<input type=&quot;text&quot; name=&quot;Q[1]&quot; value=&quot;$Q[1]&quot;>
and when submitted, the php script pulls them out as array
values like this:
foreach ($Q as $value) {
$sql=&quot;INSERT INTO $table (id,value) VALUES (NULL,'$value')&quot;;
mysql_query($sql,$con) or die(&quot;Unable to save $value&quot;);
}


as for referencing them in javascript BEFORE the form is submitted, you can access them just as said above:

document.myform.contact_details['pr_name_first'].value
or
document.myform.contact_details['pr_name_first'].src
(if you wanna change what it is holding for something else)

hope it helps.

I've got lots of working examples if u wanna look.
just lemme know
 
Hi Guys

Thanks for all the advice. Actually I managed to solve the problem by referring to my field names in Javascript like this:

document.contact.elements[&quot;array[key]&quot;].disabled=true

This disables certain fields that I do not want modified. The trick was the elements[&quot;... bit.

What your suggestions have done (thank you again) is get me thinking about a different approach to my development. I am passing a lot of variables (mainly flags) from javascript to PHP and back - not a good idea I am told (at least not from Javascript to PHP anyway).

Thanks again. As you can tell I am very new at PHP and my other development skills are rusty.

Cheers
Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top