简体   繁体   English

错误:未捕获的 ReferenceError:onChangeAssignedGroup 未在 HTMLSelectElement.onchange 中定义

[英]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.当用户更改“分配给组”选项中的值时,我在尝试通过 javascript 调用 php 脚本时遇到问题。 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.当我添加 Ajax 命令时,它开始无法识别它。 I'm sure I'm probably just using incorrectly, somehow, sense I've never used ajax before.我确定我可能只是使用不正确,不知何故,感觉我以前从未使用过 ajax。 But from the samples I've seen, it seems fairly straight forward.但从我看到的样本来看,这似乎相当简单。

Error: Uncaught ReferenceError: onChangeAssignedGroup is not defined at HTMLSelectElement.onchange错误:未捕获的 ReferenceError:onChangeAssignedGroup 未在 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>

In the data object in your ajax request, you need to replace the semicolons with commas.在您的 ajax 请求中的数据 object 中,您需要将分号替换为逗号。

You should also always use prepared statements to interact with the database.您还应该始终使用准备好的语句与数据库进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM