简体   繁体   English

jquery ajax从一个函数到另一个php获取变量

[英]jquery ajax getting variable from one function to another php

So basically to give a brief before exploring in to the code I give below.所以基本上是在探索我下面给出的代码之前做一个简短的介绍。 Basically what I'm trying to do is, I have a modal in one of my php page as below.基本上我想要做的是,我在我的 php 页面之一中有一个模态,如下所示。 I'm trying to show the model from a php function "fetch_assignment_history" given below.我正在尝试从下面给出的 php 函数“fetch_assignment_history”中显示模型。 There is a button in which we can add users to the assignment, and the button onclick event(onClick="assign(this.dataset.id)") call function passing the id from the php function to JS function.有一个按钮,我们可以将用户添加到分配中,按钮 onclick event(onClick="assign(this.dataset.id)") 调用函数将 id 从 php 函数传递给 JS 函数。

So when the user click the button the JS function shows the modal as well as get the assignment id in the function.因此,当用户单击按钮时,JS 函数会显示模态并在函数中获取分配 ID。 This assignment id is set (through JS) to data attribute of multiple select dropdown in the modal.此分配 id 设置(通过 JS)到模式中多选下拉列表的数据属性。

Now the point where I'm stuck is, a setinterval constantly fetch the assigned users.现在我遇到的问题是,一个 setinterval 不断地获取分配的用户。 This basically collect from mysql database of users who were added to the assignment (comma delimitted id's).这基本上是从添加到分配中的用户的 mysql 数据库中收集的(逗号分隔的 ID)。

When the modal is opened I want the previously assigned users to be selected in the multiple select (as per data in mysql) as default.当模式打开时,我希望在多重选择中选择先前分配的用户(根据 mysql 中的数据)作为默认值。 For this I want to get the id passed from the onclick event为此,我想获取从 onclick 事件传递的 id

My HTML part (modal)我的 HTML 部分(模态)

<div id="assignee" class="modal custom-modal fade" role="dialog">
                <div class="modal-dialog modal-dialog-centered" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title text-center">Assignment</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <div class="col-md-12 col-sm-12 col-xs-12 form-group has-feedback">
                                <label style="color:#333D79FF; font-size:15px;">Add Users <span class="text-danger">*</span></label>
                                <div>
                                    <select class="select2_multiple form-control" multiple="multiple" id="userslist" name="userslist[]" style="width: 100%;left: 10px;">
                                        <option id="none" value="">Select Users</option>
                                            <?php
                                                My PHP CODE TO PULL OUT DATA FROM MYSQL DB
                                                ?>
                                                <option value="<?php echo $id; ?>" data-useravatar="<?php echo $avatar; ?>" data-usertitle="<?php echo $firstname;echo "&nbsp;";echo $lastname; ?>" data-role="<?php echo $role; ?>" data-assignment=""><?php echo $firstname;echo "&nbsp;";echo $lastname; ?></option>

                                            <?php } ?>
                                    </select>
                                </div>
                              <div id="usersfetch"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

My PHP我的 PHP

1. function to fetch assignment history. 1. 获取分配历史的函数。
Below button onclick goes to JS and shows modal.下面的按钮 onclick 转到 JS 并显示模态。 and also pass id并且还传递id

function fetch_assignment_history($connect)
{
    $query = "
    SELECT * FROM assignment 
    WHERE id = '".$_SESSION['id']."' 
    ORDER BY id ASC
    ";

    $statement = $connect->prepare($query);

    $statement->execute();

    $result = $statement->fetchAll();

    $output = '<ul class="list-unstyled">';
    foreach($result as $row)
    {
        $user_name = '';
        $dynamic_background = '';
        $message = '';

                $assignmentname = $row["assignmentname"];
                $name = $row["name"];
                $id     = $row['id'];
        $output .= '

    <div class="x_panel" style=" font-size:14px;">
                    <span class="controlbuttons">
                        <button class="btn btn-round" data-id="'.$id.'" onClick="assign(this.dataset.id)"><i class="material-icons" title="Assign">person_add</i></button>
                    </span>
                </div>

        ';
    }
    $output .= '</ul>';
    return $output;
}
  1. function to assigned user history分配用户历史的功能

    function fetch_assigned_user_history($connect) { some php queries }

My JS我的JS
I want the assignid from function assign(assignid) to be called from function fetch_assigned_user_history().我希望从函数 fetch_assigned_user_history() 调用函数assign(assignid) 中的assignid。 How do I accomplish this??????我该如何实现这个??????

setInterval(function(){
    fetch_assigned_user_history();
}, 1000);

function assign(assignid)
    {
        var assignid    = assignid;
        $("#assignee").modal("show");
        $('#userslist').data("assignment",assignid);

    }
function fetch_assigned_user_history()
    {
        var action = "fetch_assigned_user_data";
        var getid = ;// I need to get the ID from the previous function from onclick event function assign(assignid) 

        $.ajax({
            url:"assignment.php",
            method:"POST",
            data:{action:action, assignmentid:getid},
            success:function(data)
            {
                $('#usersfetch').html(data);
            }
        })
    }

Hope I'm clear with my question.希望我清楚我的问题。 Appreciate for an advise to fix.感谢您提供修复建议。 To summarise, the basic question is how to call variable of one JS function in to another (in this example id's) so that i can get the list of users in that assignment?总而言之,基本问题是如何将一个 JS 函数的变量调用到另一个(在此示例中为 id),以便我可以获取该分配中的用户列表? Also how to get it selected by default the Multiple select with database values.还有如何在默认情况下选择带有数据库值的多选。

You already stored 'assignid' in data-* attribute, you can just retrieve it when you need it您已经在 data-* 属性中存储了 'assignid',您可以在需要时检索它

 function assign(assignid) { var assignid = assignid; $("#assignee").modal("show"); // You already store your assignid at here $('#userslist').data("assignment",assignid); } function fetch_assigned_user_history() { var action = "fetch_assigned_user_data"; // Now you just need to retrieve it var getid = $('#userslist').data("assignment"); $.ajax({ url:"assignment.php", method:"POST", data:{action:action, assignmentid:getid}, success:function(data) { $('#usersfetch').html(data); } }) }

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

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