Page 1 of 1
[SOLVED] Radio button redirect
Posted: Wed Sep 07, 2005 8:11 pm
by vchris
Hey Guys!
I have a form and I want the user to be redirected as soon as they click the radio button without clicking a submit button.
This is a my javascript code to redirect the user:
Code: Select all
function modifycontact() {
window.href="contactlist_mod_admin.php?cid="document.contactlistform.modify.value;
}
This is my radio input:
Code: Select all
<input type="radio" name="modify" OnClick="modifycontact();" value="' . $row['contactid'] . '">
When I click on the radio button it simply does nothing. No redirect.
Posted: Wed Sep 07, 2005 8:23 pm
by Burrito
use the "location" method you should:
and missing the concat operator (+) for js you are.
Code: Select all
function modifycontact() {
location = "contactlist_mod_admin.php?cid="+document.contactlistform.modify.value;
}
Posted: Wed Sep 07, 2005 8:47 pm
by vchris
I was so close.
It works!!! thanks.
Query String error
Posted: Thu Sep 08, 2005 9:36 pm
by vchris
I just found out something about that redirect.
About the query string cid, the value of the radio button is the contact id of that contact and should something like 1, 2, 3... But in the address bar it shows: cid=undefined
Here is the code to redirect and display the query string:
Code: Select all
location = "contactlist_mod_admin.php?cid="+document.contactlistform.edit.value;
Posted: Thu Sep 08, 2005 10:44 pm
by Burrito
it's a radio button so you have to loop over the entire length of the element...example below:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script>
function modifycontact(){
for(i=0;i<document.MyForm.modify.length;i++)
{
if(document.MyForm.modify[i].checked)
{
var bob =document.MyForm.modify[i].value;
break;
}
}
location = "contactlist_mod_admin.php?cid="+bob;
}
</script>
</head>
<body>
<form name="MyForm">
<input type="radio" name="modify" value="1" onClick="modifycontact()"><br>
<input type="radio" name="modify" value="2" onClick="modifycontact()"><br>
<input type="radio" name="modify" value="3" onClick="modifycontact()"><br>
<input type="radio" name="modify" value="4" onClick="modifycontact()"><br>
</form>
</body>
</html>
Posted: Thu Sep 08, 2005 11:06 pm
by feyd
or, you could pass this.value to the JS function

Posted: Fri Sep 09, 2005 4:32 pm
by vchris
I have tried your code Burrito and it doesn't do anything when I click the radio button.
Here is my javascript:
Code: Select all
function modifycontact() {
for(var i=0 ; i<document.contactlistform.modify.length ; i++){
if(document.contactlistform.modify[i].checked){
var cid = document.contactlistform.modify[i].value;
break;
}
}
location = "contactlist_mod_admin.php?cid="+cid;
}
Posted: Fri Sep 09, 2005 4:39 pm
by Burrito
the snippet I posted worked fine.
In your case though, it'd probably be easier to do what Feyd suggested.
Code: Select all
<script>
function modifycontact(what)
{
location = 'contactlist_mod_admin.php?cid='+what;
}
</script>
<input type="radio" name="modify" value="1" onClick="modifycontact(this.value)">
<input type="radio" name="modify" value="2" onClick="modifycontact(this.value)">
Posted: Fri Sep 09, 2005 4:40 pm
by feyd
Code: Select all
function modifycontact(value)
{
document.location = "contactlist_mod_admin.php?cid=" + value;
}
-----------
<form name="MyForm">
<input type="radio" name="modify" value="1" onClick="modifycontact(this.value)"><br>
<input type="radio" name="modify" value="2" onClick="modifycontact(this.value)"><br>
<input type="radio" name="modify" value="3" onClick="modifycontact(this.value)"><br>
<input type="radio" name="modify" value="4" onClick="modifycontact(this.value)"><br>
</form>
much easier
:edit: nice snipe burr

Posted: Sat Sep 10, 2005 11:40 am
by vchris
Perfect!!!
it's simple and it works.
Thanks a lot for the help guys!
