bravo14 Posted May 16, 2014 Share Posted May 16, 2014 Hi I am using the following PHP code to create a table for a form <tr><td><img src="images/gate_<?php echo $rider_row['colour'];?>.png"/> <input name="id[]" type="hidden" value="<?php echo $rider_row['id'];?>"/></td><td><input readonly="readonly" name="rider[]" value="<?php echo $rider_row['rider_name'];?>" class="txtfield"/><br /> <select name="substitute[]" class="txtfield" style="display:none"> <option value="">-----------------</option> <?php foreach($reserves as $key=> $value): echo '<option value="'.$value.'">'.$value.'</option>'; //close your tags!! endforeach; ?> </select></td><td><select name="points[]" class="txtfield" onchange="test();"> <option value="">-----------------</option> <?php foreach($scoring as $key=> $value): echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!! endforeach; ?> </select><br/> <select name="subpts[]" class="txtfield" style="display:none"> <option value="">-----------------</option> <?php foreach($scoring as $key=> $value): echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!! endforeach; ?> </select> </td></tr> On changing the value in the field called points[], if certain values are selected then two other fields are to be displayed, I am using the following function <script> function test() { if (document.getElementByName('points[]').value == 'F2 || E || N') { document.getElementByName('substitute[]').style.display = ''; document.getElementByName('subpts[]').style.display = ''; } else { document.getElementByName('substitute[]').style.display = 'none'; document.getElementByName('subpts[]').style.display = 'none'; } }</script> I am getting an error saying getElementByName is not a function, I know I can do this by getElementById, but the fields do not have ID and not sure how I can give them a unique ID when echoing them from the database. If someone could just give me some pointers I would be very grateful Quote Link to comment https://forums.phpfreaks.com/topic/288551-javascript-function-and-php/ Share on other sites More sharing options...
requinix Posted May 16, 2014 Share Posted May 16, 2014 getElementsByName Quote Link to comment https://forums.phpfreaks.com/topic/288551-javascript-function-and-php/#findComment-1479776 Share on other sites More sharing options...
bravo14 Posted May 17, 2014 Author Share Posted May 17, 2014 Thanks, I've changed it to that, but now when changing the value in the select field I get the following error TypeError: document.getElementsByName(...).style is undefined The javascript function says <script> function test() { if (document.getElementsByName('points[]').value == 'F2') { document.getElementsByName('subpts[]').style.display = ''; } else { document.getElementsByName('subpts[]').style.display = 'none'; } }</script> and the php says <tr><td><img src="images/gate_<?php echo $rider_row['colour'];?>.png"/> <input name="id[]" type="hidden" value="<?php echo $rider_row['id'];?>"/></td><td><input readonly="readonly" name="rider[]" value="<?php echo $rider_row['rider_name'];?>" class="txtfield"/><br /> <select name="substitute[]" class="txtfield" style="display:none"> <option value="">-----------------</option> <?php foreach($reserves as $key=> $value): echo '<option value="'.$value.'">'.$value.'</option>'; //close your tags!! endforeach; ?> </select></td><td><select name="points[]" class="txtfield" onchange="test();"> <option value="">-----------------</option> <?php foreach($scoring as $key=> $value): echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!! endforeach; ?> </select><br/> <select name="subpts[]" class="txtfield" style="display:none"> <option value="">-----------------</option> <?php foreach($scoring as $key=> $value): echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!! endforeach; ?> </select> </td></tr> the style is set in the php as display:none Quote Link to comment https://forums.phpfreaks.com/topic/288551-javascript-function-and-php/#findComment-1479802 Share on other sites More sharing options...
requinix Posted May 17, 2014 Share Posted May 17, 2014 Because, as its name may imply, it returns more than one node. A node list, as it were. You could grab the [0] if that's all you want. There's another way to get those though: get the form and do, like, ["points[]"]. Quote Link to comment https://forums.phpfreaks.com/topic/288551-javascript-function-and-php/#findComment-1479810 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.