Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Radio Buttons instead of Check Boxes on Form

    I am trying to adapt code that lets users select a search engine
    from radio buttons and use it to search internet.

    It works fine with check boxes, but I would like RADIO BUTTONS
    but cannot because they all need the same name to function
    in a set. I have no real knowledge of javascript and would really
    appreciate any help.

    Code Below

    Lee Collins
    Liverpool
    England

    Code:
    <script language="javascript">
    <!--
    
    function netsearch(formname) 
    
    {
      
       
       var e1
       var e2
       var f1
       var f2
       var g1
       var g2
       var plus
       var TEXT
       var noENGINE
       var haveTEXT
    
    
    
       
       TEXT=formname.TEXT.value;
    
       noEngine=true;
       haveTEXT=true;
       plus=""
    
       if (TEXT=="")
       {
       alert("Please type in some text!")
       haveTEXT=false
       }
          else
    	  {
    	  for (var i=0; i < TEXT.length; i++)
    	     {
             if (TEXT.charAt(i)==" ")
    	        {
                plus+="%20"
                }
    	     else
    	        {
                plus += TEXT.charAt(i)
    	        }
             }
          }
       
    TEXT=plus
    
    
        
        
       //LYCOS
       e1=formname.lycos.checked;
      
       e2="http://search.live.com/images/results.aspx?q=" + TEXT + "&go=&form=QBLH&scope=images";
    	 
    	 
    	 if (e1)
           {
           noEngine=false
           if (haveTEXT)
             {
             newWindow=window.open(e2, "l","toolbar,location,directories,status,menubar,scrollbars,resizable=1")
             }
           }  
    
       //WEBCRAWLER
       f1=formname.webcrawler.checked;
       f2="http://en.wikipedia.org/w/index.php?title=Special%3ASearch&ns0=1&search=" + TEXT; + "&fulltext=Advanced+search"
    	 
    	 
    	 
    	 if (f1)
           {
           noEngine=false
           if (haveTEXT)
             {
             newWindow=window.open(f2, "w","toolbar,location,directories,status,menubar,scrollbars,resizable=1")
             }
           }       
    
       //YAHOO
       g1=formname.yahoo.checked;
       g2="http://www.google.co.uk/search?pg=q&what=web&fmt=.&q=" + TEXT;
    	 
    	 if (g1)
           {
           noEngine=false
           if (haveTEXT)
             {
             newWindow=window.open(g2, "y","toolbar,location,directories,status,menubar,scrollbars,resizable=1")
             }
           }  
    
       //noENGINE
         if (noEngine)
           {
           alert("Please select a search engine!")
           }  
    
    // END Netsearch
     
    }
    
    function getPath(url) {
    
            lastSlash = url.lastIndexOf("/")
    
            return url.substring(0, lastSlash + 1)
    
    }
    
    // -->
    
    </script>
    
    
    <form name="engines" onSubmit="netsearch(engines)">
      <p><input type="text" size="25" maxlength="200" name="TEXT" value="Quick Search For..." onClick="form.TEXT.select()">
    
    <br />
    <input type="checkbox" name="yahoo" CHECKED>
    Google <br />
    <input type="checkbox" name="lycos">
    Live Images<br />
        <input type="checkbox" name="webcrawler"> 
        Wikipedia
          </p>
    </form>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Here you are - but you will have to adapt the rest of your code to suit:-


    Code:
    <form name="engines" onSubmit="netsearch(engines)">
    
    Lycos <input type = radio name = "rad1" id = "rad1" value = "Lycos" onclick = "getId()">
    Yahoo <input type = radio name = "rad1" id = "rad1" value = "Yahoo" onclick = "getId()">
    Webcrawler<input type = radio name = "rad1" id = "rad1" value = "Webcrawler" onclick = "getId()">
    
    </form>
    
    
    <script type = "text/javascript">
    
    var site = "";
    function getId() {
    for (var i = 0; i < document.engines.rad1.length; i++) {
    if (document.engines.rad1[i].checked) {
    site = document.engines.rad1[i].value;
    alert (site);  // delete after testing
    }
    }
    }
    
    </script>
    <script language="javascript"> is deprecated and obsolete, and indicative of old code. Use <script type = "text/javascript"> instead.


    "640K ought to be enough for anybody." — Bill Gates, 1981.

  • #3
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for trying to help here, but I have
    no idea how to adapt your code into my existing stuff.

    Your code just brings up boxes to state which engine they have selected.

    The idea is that I want people (kids in a school actually) to be able to
    select and search from different engines. The check boxes do work, but
    with the problem that they have to 'un-select' the original check tick
    when making a new choice, somthing that radio buttons would solve.

    I assume you tested the original code out?

    Imagine this (which does work) but only with radio buttons, that's what I'm after.

    Really appreciate any help here.

    Cheers

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Hmmm, that coding style seems familar. Reminds me of my friends Cr----rd, He--ey and an--ra! But it cannot really be by any of them them, as they have all been banned from the forum.
    Last edited by Philip M; 03-28-2009 at 05:33 PM.

  • #5
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Here's an example using radiobuttons, just add more searchengines to the form, ex:
    Code:
    <script type="text/javascript">
    
    function MySearch()
    {
    	if (document.MyForm.query.value.length == 0)
    	{
    		alert("Please type in some text!");
    		return;
    	}
    
    	for (var i=0; i < document.MyForm.engine.length; i++)
    	{
    		if (document.MyForm.engine[i].checked == true)
    		{
    			window.open(document.MyForm.engine[i].value + escape(document.MyForm.query.value));
    		}
    	}
    }
    
    </script>
    
    <form name="MyForm" action="" method="">
    <input type="radio" name="engine" value="http://www.google.co.uk/search?q=" checked="checked" /> Google<br />
    <input type="radio" name="engine" value="http://search.live.com/images/results.aspx?q=" /> MSN Images<br />
    <input type="radio" name="engine" value="http://en.wikipedia.org/w/index.php?title=Special%3ASearch&ns0=1&fulltext=Advanced+search&search=" />Wikipedia<br />
    <input type="text" name="query" />
    <input type="button" value="Search!" onclick="MySearch()">
    </form>

  • Users who have thanked Nicklas for this post:

    leecollins (03-28-2009)

  • #6
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for your efforts here guys.

    The second radio button is the closest to what I need, but
    I would like the user to be able to press 'enter' after typing and
    it use the default (checked) button, as to opposed to actually having to click the button.

    It is set focus or something?

    I have no idea about this stuff really.

    Thanks again for any help
    Last edited by leecollins; 03-28-2009 at 08:17 PM. Reason: one more issue.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Well, as one button is checked by default the user naturally does not have to select it again. He only needs to click on one of the radio if he wants to change from the default.

  • #8
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I'd like it to work the same way that google does.

    I.e you type into the text box and press enter to got the the search results.

    BUT if you want to you can choose 'UK pages only' if you'd like.

    It's for the school I work in basically - these kids are lazy y' know

  • #9
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Here's a slightly modified version of my previous example, ex:
    Code:
    <script type="text/javascript">
    
    function MySearch()
    {
    	if (document.MyForm.query.value.length == 0)
    	{
    		alert("Please type in some text!");
    	}
    	else 
    	{
    		for (var i=0; i < document.MyForm.engine.length; i++)
    		{
    			if (document.MyForm.engine[i].checked == true)
    			{
    				//document.MyForm.action = document.MyForm.engine[i].value + escape(document.MyForm.query.value);
    				window.open(document.MyForm.engine[i].value + escape(document.MyForm.query.value));
    
    			}
    		}
    	}
    
    return false;
    
    }
    
    </script>
    
    <form name="MyForm" action="" method="post" onsubmit="return MySearch()" >
    <input type="radio" name="engine" value="http://www.google.co.uk/search?q=" checked="checked" /> Google<br />
    <input type="radio" name="engine" value="http://search.live.com/images/results.aspx?q=" /> MSN Images<br />
    <input type="radio" name="engine" value="http://en.wikipedia.org/w/index.php?title=Special%3ASearch&ns0=1&fulltext=Advanced+search&search=" />Wikipedia<br />
    <input type="text" name="query" />
    <input type="submit" value="Search!" />
    </form>
    Last edited by Nicklas; 03-30-2009 at 01:24 AM.

  • Users who have thanked Nicklas for this post:

    leecollins (03-30-2009)

  • #10
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up

    At last, someone understands what I am after.

    This is perfect!

    Nicklas, you will be rewarded in Valhalla for this

    Thank you very much.


  •  

    Posting Permissions

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