SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Random choice?

  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random choice?

    I have a form with 9 radio buttons that each have a different output. I want to create a 10th radio button called "random." So when a person would select the random radio button and submitted the form, one of the 9 radio buttons would be selected as output of the form.

    How can I do this?

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1)Give the same name to radio buttons 1-9, and a different name to radio button 10.

    2)Put an onclick event handler in your 10th radio button that calls a function.

    3)In your function get an array of the radio buttons 1-9:

    var radios = document.getElementsByName("1-9Name");

    4) Create a random number between 0 and 8:

    var r = Math.floor(Math.random() * 9);

    5) Use the random number to check the radio button at that position in the array:

    radios[r].checked = true;

  3. #3
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did what you said but it doesn't work I'm just a beginner at javascript so maybe you could take a look at my script and say what's wrong ?

    Code:
    <?php
    
    
    
    function randomize(oRadio, idx)
    {
    	var radios = document.getElementsByName("1-8system");
    	var r = Math.floor(Math.random() * 9); radios[r].checked = true;	
    }
    
    if ( isset($_POST['submit']) )
    {
    if ( !isset($_POST['nick'])  ||  !isset($_POST['system']) )
    {
    exit('Error, no nick or website selected.');
    }
    else
    {
    $nick = $_POST['nick'];
    $system = $_POST['system'];
    if ($system == 'google')
    {
    header("Location: http://www.google.com". $_POST['nick'] . "time");
    exit;
    }
    elseif ($system == 'yahoo')
    {
    header("Location: http://www.yahoo.com" . $_POST['nick'] . "time");
    exit;
    }
    elseif ($system == 'cnn')
    {
    header("Location: http://www.cnn.com" . $_POST['nick'] . "time");
    exit;
    }
    elseif ($system == 'bbc')
    {
    header("Location: http://www.bbc.com" . $_POST['nick'] . "time");
    exit;
    }
    elseif ($system == 'aol')
    {
    header("Location: http://www.aol.com" . $_POST['nick'] . "time");
    exit;
    }
    elseif ($system == 'zone')
    {
    header("Location: http://www.zone.com" . $_POST['nick'] . "time");
    exit;
    }
    elseif ($system == 'mozilla')
    {
    header("Location: http://www.mozilla.com" . $_POST['nick'] . "time");
    exit;
    }
    elseif ($system == 'download')
    {
    header("Location: http://www.download.com" . $_POST['nick'] . "time");
    exit;
    }
    }
    }
    php?>
    
    <html><head></head><body>
    
    <form method="post" target="_blank" action="<?=$_SERVER['PHP_SELF'] ?>">
    <input type="text" name="nick" size="10"><br>
    <input type="radio" name="system" value="google">google<br>
    <input type="radio" name="system" value="yahoo">yahoo<br>
    <input type="radio" name="system" value="cnn">yahoo<br>
    <input type="radio" name="system" value="bbc">yahoo<br>
    <input type="radio" name="system" value="aol">yahoo<br>
    <input type="radio" name="system" value="zone">yahoo<br>
    <input type="radio" name="system" value="mozilla">yahoo<br>
    <input type="radio" name="system" value="download">yahoo<br>
    <input type="radio" name="pick" value="pick" onclick="randomize(this,0)">random<br>
    
    <input type="image" src="images/search.jpg" name="submit" value="go">
    
    
    
    </form>
    
    </body></html>

  4. #4
    SitePoint Addict rokc's Avatar
    Join Date
    Oct 2004
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,
    You need to write the javascript functions within the javascript tag just as u write the php functions or methods. Made some changes to the existing function. I hope its not protectd by the copyright act.
    HTML Code:
    <script type="text/javascript">
    function randomize(oradio,idx)
    {
    	var radios = document.getElementsByName("system");
    	var r = Math.floor(Math.random() * 8); 
    	radios[r].checked = true;	
    }
    </script>
    Also there were only 8 radio buttons in your form that needed to be randomized.So you can see Math.random()*8 is passed into the Math.floor function. If you change the number of radio buttons in your form thn you need to change the parameter also accordingly.

  5. #5
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it works


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
  •