Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post

    Small function not working, dont know why, it looks ok to me.

    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.

    PHP Code:
    <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

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    [QUOTE=Anthony2oo4]

    Code:
    fm = document.useradmin;
    choice = document.useradmin.choice.value;
    user = document.useradmin.username.value;
    Alway use the var keyword or your variables will be global.

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

    Code:
    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;

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    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.
    Last edited by Anthony2oo4; 08-29-2006 at 01:36 AM.

  • #4
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    Thanks but I got it too work with this:

    PHP Code:
    <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=0i<document.useradmin.choice.lengthi++)  {
    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> 

  • #5
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Anthony2oo4
    Thanks but I got it too work with this:

    Code:
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •