简体   繁体   English

将PHP函数中的值分配给jquery

[英]Assign value from a PHP function to jquery

I have 3 pages as follow: Tasks.php (shows the data) getting data from tasks-functions.php (runs the code) and (tasks.js) that runs scripts. 我有3页,如下所示:Tasks.php(显示数据)从task-functions.php(运行代码)和运行脚本的(tasks.js)获取数据。 In tasks.js - I have a click function as follows: 在tasks.js中,我具有如下单击功能:

  $(document).on("click", "#mybtn", function () {
    var val1 = $('#taskid').val();
    alert($(val1).val());
    $('#EditTaskModal').modal('show');
  $(".table table-striped table-advance table-hover #test").html(val1);
    });

(#taskid) is in the tasks-functions.php which is an echo that shows data into a table by id on tasks.php/ (#taskid)在tasks-functions.php中,它是一个回显,通过在tasks.php /上的id将数据显示到表中

The tasks.js cannot get the values to assign to val1. task.js无法获取要分配给val1的值。

Tasks-Functions: 任务-功能:

function loadUserTasks(){
    $myTasksUser = $_SESSION['username'];
    $myTasks = mysql_query("SELECT * FROM tasks WHERE taskowner = '$myTasksUser' && taskstatus = 'Active'");
        while ($rows = mysql_fetch_array($myTasks)){
            $this->taskID = $rows['taskid'];
            $this->taskTitle = $rows['tasktitle'];
                $this->taskDetail = $rows['taskdetail'];
                $this->taskType = $rows['tasktype'];
                $this->taskResource = $rows['taskresource'];
                $this->taskStatus = $rows['taskstatus'];

                //Change icon color and value for task status
                if ($this->taskStatus == 'completed') {
                    $this->taskStatus = '<span class="label label-info label-mini">Completed</span>';
                }
                else{
                    $this->taskStatus = '<span class="label label-warning label-mini">Open</span>';
                }

                echo "  
                                      <tr>                          
                                  <td><a href=\"#\" id=\"taskid\" data-toggle=\"modal\">$this->taskTitle </a></td>
                                  <td>$this->taskDetail</td>
                                  <td>$this->taskResource</td>
                                  <td>$this->taskStatus</td>
                                  <td><button id=\"mybtn\">Edit</button></td>
                                  <td>
                                      <div>
                                        <form id=\"completetaskform\"  method=\"post\" >
                                            <input type=\"hidden\" name=\"taskid\" value=\"$this->taskID\" />
                                             <input type=\"hidden\" name=\"acct\" value=\"$this->taskResource\" />
                                             <div class=\"pull-right chat-features\">
                                          <button class=\"btn btn-success btn-xs\" id=\"completeTask\"><i class=\"icon-ok\"></i></button>
                                         </form> 
                                        </div>           
                                      <button class=\"btn btn-primary btn-xs\"><i class=\"icon-pencil\"></i></button>
                                      <button class=\"btn btn-danger btn-xs\"><i class=\"icon-trash \"></i></button>
                                  </td>
                              </tr>";

                          } 
   }

tasks.php: task.php:

                  <table class="table table-striped table-advance table-hover" id="tasksTable">
                      <thead>
                      <tr>
                          <th><i class="icon-bullhorn"></i> Title</th>
                          <th class="hidden-phone"><i class="icon-question-sign"></i> Descrition</th>
                          <th><i class="icon-bookmark"></i> Related To</th>
                          <th><i class=" icon-edit"></i> Status</th>
                          <th><i class=" icon-edit"></i> Action</th>
                      </tr>
                      </thead>
                      <?php $var = new task; $var->loadUserTasks(); ?>
                      <tbody>
                      <!-- get data from tasks_functions.php -->
                      </tbody>
                  </table>

I hope i am explaning enough, i am not sure if this is possible since there are 3 pages involved. 我希望我的解释足够多,我不确定这是否可能,因为涉及3页。

If you want to pass that variable along to your Javascript, you can do the following: 如果要将该变量传递给Javascript,可以执行以下操作:

<table class="table table-striped table-advance table-hover" id="tasksTable">
    <thead>
    <tr>
        <th><i class="icon-bullhorn"></i> Title</th>
        <th class="hidden-phone"><i class="icon-question-sign"></i> Descrition</th>
        <th><i class="icon-bookmark"></i> Related To</th>
        <th><i class=" icon-edit"></i> Status</th>
        <th><i class=" icon-edit"></i> Action</th>
    </tr>
    </thead>
    <?php $var = new task; $var->loadUserTasks(); ?>
    <tbody>
    <!-- get data from tasks_functions.php -->
    </tbody>
</table>
<script>
  // Declare a global variable to be used by your JavaScript
  var taskId = <?php echo $var->taskID; ?>;
 </script>

Then, in your JavaScript, you can just do this: 然后,在您的JavaScript中,您可以执行以下操作:

$(document).on("click", "#mybtn", function () {
    // Use your global taskId variable as an #id query
    var val1 = $('#' + taskId).val();
    alert($(val1).val());
    $('#EditTaskModal').modal('show');
    $(".table table-striped table-advance table-hover #test").html(val1);
});

Rather than passing variables from your PHP into your javascript, you can just query you table using jQuery and getting the appropriate value: 除了将变量从PHP传递到javascript中之外,您还可以使用jQuery查询表并获取适当的值:

$(document).on("click", "#mybtn", function () { 
    // Query the DOM for our table
    var $table = $(".table.table-striped.table-advance.table-hover");
    // Find the first input:hidden in the DOM (it would be better to add a class
    // or an ID that input and query it using that
    var val1 = $table.find('form input:hidden').first().val();
    alert($(val1).val());
    $('#EditTaskModal').modal('show');
    // Find something with the test id in our table
    $table.find("#test").html(val1); // This #test doesn't seem to exist
});

As you have a while loop and is printing more than one anchor, you have to add a class to your anchors and bind the click event on this class. 当您有一个while循环并要打印多个锚点时,您必须向锚点添加一个类,并将click事件绑定到该类上。 The event handler can access the element by 'this'. 事件处理程序可以通过“ this”访问元素。

I'm gonna add a class called '.taskAnchor'. 我要添加一个名为“ .taskAnchor”的类。 Your code could be like this: 您的代码可能像这样:

$(document).on("click", ".taskAnchor", function () {
    var val1 = $(this).text();
    //... your handler code here
});

Your php echo have to print the anchor with the class: 您的php echo必须输出带有该类的锚点:

<td><a href=\"#\" class=\"taskAnchor\" data-toggle=\"modal\">$this->taskTitle </a></td>

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

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