[SOLVED] Radio button redirect

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

[SOLVED] Radio button redirect

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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; 
}
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

I was so close.

It works!!! thanks.
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Query String error

Post 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;
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

or, you could pass this.value to the JS function :P
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post 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;
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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)">
Last edited by Burrito on Fri Sep 09, 2005 4:44 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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 :lol:

:edit: nice snipe burr ;)
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

Perfect!!!

it's simple and it works.

Thanks a lot for the help guys! :D
Post Reply