#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12

    Javascript of PHP?


    I decided to post this in javascript because I thought that would be the best way to handle this situation. The link to the situation is below, the description of what I attempting is as follows:

    I have a true/false question with a radio button for true, and a radio button for false. Before going to the php page when clicking the submit button, I want to make sure both radio boxes arent checked. I also want to make sure that they checked at least one of them (true or false). Could someone give me a sample code of what I should do? Here is the link:

    http://www.cnyhealthyair.org/quiz.html

    Thanks in advance guys.
    -andy
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    I am going to have to do all the radio buttons and the submit button in javascript I just realized to obtain my goal. Am I correct? Also, once pressed, they can't "unpress" it, if you know what I meen. Could I achieve this easier through javascript?
    -andy
    Last edited by andy3109; February 6th, 2003 at 08:55 PM.
  4. #3
  5. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Andy...calm down. Radio buttons are always used in groups of at least two; you give each group the same name in HTML, which is what causes them to behave as radio buttons. Here:

    [HTML]
    Code:
    <form action = "quiz.php" method="post" onsubmit="return checkRadios(this)">
            <p>&nbsp;</p>
            <p>1. Radon comes from microwave ovens.</p>
            <p> 
              <input type="radio" name="Q1" value="true">
              True 
              <input type="radio" name="Q1" value="false">
              False 
            <p>2. Radon can cause headaches and sinus problems.
            <p> 
              <input type="radio" name="Q2" value="true">
              True 
              <input type="radio" name="Q2" value="false">
              False
            <p>3. Radon is the second leading cause of lung cancer.
            <p> 
              <input type="radio" name="Q3" value="true">
              True 
              <input type="radio" name="Q3" value="false">
              False
            <p>4. New homes, as well as older homes, can have radon problems.
           <p> 
              <input type="radio" name="Q4" value="true">
              True 
              <input type="radio" name="Q4" value="false">
              False
            <p>5. Opening windows is always a reliable way to reduce radon levels.
            <p> 
              <input type="radio" name="Q5" value="true">
              True 
              <input type="radio" name="Q5" value="false">
              False
            <p>6. Radon can be a problem in homes that are built without basements.
            <p> 
              <input type="radio" name="Q6" value="true">
              True 
              <input type="radio" name="Q6" value="false">
              False
            <p>
              <center><input type="submit" name="Submit" value="Submit"></center>
            
          </form>
    [JavaScript]
    Code:
    		<link rel = stylesheet href = "main.css">
    		<script type="text/javascript" language="javascript">
    
    function checkRadios(oForm) {
         var el, radgrp, i = 0;
         while (el = oForm.elements[i++]) if (el.type == 'radio') {
                i++;
                radgrp = oForm.elements[el.name];
                if (!radgrp[0].checked && !radgrp[1].checked) {
                    alert('Please answer true or false for Question ' + el.name.split('Q')[1] + '.');
                    el.focus();
                    return false;
          }
       }
         return true;
    }
    
    
    </script>
    
    	</HEAD>
    Edit to suit. Pardon me while I go check for radon.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    That wouldn't work though right? The variables need to be seperated, I need to call the radio value by its name in PHP. If I make two of them the same name, it would get confused. Am I not seeing this correctly? Also, I recently compiled this code..this would eliminate the two ever conflicting:

    <html>
    <HEAD>


    <SCRIPT LANGUAGE="JavaScript">

    function disabler1() {

    if(document.form.a[0].checked) {
    document.form.a[1].checked=false
    }

    if(document.form.a[1].checked) {
    document.form.a[0].checked=false
    }

    }

    function disabler2() {

    if(document.form.b[0].checked) {
    document.form.b[3].checked=false

    }

    if(document.form.b[1].checked) {
    document.form.b[0].checked=false

    }

    }


    </script>

    </HEAD>
    <BODY>
    <form method = "post" action = "quiz.php">

    Question #1<br>

    <input type="radio" name="a" onClick="disabler1()">True<onClick="document.form.a[0].click();return false"></a><br>

    <input type="radio" name="a" onClick="disabler1()"><a href="http://www.javascriptsource.com"

    onClick="document.form.a[1].click();return false">False</a><br>

    Question #2<br>

    <input type="radio" name="b" onClick="disabler2()">True<onClick="document.form.b[0].click();return false"></a><br>

    <input type="radio" name="b" onClick="disabler2()">False<onClick="document.form.b[1].click();return false"></a><br>

    </form>
    </html>

    -andy


    P.S --you are funny
    Last edited by andy3109; February 6th, 2003 at 09:44 PM.
  8. #5
  9. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Change the form's method to "get" and submit it. Look in the address bar - that's what gets submitted (with POST as well). See what you're testing for?

    ps Don't name the submit button, unless you think someone might change it....
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Few questions:

    1. It works, but you say I should use get instead of post..why?

    2. I forgot how to list arrays in the if statement, I am trying to say something like this:

    if(document.form.b[0, 1].checked=false {
    alert ('please Select One')
    }

    But it isn't stopping if array 0 and 1 arent checked. Here is my newest code:

    <html>
    <HEAD>


    <SCRIPT LANGUAGE="JavaScript">

    function disabler1() {

    if(document.form.a[0].checked) {
    document.form.a[1].checked=false
    }

    if(document.form.a[1].checked) {
    document.form.a[0].checked=false
    }

    }

    function disabler2() {

    if(document.form.b[0].checked) {
    document.form.b[3].checked=false

    }

    if(document.form.b[1].checked) {
    document.form.b[0].checked=false

    }
    if(document.form.b[0, 1].checked=false {
    alert ('please fill in one')
    }

    }


    </script>




    </HEAD>



    <BODY>



    <form action = "quiz.php" method = "post">

    Category A<br>

    <input type="radio" name="a"

    onClick="disabler1()">True<onClick="document.form.a[0].click();return false"></a><br>

    <input type="radio" name="a"

    onClick="disabler1()">False<onClick="document.form.a[1].click();return false"></a><br>



    Category B<br>

    <input type="radio" name="b"

    onClick="disabler2()">True<onClick="document.form.b[0].click();return false"></a><br>

    <input type="radio" name="b"

    onClick="disabler2()">False<onClick="document.form.b[1].click();return false"></a><br>

    <input type="submit" value="Submit">


    </form>

    </html>
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    By the way, thanks for all your help so far.
  14. #8
  15. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Andy...just a heads-up: nobody scripts radio buttons - they're already programmed with the correct behavior.

    Use POST by all means - I just wanted you to see the submitted name/value pairs so you'd know what to check for in your PHP. Change the values to numbers if you like, I just used the obvious. Notice that only one value - the value of the checked button - is submitted? That's about all you need to know about radios...

    http://www.estek.net/estek/idocs/for...YPE_RADIO.html
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    But how should I make it so the user MUST press eather true of false. If they don't an alert button pops up saying "You must select true or false."
    -andy
  18. #10
  19. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    1) Paste in exactly what I posted above.
    2) I left a bit of your code in so you could see where the new code goes.
    3) Change method to GET - just for now - and un-name that submit button.
    4) Take quiz. Leave questions unanswered. Keep submitting. When it runs, look at address bar. Comment here.
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Ok, it works..Thanks alot man. I just have to figure out how to call them in PhP now that two radio buttons have the same name.
    -andy
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Got it! Thanks man:

    http://www.cnyhealthyair.org/quiz.html

    CHECK YOUR RADON!

IMN logo majestic logo threadwatch logo seochat tools logo