首页 > 解决方案 > javascripts是否可以有带有ID的hrefs?

问题描述

是否可以在 runConfirm 函数中添加一个包含 id 的 href?我的目标是在我单击接受后出现一个弹出窗口,并且接受按钮从 isset 进行查询。

这是我接受的代码。

$displaybody .= "<tr>
<td>" . $rows['lname'] . ", " . $rows['fname'] . "</td>
 <td>" . $rows['subject'] . "</td>
 <td>" . $rows['days'] . " " . $rows['rstime'] . " - " . $rows['retime'] . " 
</td>
   <td>" . $rows['note'] . "</td>
  <td>
<a class='runConfirm' data-id='" . $rows['rid'] . "'>Accept</a>                
<a href='home.php?decline=" . $rows['rid'] . "'>Decline</a>
</td>
</tr>"

这是我的弹出功能。(更新)

<script type='text/javascript'> 
        $(function(){
        $('.runConfirm').click(function(event){
          // get the id you want to act on
          var theId = $(this).data('rid');

          // ask user for confirmation
          alertify.confirm('Are you sure to accept this request?',

            // accepted
            function(){

              // go to the processing page
              window.location('/accept.php?rid=' + theId);
            },

            // declined
            function(){
              alertify.error('You clicked CANCEL');
          }).set('closable', false);

        });
        });
  </script>

Accept.php 包含接受预订的过程。

    <?php
    include("server.php");
    session_start();

    if(isset($_GET['accept']))
    {
      $rid=$_GET['accept'];
            $query=$mysqli->query("SELECT qid FROM requests WHERE rid='".$rid."'");
                if($query->num_rows>0)
                {
                        while($rows=$query->fetch_assoc())
                        {
                            $qid=$rows['qid'];
                        }
            }

     $query1=$mysqli->query("UPDATE qualified SET status='accepted' WHERE 
   qid='".$qid."' ");
     $query=$mysqli->query("UPDATE requests SET action='accepted' WHERE 
   rid='".$rid."'");

     header("Location:home.php");
   }

   ?>

标签: javascriptphp

解决方案


生成 DOM 时,您可以将数据属性添加到“确认”链接,然后您可以从runConfirm点击回调函数访问该链接。

假设您要使用的 id$rows['rid']将给出:( 为清楚起见仅显示更改的部分)

dom 生成

$displaybody .= "<tr>
  <td>" . $rows['lname'] . ", " . $rows['fname'] . "</td>
  <td>" . $rows['subject'] . "</td>
  <td>" . $rows['days'] . " " . $rows['rstime'] . " - " . $rows['retime'] . "</td>
  <td>" . $rows['note'] . "</td>
  <td>
    <a class='runConfirm' data-id='" . $rows['rid'] . "'>Accept</a>                
    <a href='home.php?decline=" . $rows['rid'] . "'>Decline</a>
  </td>
</tr>"

弹出窗口处理(更新)

$('.runConfirm').click(function(event){
  // get the id you want to act on
  var theId = $(this).data('id');

  // ask user for confirmation
  alertify.confirm('Are you sure to accept this request?',

    // accepted
    function(){

      // go to the processing page
      window.location = '/accept.php?rid=' + theId;
    },

    // declined
    function(){
      alertify.error('You clicked CANCEL');
  }).set('closable', false);

});

你的第三个片段会进入accept.php.

您的代码将:

  1. 使用行 ID 作为数据属性呈现您的表格

  2. 当点击“Accept”时,提示用户确认并将他发送到“accept.php”页面。

  3. 在服务器上运行后处理代码,最终将用户重定向到“home.php”

您可以在文档中找到有关 jQuery 数据的更多信息:https ://api.jquery.com/data/


推荐阅读