Search code examples
javascriptphpajaxselectiononchange

Error: Uncaught ReferenceError: onChangeAssignedGroup is not defined at HTMLSelectElement.onchange


I've been having issues trying to call a php script via javascript when the user changes a value in the "Assigned To Group" selection. Which is ultimately supposed to change the option list of, a not yet created, "Assign to User" selection.

I keep getting an error saying that it doesn't recognize the function's name. When I tried running a simple value change and alert to verify it could get the values, it recognized it and it updated accordingly. When I added the Ajax command is when it started not recognizing it. I'm sure I'm probably just using incorrectly, somehow, sense I've never used ajax before. But from the samples I've seen, it seems fairly straight forward.

Error: Uncaught ReferenceError: onChangeAssignedGroup is not defined at HTMLSelectElement.onchange

<?php
include('classes/class.User.php');
include('classes/class.Role.php');
include('classes/class.User_Role.php');
include('constants.php');
session_start();
?>
<html>
    <head>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script>
            function onChangeAssignedGroup() {
                
              var new_assigned_to_role_id = document.getElementById("option_list_assigned_to_role_id").value;
              document.getElementById("assignedGroupId").innerHTML = "You selected: " + new_assigned_to_role_id;
              alert(new_assigned_to_role_id);
              $.ajax({
                  method: "POST",
                  url: "ticket_details.php",
                  data:{
                        ticket_id:$_POST['ticket_id'];
                        creator_user_id:$_POST['creator_user_id'];
                        creator_user_name:$_POST['creator_user_name'];
                        status:$_POST['status'];
                        priority:$_POST['priority'];
                        title:$_POST['title'];
                        assigned_to_role_id:new_assigned_to_role_id;
                        assigned_to_role_name:"";
                        assigned_to_user_id:$_POST['assigned_to_user_id'];
                        assigned_to_user_name:$_POST['assigned_to_user_name'];
                  },
                  success: function () {
                    alert('form was submitted');
                  }
              });
            }
        </script>
    </head>
    <body>
        <p><a href="ticket_overview.php">Return to Tickets</a></p>
        <?php
            
        
            if(isset($_POST['submit']) or isset($_POST['submit_new_comment']) or isset($_POST['submit_update_ticket'])){
                $ticket_id = $_POST['ticket_id'];
                $creator_user_id = $_POST['creator_user_id'];
                $creator_user_name = $_POST['creator_user_name'];
                $status = $_POST['status'];
                $priority = $_POST['priority'];
                $title = $_POST['title'];
                $assigned_to_role_id = $_POST['assigned_to_role_id'];
                $assigned_to_role_name = $_POST['assigned_to_role_name'];
                $assigned_to_user_id = $_POST['assigned_to_user_id'];
                $assigned_to_user_name = $_POST['assigned_to_user_name'];
                //connect to database
                include("database_connection.php");
                
                //handle update to ticket
                if(isset($_POST['submit_update_ticket'])){
                    $update = $mysqli->query("UPDATE `ticket` SET `status` = '$status', `priority` = '$priority', 
                                            `assigned_to_role_id` = '$assigned_to_role_id', `assigned_to_role_name` = '$assigned_to_role_name', 
                                            `assigned_to_user_id` = '$assigned_to_user_id', `assigned_to_user_name` = '$assigned_to_user_name' 
                                            WHERE `ticket`.`id` = $ticket_id");
                    if(!$update){
                        echo"<p>".$mysqli->error."</p>";
                    }
                }
                
                //handle new comment
                if(isset($_POST['submit_new_comment'])){
                    $new_comment = $_POST['new_comment'];
                    $current_id = $_SESSION['user']->getId();
                    $current_username = $_SESSION['user']->getUsername();
                    
                    //sanitize data
                    $new_comment = $mysqli->real_escape_string($new_comment);
                    
                    unset($_POST['submit_new_comment']);
                    //insert new comment into database.
                    $insert = $mysqli->query("INSERT INTO ticket_comment (ticket_id, user_id, user_name, text) VALUES ('$ticket_id', '$current_id', '$current_username', '$new_comment')");
                    if(!$insert){
                        echo"<p>".$mysqli->error."</p>";
                    }
                    
                }
                
                //include arrays for converting values returned
                include("value_maps.php");
                echo "<p id='assignedGroupId'></p>";
                echo "<p>role id:".$assigned_to_role_id."</p>";
                echo "<p>role name:".$assigned_to_role_name."</p>";
                if(in_array($assigned_to_role_id,$_SESSION['user']->getRoles())){
                    echo "<p>you have this role.</p>";
                } else {
                    echo "<p>you don't have this role.</p>";
                }
                print_r($_SESSION['user']->getRoles());
                echo"
                    <form action='' method='post'>
                        <input type='hidden' name='ticket_id' value='$ticket_id'>
                        <input type='hidden' name='creator_user_id' value='$creator_user_id'>
                        <input type='hidden' name='creator_user_name' value='$creator_user_name'>
                        <input type='hidden' name='title' value='$title'>
                        <input type='hidden' name='assigned_to_role_id' value='$assigned_to_role_id'>
                        <input type='hidden' name='assigned_to_role_name' value='$assigned_to_role_name'>
                        <input type='hidden' name='assigned_to_user_id' value='$assigned_to_user_id'>
                        <input type='hidden' name='assigned_to_user_name' value='$assigned_to_user_name'>
                    <table border='0' align='center' cellpadding='5'>
                        <tr>
                            <th>Ticket ID</th>
                            <th>Title</th>
                            <th>Status</th>
                            <th>Priority</th>
                            <th>Assigned To Group</th>
                            <th>Assigned To User</th>
                        </tr>
                        <tr>
                            <td>$ticket_id</td>
                            <td>$title</td>";
                if(in_array($assigned_to_role_id,$_SESSION['user']->getRoles()) or in_array(ROLE_ID_ADMIN,$_SESSION['user']->getRoles())){
                    
                    echo "<td><select name='status'>";
                    $status_index = 0;
                    foreach($status_array as $status_choice){
                        if($status == $status_index){
                            echo "<option value='". $status_index."' selected>". $status_choice ."</option>";
                        } else {
                            echo "<option value='". $status_index."'>". $status_choice ."</option>";
                        }
                        $status_index++;
                    }
                    echo "</select></th>";
                    echo "<td><select name='priority'>";
                    $priority_index = 0;
                    foreach($priority_array as $priority_choice){
                        if($priority == $priority_index){
                            echo "<option value='". $priority_index."' selected>". $priority_choice ."</option>";
                        } else {
                            echo "<option value='". $priority_index."'>". $priority_choice ."</option>";
                        }
                        $priority_index++;
                    }
                    echo "</select></th>";
                } else {
                    echo "<td>".$status_array[$status]."</th>";
                    echo "<td>".$priority_array[$priority]."</th>";
                }
                if(in_array(ROLE_ID_ADMIN,$_SESSION['user']->getRoles())){
                    echo "<td><select id='option_list_assigned_to_role_id' name='assigned_to_role' onchange='onChangeAssignedGroup()'>";
                    foreach($_SESSION['roles'] as $assigned_to_role_choice){
                        if($assigned_to_role_id == $assigned_to_role_choice->getId()){
                            echo "<option value='". $assigned_to_role_choice->getId()."' selected>". $assigned_to_role_choice->getName() ."</option>";
                        } else {
                            echo "<option value='". $assigned_to_role_choice->getId()."'>". $assigned_to_role_choice->getName() ."</option>";
                        }
                    }
                    echo "</select></td>";
                } else {
                    echo "<td>$assigned_to_role_name</td>";
                    echo "<td>$assigned_to_user_name</td>";
                }
                echo"
                        </tr>
                        <tr>
                            <td colspan='5'></td>
                            <td><input type='submit' name='submit_update_ticket' value='Update Ticket Details' required></td>
                        </tr>
                    </table>

                </form>
                ";
                
                //get back ticket details
                $results = $mysqli->query("SELECT `id`,`ticket_id`,`user_id`,`user_name`,`text`,`create_date`,`modify_date` FROM `ticket_comment` WHERE `ticket_id` = ".$ticket_id." ORDER BY `create_date`");
                
                //if insert was successful
                if($results){
                    
                    //header('location:registration_email_sent.php');
                    if ($results->num_rows > 0){
                        echo "
                            <table border='0' align='center' cellpadding='5'>
                                <tr>
                                    <th>Username</th>
                                    <th>Comment</th>
                                </tr>
                                ";
                        while($row = $results->fetch_row()){
                            echo"
                                <tr>
                                    <td>".$row[3].": </td>
                                    <td>".$row[4]."</td>
                                </tr>";
                        }
                    } else {
                        echo "<p>No comments found. </p>";
                    }
                }
                
                $mysqli->close();
                
                echo"
                    <form method='post' action=''>
                        <input type='hidden' name='ticket_id' value='$ticket_id'>
                        <input type='hidden' name='creator_user_id' value='$creator_user_id'>
                        <input type='hidden' name='creator_user_name' value='$creator_user_name'>
                        <input type='hidden' name='status' value='$status'>
                        <input type='hidden' name='priority' value='$priority'>
                        <input type='hidden' name='title' value='$title'>
                        <input type='hidden' name='assigned_to_role_id' value='$assigned_to_role_id'>
                        <input type='hidden' name='assigned_to_role_name' value='$assigned_to_role_name'>
                        <input type='hidden' name='assigned_to_user_id' value='$assigned_to_user_id'>
                        <input type='hidden' name='assigned_to_user_name' value='$assigned_to_user_name'>
                        
                    
                        <table border='0' align='center' cellpadding='5'>
                            <tr>
                                <th>New Comment</th>
                                <th><input type='submit' name='submit_new_comment' value='Post New Comment'></th>
                            </tr>
                            <tr>
                                <td colspan='2'><textarea name='new_comment' rows='10' cols='30' placeholder='New Comment Here' required></textarea></td>
                            </tr>
                        </table>
                    </form>
                ";
            } 
        ?>
        
    </body>
</html>

Solution

  • In the data object in your ajax request, you need to replace the semicolons with commas.

    You should also always use prepared statements to interact with the database.