html form on load pre-check checkbox

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
sectionLeader123
Forum Commoner
Posts: 31
Joined: Fri Oct 11, 2013 8:46 am

html form on load pre-check checkbox

Post 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"/>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: html form on load pre-check checkbox

Post 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"'; ?> />
sectionLeader123
Forum Commoner
Posts: 31
Joined: Fri Oct 11, 2013 8:46 am

Re: html form on load pre-check checkbox

Post by sectionLeader123 »

got it thanks, appreciate the input
Post Reply