Page 1 of 1

html form on load pre-check checkbox

Posted: Mon Oct 21, 2013 6:31 am
by sectionLeader123

Code: Select all

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 

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

Re: html form on load pre-check checkbox

Posted: Mon Oct 21, 2013 12:33 pm
by requinix

Code: Select all

if(document.form.$result2[30]=='Yes'){
You can't mix PHP and Javascript like that.

Fortunately you don't actually need to in the first place. Drop the whole Javascript thing and just use

Code: Select all

<input type="hidden" name="antivirus" value="<?php echo $result2[30]; ?>" <?php if ($result2[30]) echo 'checked="checked"'; ?> />

Re: html form on load pre-check checkbox

Posted: Tue Oct 22, 2013 9:34 am
by sectionLeader123
got it thanks, appreciate the input