...

View Full Version : Small function not working, dont know why, it looks ok to me.



Anthony2oo4
08-28-2006, 11:14 PM
Hey, iv been working on this script, im not an expert with javascript i mainly code in php, but I cant get this script too work. I had it working at one point but the if statment didnt work, it always returned true for the first condition.

Any help would be great.


<script type="text/javascript">
<!--
/*<![CDATA[*/
function User() {
fm = document.useradmin;
choice = document.useradmin.choice.value;
user = document.useradmin.username.value;

if (choice == "edit") {
action="BHijkdfs767FGsdg.php?op=users&mode=edit&edit=profile&id="+user;
} else if (choice == "ban") {
action="BHijkdfs767FGsdg.php?op=users&mode=edit&edit=admin&id="+user;
} else if (choice == "posts") {
action="Forums/search/search_author="+user+".html";
} else if (choice == "pm") {
action="Private_Messages/mode=post/u="+user+".html";
} else if (choice == "profile") {
action="Your_Account/profile="+user+".html";
}
//document.write(action); I used these for debugging and commented out the last line but even that dosnt work, it just refreshs the page.
//document.write("<br>");
//document.write(choice);
fm.action=action;
}
/*]]>*/
// -->
</script>
<div align="center">
<form name="useradmin" method="POST" action="" onsubmit="User()">
<p style="margin-top: 0; margin-bottom: 0"><input type="text" name="username" size="20"></p>
<p style="margin-top: 0; margin-bottom: 0"><input type="radio" name="choice" value="edit" >Edit User</p>
<p style="margin-top: 0; margin-bottom: 0"><input type="radio" name="choice" value="ban">Ban User</p>
<p style="margin-top: 0; margin-bottom: 0"><input type="radio" name="choice" value="posts">View Posts</p>
<p style="margin-top: 0; margin-bottom: 0"><input type="radio" name="choice" value="pm">PM User</p>
<p style="margin-top: 0; margin-bottom: 0"><input type="radio" name="choice" value="profile">View Profile</p>
<p style="margin-top: 0; margin-bottom: 0"><input type="submit" value="Submit" name="B1"></td>
</form>
</div>

thanks for your time

Arty Effem
08-29-2006, 01:03 AM
[QUOTE]
fm = document.useradmin;
choice = document.useradmin.choice.value;
user = document.useradmin.username.value;
Alway use the var keyword or your variables will be global.



choice = document.useradmin.choice.value;
choice will be undefined. You cannot read directly the selected value in a set of radio buttons. You loop through the elements until you find the one checked and you read its value.



if (choice == "edit") {
action="BHijkdfs767FGsdg.php?op=users&mode=edit&edit=profile&id="+user;
}As a matter of good practice, variables should be pre-declared. Creating a variable conditionally can lead to errors, as would be the case here. If no buttons were checked, action would not exist, leading to a failure of the assignment: fm.action=action;

Anthony2oo4
08-29-2006, 01:30 AM
Thanks for the reply, can you show me how to loop through the radio buttons ?

also, if i add this:

var fm;
var choice;
var user;
var action;

under function will that be ok ?

Thanks for your time.

Anthony2oo4
08-29-2006, 01:44 AM
Thanks but I got it too work with this:


<script type="text/javascript">
<!--
/*<![CDATA[*/
function User() {
var fm
var choice
var user
var action

fm = document.useradmin;
choice = document.useradmin.choice.value;
user = document.useradmin.username.value;

for (var i=0; i<document.useradmin.choice.length; i++) {
if (document.useradmin.choice[i].checked) {
choice = document.useradmin.choice[i].value
}
}

if (choice == "edit") {
action="BHijkdfs767FGsdg.php?op=users&mode=edit&edit=profile&id="+user;
} else if (choice == "ban") {
action="BHijkdfs767FGsdg.php?op=users&mode=edit&edit=admin&id="+user;
} else if (choice == "posts") {
action="Forums/search/search_author="+user+".html";
} else if (choice == "pm") {
action="Private_Messages/mode=post/u="+user+".html";
} else if (choice == "profile") {
action="Your_Account/profile="+user+".html";
}
fm.action=action;
}
/*]]>*/
// -->
</script>
<div align="center">
<form name="useradmin" method="POST" action="" onsubmit="User()">
<p style="margin-top: 0; margin-bottom: 0"><input type="text" name="username" size="20"></p>
<p style="margin-top: 0; margin-bottom: 0"><input type="radio" name="choice" value="edit" >Edit User</p>
<p style="margin-top: 0; margin-bottom: 0"><input type="radio" name="choice" value="ban">Ban User</p>
<p style="margin-top: 0; margin-bottom: 0"><input type="radio" name="choice" value="posts">View Posts</p>
<p style="margin-top: 0; margin-bottom: 0"><input type="radio" name="choice" value="pm">PM User</p>
<p style="margin-top: 0; margin-bottom: 0"><input type="radio" name="choice" value="profile">View Profile</p>
<p style="margin-top: 0; margin-bottom: 0"><input type="submit" value="Submit" name="B1"></td>
</form>
</div>

Arty Effem
08-29-2006, 02:46 AM
Thanks but I got it too work with this:


for (var i=0; i<document.useradmin.choice.length; i++) {
if (document.useradmin.choice[i].checked) {
choice = document.useradmin.choice[i].value
}
}That will get the value if the user has made a selection, but what if they haven't?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum