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 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts

    jQuery Change Function

    When the user gets to the control panel it uses a gets the session data and assigns a variable $defaultCharID, which works by the way, with the dropdown I need the selection, if there is one changed, change the value of the variable from the option value. Below is a little bit of code from a very similiar type script and my intent is to make it more suitable for my page with the rest of the coding. As you can see it was written in javascript but I'm trying to modify it so I can have less code and I'm using jQuery on my page. I do have a start but not sure what I would need after this.

    PHP Code:
    print "<form method=POST name=changedefaultcharacter>\n";
       print 
    "<input type=hidden name=newdefaultcharacterid value=0>\n";
       print 
    "</form>\n";

       print 
    "<script type=\"text/javascript\" language=\"javascript\">\n";
       print 
    "function executeformchangedefaultcharacter(newdefaultcharacterid) {\n";
       print 
    "document.changedefaultcharacter.newdefaultcharacterid.value = newdefaultcharacterid;\n";
       print 
    "document.changedefaultcharacter.submit();\n";
       print 
    "}\n";
       print 
    "</script>\n";

    print 
    "<form method=POST name=changecharacter>\n";
             print 
    "<select name=newdefaultcharacterid class=dropdown>"

    Code:
    $('#newDefaultCharID').change(function() {
              var newDefaultCharID = $("select#newDefaultCharID").val();
    
    });
    
    <form action="#" id="changeCharacter">
    <select class="dropdown" name="newDefaultCharID" id="newDefaultCharID" title="Select Character">

  • #2
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    Any ideas?

  • #3
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Your question is very hard to understand. What exactly is it that you want to do? What you're currently doing is define a local variable when the dropdown is changed, which is a bit useless, as the variable will be gone as soon as the change handler has finished.

  • #4
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    Let me rephrase it as I have difficulty describing issues and questions sometimes.

    The top code in my original post was from a fellow programmer with a similar script. My intentions was to pretty much take his bit of code and translate it into a jQuery shortened code. The bottom section of code was my attempt at accomplishing this but I'm sure if I'm even close yet.

  • #5
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    The first snipped defines a function that would submit a form, but the function is never actually called in that snippet, so it's hard to say what you actually want.

    Why don't we just forget that first snippet (which is badly done, too), and you tell me in words, what you want?

    For example: "I have a form with a dropdown in it, and once the user selects something, I want the form to be automatically submitted" or whatever. Just plain english, not some dated, incomplete and hard to read snippet.

  • #6
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    Okay now if you notice I've posted all the necessary code that will help hopefully explain this better.

    PHP Code:
    <?php

    session_start
    (); // Access the existing session

    // Include the database page
    include ('inc/dbconfig.php');

    // Include the variables page
    include ('inc/variables.php');

    $userID $_SESSION['userID'];
    $isAdmin $_SESSION['isAdmin'];
    $defaultCharID $_SESSION['defaultCharID'];
    $defaultCharacterName $_SESSION['defaultCharacterName'];
    $firstName $_SESSION['firstName'];
    $lastName $_SESSION['lastName'];
    $defaultCharShortName $_SESSION['shortName'];

    ?>
    Code:
    <script>
    $(document).ready(function() {
        $('#newDefaultCharID').change(function() {
            var newDefaultCharID = $('#newDefaultCharID option:selected').val();
            $('#characterID').val(newDefaultCharID);
            $.ajax({
                type: "POST"
                
            });  
        });
    });
    </script>
    PHP Code:
    <div id="headerCharacter">   
            <?php
                
    if ($isAdmin == "Yes") {
                    
    $query "SELECT
                            characters.ID
                        FROM
                            characters"

                    
    $result mysqli_query ($dbc,$query); 
                    
    $totalNumCharacters mysqli_num_rows($result);
            
                    
    $query "SELECT
                            handlercharacters.ID
                        FROM
                            handlercharacters
                        INNER JOIN
                            handlers
                            ON handlers.ID = handlercharacters.handlerID"

                } else {    
                    
    $query "SELECT
                            handlercharacters.id
                        FROM
                            handlercharacters
                        INNER JOIN
                            handlers
                            ON handlers.ID = handlercharacters.handlerID
                        WHERE
                            handlers.ID = '"
    .$userID."'"
                }
                
    $result mysqli_query ($dbc,$query);
                
    $numAvailableCharacters mysqli_num_rows($result);
                if ((
    $numAvailableCharacters "1") || (($isAdmin == "Yes") && (isset($totalNumCharacters)) && ($totalNumCharacters "0"))) {
                
    ?>
                    <input type="hidden" name="characterID" id="characterID" value="0" />
                    <select class="dropdown" name="newDefaultCharID" id="newDefaultCharID" title="Select Character">                
                <?php
        
                    
    if ($defaultCharID "0") {
                        print 
    "<option value=".$defaultCharID.">".$defaultCharacterName;
                    } else {
                        print 
    "<option value=0>- Select -";
                    }
                    if (
    $isAdmin == "Yes") {
                        
    $query "SELECT
                                characters.ID,
                                characters.characterName
                            FROM
                                characters
                            WHERE
                                characters.ID <> '"
    .$defaultCharID."' AND
                                characters.statusID = '1'
                            ORDER BY
                                characters.characterName"

                    } else {
                        
    $query "SELECT
                                characters.ID,
                                characters.characterName
                            FROM
                                characters
                            INNER JOIN
                                handlercharacters
                                ON characters.ID = handlercharacters.characterID                        
                            INNER JOIN
                                handlers
                                ON handlers.ID = handlercharacters.handlerID
                            WHERE
                                handlers.ID = '"
    .$userID."' AND
                                handlercharacters.characterID <> '"
    .$defaultCharID."' AND
                                characters.statusID = '1'
                            ORDER BY
                                characters.characterName"

                    }
                    
    $result mysqli_query ($dbc,$query);
                    
    $numRows mysqli_num_rows ($result);
                    if (
    $numRows 0) {
                        if (
    $isAdmin == "Yes") {
                            print 
    "<optgroup label=\"** Active Characters **\">";
                        }
                        while ( 
    $row mysqli_fetch_array $resultMYSQL_ASSOC ) ) {
                            print 
    "<option value=\"".$row['ID']."\">".$row['characterName']."</option>\r";
                        }
                    }
                    if (
    $isAdmin == "Yes") {
                        
    $query "SELECT
                                characters.ID,
                                characters.characterName
                            FROM
                                characters
                            WHERE
                                characters.ID <> '"
    .$defaultCharID."' AND
                                characters.statusID = '2'
                            ORDER BY
                                characters.characterName"

                    } else {
                        
    $query "SELECT
                                characters.ID,
                                characters.characterName
                            FROM
                                characters
                            LEFT JOIN
                                handlercharacters
                                ON characters.ID = handlercharacters.characterID                        
                            LEFT JOIN
                                handlers
                                ON handlers.ID = handlercharacters.handlerID
                            WHERE
                                handlers.ID = '"
    .$userID."' AND
                                handlercharacters.characterID <> '"
    .$defaultCharID."' AND
                                characters.statusID = '2'
                            ORDER BY
                                characters.characterName"

                    }
                    
    $result mysqli_query ($dbc,$query); 
                    
    $numRows mysqli_num_rows($result);
                    if (
    $numRows "0") {
                        print 
    "<optgroup label=\"** Inactive Characters **\">";    
                        while ( 
    $row mysqli_fetch_array $resultMYSQL_ASSOC ) ) {
                            print 
    "<option value=\"".$row['ID']."\">".$row['characterName']."</option>\r";    
                        }
                    }
                    
    ?>
                    </select>
                <?php
                
    } else {
                    print 
    "<h1>".$defaultCharacterName."</h1>\n";
                }
                
    ?>
                </div>
    What I'm wanting to do is if you notice after the person logs in and is taken to this page that holds all three blocks of coding (control panel) It establishes some variables based off the login credentials the user gave.

    The purpose of the defaultCharID and defaultCharacterName is just to establish which is the main character the user is controlling AT THE TIME after login. Now with the dropdown I'm not sure how to handle this but I'm thinking after the selection it will create a new session(?) with the new characterName and its ID as the new default ID and character of currently controlling. If all of that makes sense.

  • #7
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Sounds fine. Why do you want to use AJAX for that, though? Can't you just normally submit the form, and let your PHP give you back a new page with the changed character data?

  • #8
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    Is just one dropdown. There is no submit button.

  • #9
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    There's no such thing as "just one dropdown". Every dropdown is part of a form. It doesn't need to have a submit button; you can do the submitting onchange with Javascript.

  • #10
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    Right. I know that jQuery comes with a change event however this is where I get stuck I'm not sure where to go to from here. If you notice in the second block of code I have a change event started but from that point I'm clueless.

  • #11
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    What is there to get stuck on? Just submit the thing!


    PHP Code:
    $('#newDefaultCharID').change(function() {
        $(
    this).parents('form').submit();
    }); 
    Of course, your form needs method="POST" (which I don't see in your code), if you want to access the submitted values using the $_POST array on the server side.

  • #12
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    I don't understand how I can submit it when there's no form tag or submit button and why would I need those. All it is one dropdown so all I need is to perform the tasks for the change event.

  • #13
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Why would there be no form tag? In both snippets in your first post, there are form tags. You don't need a submit button in order to submit, but there definitely has to be a real form, with an actual URL you're submitting to.

    As for "why would I need those": I thought that's pretty obvious; you want to submit a form, in order to get a new page from the server that reflects the choice the user made in the dropdown. So, you need a form. Sure, you can get the new information per AJAX and then change your site manually, so it reflects the dropdown choice, but why go through that hassle?


  •  

    Posting Permissions

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