JavaScript and client side scripting.
Moderator: General Moderators
vchris
Forum Contributor
Posts: 204 Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec
Post
by vchris » Wed Sep 07, 2005 8:11 pm
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.
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Wed Sep 07, 2005 8:23 pm
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 » Wed Sep 07, 2005 8:47 pm
I was so close.
It works!!! thanks.
vchris
Forum Contributor
Posts: 204 Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec
Post
by vchris » Thu Sep 08, 2005 9:36 pm
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;
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Thu Sep 08, 2005 10:44 pm
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>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Sep 08, 2005 11:06 pm
or, you could pass this.value to the JS function
vchris
Forum Contributor
Posts: 204 Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec
Post
by vchris » Fri Sep 09, 2005 4:32 pm
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;
}
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Fri Sep 09, 2005 4:39 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Sep 09, 2005 4:40 pm
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
vchris
Forum Contributor
Posts: 204 Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec
Post
by vchris » Sat Sep 10, 2005 11:40 am
Perfect!!!
it's simple and it works.
Thanks a lot for the help guys!