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 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Search and Radio buttons

    Hey all!

    I have a search function as outlined below...

    Code:
    <script>
    	function subMit(){
    val=document.getElementById("num").value
    location.href="/search/result.php?id="+val; title="Search"; 
    }
    </script>
    <form name="form1" action="" method="post">
    <input type="text" id="num" value="">
    
    <input type="button" value="Search" onclick="subMit()"/>
    </form>
    My problem is I would like to add 2 radio buttons to the search, the only code I can find is the one set out below..

    Code:
    <script type = "text/javascript">
    
    function setAction() {
    
    var actionVal;
    if (document.myForm.rad1[0].checked) {
    actionVal = "/search/result.php?id=";
    }
    else if (document.myForm.rad1[1].checked) {
    actionVal = "otherpartofsite.com/search.php";
    }
    document.myForm.action = actionVal; 
    
    }
    </script>
    
    <form name = "myForm" method="post" >
    <input type="radio" name="rad1" value="forum" checked> Forum
    <input type="radio" name="rad1" value="blog"> Blog
    <input name="search" size="20" maxlength="25" type="text">
    <input name="Submit" type="submit" value="Search!" onclick = "setAction()">
    </form>
    I have tried to "merge" these two scripts together but to no avail - and as both buttons have an "onclick" event I cannot keep them separate..

    Is there a way to make this into 1 function with 1 search box?

    thanks in advance

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    When merging two scripts like these, you don't want two buttons; you can't just mash them together and blend them. Ya gotta pick out what you need and use it.

    I don't know what you need/want, but I can supply some sample (based on yours) code that should help you understand.

    Code:
    <script>
         function subMit(){
     val=document.getElementById("num").value;
     if(document.getElementById("rad1")[0].checked) { val2 = document.getElementById("rad1")[0].value;}
    else {val2 = document.getElementById("rad1")[1].value; }
     location.href="/search/result.php?id="+val+"&rad="+val2; title="Search"; 
     } 
    </script> 
    <form name="form1" action="" method="post"> 
    <input type="text" id="num" value=""> 
    <input type="radio" name="rad1" value="forum" checked> Forum
     <input type="radio" name="rad1" value="blog"> Blog 
    <input type="button" value="Search" onclick="subMit()"/> 
    </form>
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • Users who have thanked WolfShade for this post:

    mulder (06-25-2012)

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks for your reply and help so far..

    What I would like is a search form with 2 radio button options. -

    When radio 1 is checked and the user presses "submit" the script performs this action..

    Code:
    <script>
    	function subMit(){
    val=document.getElementById("num").value
    location.href="/search/result.php?id="+val; title="Search"; 
    }
    </script>
    <input type="text" name="num" style="font-size: 7pt;" size="13" value=""  />
    When radio 2 is checked and the user presses "submit" the script performs this action

    Code:
    <form name="searchb" method="get" action="/photos/search.php">
    <input type="text" name="num" style="font-size: 7pt;" size="13" value=""  />
    Also, I don't know if I was suppose to but I Copied and Pasted the example you gave and nothing happened lol

  • #4
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    348
    Thanks
    9
    Thanked 51 Times in 50 Posts
    I think you both are close.

    But your location.href is rather unnusual though, are you sure it is the right syntax? Anyway, try this one:

    PHP Code:
    <script>
        function 
    setAction(){
            if(
    document.myForm.rad1[0].checked){
                var 
    val document.myForm.search.value;
                
    location.href '/search/result.php?id=' val ';title=Search';
                return 
    false;
            }
            else{
                return 
    true;
            }
        } 
    </script> 
    PHP Code:
    <form name="myForm" action="/photos/search.php" method="post" onsubmit="return setAction()">
        <
    input type="radio" name="rad1" value="forum" checked>Forum
        
    <input type="radio" name="rad1" value="blog">Blog
        
    <input name="search" size="20" maxlength="25" type="text">
        <
    input name="Submit" type="submit" value="Search!">
    </
    form

  • Users who have thanked hdewantara for this post:

    mulder (06-25-2012)

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    12
    Thanked 0 Times in 0 Posts
    That worked a treat! Thanks so much! I appreciate the time and effort you afforded me!!


  •  

    Tags for this Thread

    Posting Permissions

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