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

Can't refer to array index

Status
Not open for further replies.

Dronealone

IS-IT--Management
Mar 13, 2002
64
GB
Hi,

I have the following code which is supposed to open a window with the url query string being created from the input boxes:

<input type='checkbox' class='col3' name='monitor[1]' value='1'>
<input type='text' class='col4 text' name='monitor_url[1]' value=''>
<input type='text' class='col5 text' name='monitor_string[1]' value=''>
<input type="button" class="goAnchor col5" value="Test Monitoring" onClick="javascript:win2col('/fetching/test_monitoring.php?url=' + monitor_url[1].value + '&string=' + monitor_string[1].value)">

The reason I am using the monitor_url[1] type syntax is that there are many of these boxes. When the button is clicked it tells me that monitor_url is not defined.

Can anybody tell me what I am doing wrong?

Many thanks.
 
sure,
u dont need to give a number, ur browser will do it on its own.
e.g:
<input type="text" name="txt" value="1">

<input type="text" name="txt" value="2">

this will automatically change txt to an array.
e.g:
alert(document.FormName.txt[0].value) //will give 1
alert(document.FormName.txt[1].value) //will give 2


Known is handfull, Unknown is worldfull
 
Your page doesn't look like you are using javascript at all. Given you posted to the javascript forum, I can only assume a simple mistake...

I think you want to name the input elements according to the contents of an array. You can do this... but not quite the way you are doing it now.

Here is one solution (I have defined a form name -- substitute in your own):

Code:
<form name="myForm">
<script type="text/javascript">
var _html = '';
_html += '<input type="checkbox" class="col3" name="' + monitor + '" value="1">';
_html += '<input type="text" class="col4 text" name="' + monitor_url + '" value="">';
_html += '<input type="text" class="col5 text" name="' + monitor_string + '" value="">';
_html += '<input type="button" class="goAnchor col5" value="Test Monitoring" onClick="javascript:win2col(\'/fetching/test_monitoring.php?url=\' + document.myForm.' + monitor_url + '.value + \'&string=\' + document.myForm.' + monitor_string + '.value)">';
document.write(_html);
</script>
</form>

Hope that gets you started,
Jeff
 
Hi,

thanks for your replies, however I don't think I've made it clear what I need to get done.

The checkbox, and two textbox fields and button, are all part of a much larger form which is dynamically output via PHP.

The reason the variable names have been indexed with the [] brackets is because the numbers within these correspond to database ids.

Where I need to use javascript is to is build the url which is opened in a new window when the button is clicked. So really what I need is to know how to access the value of monitor_url[1] and monitor_string[1] and append these to the query string of a url, as I have tried to do with the snippet below:

<input type="button" class="goAnchor col5" value="Test Monitoring" onClick="javascript:win2col('/fetching/test_monitoring.php?url=' + monitor_url[1].value + '&string=' + monitor_string[1].value)">

My problem is that javascript tells me that monitor_string is not defined.

Any ideas?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top