Jump to content

javascript function and PHP


bravo14

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/288551-javascript-function-and-php/
Share on other sites

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

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.