SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Radio button value not being read when checking if selected

    I have a form checker script working on all my drop downs and all working fine, as they all need to have been used, but when it comes to the radio buttons, they dont work for some reason, the form checker doesnt see the value 0 as below and allows the form to submit.

    Code:
    else if (document.form2.potentialoutcome.value == "0")
    {
    	alert("Potential level of injury - Please complete all required fields");
    	window.location="detail.php?ID=<?=$report_ID ?>#potentialoutcome";
        return false;
    }
    else if (document.form2.likelihood.value == "0")
    {
    	alert("Likelihood of injury - Please complete all required fields");
    	window.location="detail.php?ID=<?=$report_ID ?>#likelihood";
        return false;
    }
    
    <div style="position:relative; width:950px; height:200px; margin-top:30px;">
    <a name="potentialoutcome" id="potentialoutcome"></a>
    <div style="position:relative; float:left; width:210px; height:200px; line-height:30px;">
    <span style="color:#900">*</span> Potential level of injury?</div>
    <div style="position:relative; float:right; width:735px; height:170px; line-height:30px; padding-top:0px;">
    <input name="potentialoutcome" type="radio" value="0" checked="checked"> Select Option<br/>
    <input name="potentialoutcome" type="radio" value="Minor"> Minor<br/>
    <input name="potentialoutcome" type="radio" value="First aid"> First aid<br/>
    <input name="potentialoutcome" type="radio" value="Lost time (incl >3 days)"> Lost time (incl >3 days)<br/>
    <input name="potentialoutcome" type="radio" value="Major injury"> Major injury<br/>
    <input name="potentialoutcome" type="radio" value="Fatality"> Fatality</div>
    </div>
    
    <div style="position:relative; width:950px; height:200px; top:10px;">
    <a name="likelihood" id="likelihood"></a>
    <div style="position:relative; float:left; width:210px; height:200px; line-height:30px;">
    <span style="color:#900">*</span> Likelihood of injury?</div>
    <div style="position:relative; float:right; width:735px; height:170px; line-height:30px; padding-top:0px;">
    <input type="radio" name="likelihood" value="0" checked> Select Option<br/>
    <input type="radio" name="likelihood" value="Very unlikely"> Very unlikely<br/>
    <input type="radio" name="likelihood" value="Unlikely"> Unlikely<br/>
    <input type="radio" name="likelihood" value="Possible"> Possible<br/>
    <input type="radio" name="likelihood" value="Likely"> Likely<br/>
    <input type="radio" name="likelihood" value="Very Likely"> Very Likely</div>
    </div>
    http://www.whhazardreport.co.uk/mana...p?ID=66#hazard

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,437
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Radio buttons work differently than other inputs, so rather than checking the value like this:
    Code JavaScript:
    if (document.form2.potentialoutcome.value == "0")
    you want to be checking if the first radio button is checked, like this:
    Code JavaScript:
    if (document.form2.potentialoutcome[0].checked)

  3. #3
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah I see, thank you fretburner, works perfectly.

    Can carry on now, thank you.


Bookmarks

Posting Permissions

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