Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2013
    Posts
    141
    Thanks
    22
    Thanked 0 Times in 0 Posts

    on window load pre-check checkbox

    On form load I am trying to pre-check the antivirus checkbox if $result2[30] is equal to 'Yes'. I have tried using the window.onload function but with no results so far. Is there something I have missed or am I going about it the wrong way.
    Any suggestions would be greatly appreciated thanks

    Code:
    <script type="text/javascript">
    window.onload = function(){
    	if(document.form.$result2[30]=='Yes'){
    	document.form.antivirus.checked == true;
    	}
    }
    </script>
    
    <input type="hidden" name="antivirus" value="<?php echo $result2[30]; ?>"/>
    <tr>
    <td><label>Antivirus:</label> <?php echo $result2[30]; ?></td>
    <input type="hidden" id="Antivirus" name="antivirus" value="No"/>
    <td>
    <input type="checkbox" id="Antivirus" name="antivirus" value="Yes"/>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    You must learn to distinguish between PHP and Javascript. The first one will run on the server. The second one will run on the client/browser. As soon as Javascript starts running, PHP will already be finished. So technically speaking: If you want to have access to content of PHP variables in Javascript, you must output this content to HTML/Javascript source code. This is what you did with your echo $result2[30].

    Now you have an HTML input element with name "antivirus" and with a value of whatever value $result2[30] was. In Javascript you CANNOT access the PHP variable, you can only access the HTML input element to which you wrote the content of this variable.

    The problem is now that this input has the same name as another input on the same form. This should be avoided. Please rename your first input to "myvalue" or similar.

    The HTML input can then be accessed as
    Code:
    if(document.getElementsByName('myvalue')[0].value == "Yes")
    Last edited by devnull69; 10-21-2013 at 03:21 PM.

  • #3
    Regular Coder
    Join Date
    Aug 2013
    Posts
    141
    Thanks
    22
    Thanked 0 Times in 0 Posts
    Appreciate the input devnull this is what I have now, I have used the getElementById function but still no joy. I considered what you said and tried to workaround that. Anyway here's my code now:

    Code:
    <script type="text/javascript">
    window.onload = function(){
    	if(document.getElementById('Anti')=='Yes'){
    	document.form.antivirus.checked == true;
    	}
    }
    </script>
    
    <input type="hidden" name="antivirus" id="Anti" value="<?php echo $result2[30]; ?>"/>
    <tr>
    <td><label>Antivirus:</label> <?php echo $result2[30]; ?></td>
    <input type="hidden" id="Antivirus" name="antivirus" value="No"/>
    <td>
    <input type="checkbox" id="Antivirus" name="antivirus" value="Yes" />
    </td>

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    A call to document.getElementById will result in an HTML element object. To get its value, you'll have to use its .value property
    Code:
    if(document.getElementById('Anti').value=='Yes')
    You said you considered what I said ... but you didn't :-) You still have different input elements in the same form with the same name. This only makes sense for radio button groups. For checkboxes and (hidden) text boxes, each of them should have a different name. On top of that, the id attribute MUST be unique in every HTML document.

  • #5
    Regular Coder
    Join Date
    Aug 2013
    Posts
    141
    Thanks
    22
    Thanked 0 Times in 0 Posts
    got it thanks, appreciate the input


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •