微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

用户更改“分配给组”选项中的值时,我在尝试通过 javascript 调用 PHP 脚本时遇到问题。最终应该更改尚未创建的“分配给用户”选项的选项列表。

我不断收到错误消息,说它无法识别函数名称。当我尝试运行一个简单的值更改和警报以验证它可以获取值时,它识别它并相应地更新。当我添加 Ajax 命令时,它开始无法识别它。我确定我可能只是使用不正确,不知何故,感觉我以前从未使用过 ajax。但从我看到的样本来看,它似乎相当简单。

错误:未捕获的引用错误:HTMLSelectElement.onchange 中未定义 onChangeAssignedGroup

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

解决方法

在你的ajax请求中的数据对象中,你需要用逗号替换分号。

您还应该始终使用准备好的语句与数据库进行交互。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。