Jump to content

PHP element control array and JavaScript accessing them


mweinberger

Recommended Posts

Hi All,

 

Suppose I have the following text box elements:

 

<p>Please enter text into the two boxes below.</p>

<input type="text" ID="MyID[]", NAME="MyID[]" />

<input type="text" ID="MyID[]", NAME="MyID[]" />

 

In PHP the posted data will come in as $_POST["MyID"] as an array, which I can then enumerate using a foreach loop. My questions is how would I access these same elements from JavaScript.

 

The standard JavaScript code:

 

var strText1, strText2;
var objMyTextValue1, objMyTextValue2;

objMyTextValue1 = document.getElementById("MyID[]");
objMyTextValue2 = document.getElementById("MyID[]");

strText1 = objMyTextValue1.value;
strText2 = objMyTextValue2.value;

 

The JavaScript code that sets objMyTextValue1 and objMyTextValue2 is obviously missing something. What would the correct code be?

 

Thank you in advance,

 

Marci Weinberger

ButterflyVista

 

you can use a <?php ?> tag in your javascript just like in the html sections. Just be sure to save your page as a .php file

 

for instance:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>test.php</title>
<script type="text/javascript">
var test = "<?php echo 'HELLO WORLD'; ?>";
document.write(test);
</script>
</head>

<body>
</body>
</html>

you would create a javascript control that uses the data posted in an array. I was jus showing you how you could write the javascript with php code, thus making your javascript dynamic at the page load. It's up to you to apply said knowledge ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.