...

View Full Version : Search and Radio buttons



mulder
06-22-2012, 09:08 PM
Hey all!

I have a search function as outlined below...


<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..


<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

WolfShade
06-22-2012, 10:17 PM
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.



<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>

mulder
06-24-2012, 05:55 PM
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..


<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


<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

hdewantara
06-25-2012, 02:10 PM
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:



<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>



<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>

mulder
06-25-2012, 08:12 PM
That worked a treat! Thanks so much! I appreciate the time and effort you afforded me!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum