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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.