【问题标题】:sending user a mesage by passing their id to a modal box通过将用户的 id 传递给模态框来向用户发送消息
【发布时间】:2017-10-24 02:36:23
【问题描述】:

我正在尝试通过将用户用户名/ID 传递给模态以进行识别,从而将消息表单创建到引导模态中。

这是一个制作注册用户列表的html/php代码。

<div class="col-lg-4 col-md-4 col-sm-4 mb">
                            <div class="content" style="margin-bottom:5px;">
                            <ul class=" extended inbox">
                            <div class="notify-arrow notify-arrow-green"></div>
                            <li>
                                <p class="green">People you can follow</p>
                            </li>
                            <?php                
                            //Show all  users    
                             foreach ($users as $row) {
                            ?>
                            <li style="padding:3px; margin-bottom:3px; width:100%; background:#CF9;">
                                <a href="index.html#">
                                    <span class="photo" style="float:left; padding:4px;"><img alt="avatar" src="uploads/<?php echo $row['pix']; ?>" width="50" height="50"></span>
                                    <span class="subject" style="float:left;">
                                    <span class="from">
                                       <?php 
                                        echo '<a href="#">'.$row['user_lastname']. '&nbsp;'.$row['user_firstname'].'</a>';
                                        ?>  
<a class="" data-toggle="modal" data-target="#profile_pic_modal"><span class="glyphicon glyphicon-comment"></span></a>
                                       </span><br>
                                    <span class="time">
                                    <?php
                                    echo  (($row['receiver'] === $row['user_id'] && $row['sender'] === $user_id) 
                                     ? '<button class="btn follow following " rel="'.$row['user_id'].'">Following</button>'
                                     :' <button class="btn follow" rel="'.$row['user_id'].'">
                                        <i class="fa fa-user-plus alert-info"></i> Follow </button>');

                                       ?>
                                    </span>
                                    </span>

                                </a>
                            </li><br>

                            <?php
                             }
                             ?>
                            </ul>
                            </div>

当用户点击另一个用户的消息图标时,他/她应该能够发送消息。有人可以告诉我如何使用 php/mysqli 和 ajax 来做到这一点

【问题讨论】:

  • 你能格式化你的代码吗?
  • @E_p,我想要的是将用户 ID 传递给 。当用户单击链接时,将出现一个模式框,其中用户名/id 作为 id 传递给模式,以便获取消息发送给哪个用户。

标签: php mysql ajax


【解决方案1】:

首先添加一个data-(anyName)标签,如下所示

<?php $ID = 'The persons Username'; ?>

    <a href='#' class='btn btn-warning btn-xs open-AddBookDialog' data-id='$ID' data-toggle='modal'>My Modal</a>

然后在你的模态体内放置一个输入标签,ID为example(MyID)

<input type="text" id="MyID">

然后使用下面的代码 (jquery) 这会将 data-id 的值传递到模态显示的输入标签中。

$(document).on("click", ".open-AddBookDialog", function () {
     var myId = $(this).data('id');

     $(".modal-body #bMyID").val(myId);

      $('#myModal').modal('show');
});

【讨论】:

    【解决方案2】:

    用户名(登录名?)和登录用户的 id 等信息应该由Session 处理,不像你有充分的理由不这样做。当用户进入系统时,您已经加载了一堆数据以确保他是有效的,因此不总是从登录用户那里检索当前正在使用您的系统的所有数据的实用方法总是好的使用SESSIONs 来处理这个问题(想象一下有大量用户使用您的系统,而您需要在系统内的每次点击和例程中检索每个用户)。

    实际上,只需打开您的模式并使用已启动的会话验证您的例程,并设置用户信息。

    要澄清其他用户数据(列出当前用户将单击以发送消息的数据),您可以使用以下方法:

    您的 HTML:

        <a href="#" data-userid='<?= $row['user_id']; ?>'>
    <span class="photo" style="float:left; padding:4px;"><img alt="avatar" src="uploads/<?php echo $row['pix']; ?>" width="50" height="50"></span>
    <span class="subject" style="float:left;">
    <span class="from">
    <?php 
      echo '<a href="#">'.$row['user_lastname']. '&nbsp;'.$row['user_firstname'].'</a>';
      ?>  
    <a class="" data-toggle="modal" data-target="#profile_pic_modal"><span class="glyphicon glyphicon-comment"></span></a>
    </span><br>
    <span class="time">
    <?php
      echo  (($row['receiver'] === $row['user_id'] && $row['sender'] === $user_id) 
       ? '<button class="btn follow following " rel="'.$row['user_id'].'">Following</button>'
       :' <button class="btn follow" rel="'.$row['user_id'].'">
          <i class="fa fa-user-plus alert-info"></i> Follow </button>');
    
         ?>
    </span>
    </span>
    </a>
    

    #Modal (Assuming you're using v4)

    <div class="modal hide fade" id="user-message">
         <div class="modal-header">
            <button class="close" data-dismiss="modal">×</button>
            <h3 class="hdrtitle">Hey <?= $_SESSION['login']; ?>, Send message to <span></span></h3>
         </div>
         <div class="modal-body">
             <label for='msg'>Message:</label>
             <textarea id='msg' rows="4" cols="50" name='msg'></textarea>
             <button id='send-msg'>Send Msg</button>
         </div>
    </div>
    

    然后显示:

    $('a').on('click', function(){
    //retrieve all other user data
    $.get( "yourcode.php?userid=" + $(this).data('userid'), function( data ) {
      $('.modal h3 span').html(data.username);
      //FILL YOUR MODAL AS YOU WISH
      $('.modal').modal('toggle');
    });
    
    });
    

    但最好的方法是在局部视图中单击并挂载模态框,使 HTML 已经挂载了所有您想要触发模态例程的信息,避免用户在第一次访问时并不真正需要的 html 结构页面。

    $('a').on('click', function(){
    //retrieve all other user data
    $.get( "partialmodal-sendmsg.php?userid=" + $(this).data('userid'), function( data ) {
       $('body').append(data);
      //FILL YOUR MODAL AS YOU WISH
      $('.modal').modal('toggle');
    });
    
    });
    

    一些必须阅读的内容:

    When should I use session variables instead of cookies?

    http://cse.unl.edu/~riedesel/pub/cse413/Project%202/SESSION.pdf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 2017-02-07
      • 2019-04-03
      • 2016-11-30
      • 1970-01-01
      • 2018-03-18
      • 2023-03-13
      相关资源
      最近更新 更多