2015-02-23 71 views
-1

我想製作一個腳本,將PM發送給從MySQL中選擇的用戶。如何使用AJAX,PHP和MySQL發送個人消息?

我有用戶列表:

http://i.stack.imgur.com/9fRZV.png

當我點擊PM按鈕,我得到javascipt的sceen:

http://i.stack.imgur.com/4bJ9A.png

如何填寫的Javascript屏幕領域與用戶價值自動列出並將值發送到其他PHP文件(使用AJAX)?

用戶名單表的代碼:

所有的
 <!-- Table --> 
     <table class="table table-condensed"> 
     <tr class="bg_h"> 
     <th>Username</th> 
     <th>Email</th> 
     <th>Rank</th> 
     <th>Active?</th> 
     <th>Registration date</th> 
     <th>IP</th> 
     <th>PM</th> 
     </tr> 

     <?php 
     $sql = "SELECT * FROM users ORDER BY user_id ASC"; 
     $query = $dbh->prepare($sql); 
     $query->execute(); 
     $list = $query->fetchAll(); 

     foreach ($list as $row) { 
     ?> 
     <tr class=""> 
     <td><?php echo $row['user_name']; ?></td> 
     <td><?php echo $row['user_email']; ?></td> 
     <td> 
     <?php 
     PHP code of getting user rank. 
     ?> 
     </td> 
     <td> 
     <?php 
     PHP code to chech is user active. 
     ?> 
     </td> 
     <td> 
     <?php echo $row['user_registration_datetime']; ?> 
     </td> 
     <td> 
     <?php echo $row['user_registration_ip']; ?> 
     </td>   


     <td> 
     <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#send-pm">@PM</button> 
     <div class="modal fade" id="send-pm" tabindex="-1" role="dialog" aria-labelledby="send-pm-label" aria-hidden="true"> 
     <div class="modal-dialog"> 
     <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
     <h4 class="modal-title" id="send-pm-label">Send personal message</h4> 
     </div>  
     <div class="modal-body">   
     <div class="form-group"> 
     <label for="name" class="control-label">Receiver name</label> 
     <textarea style="height:35px;" name="name" id="name" class="form-control" ></textarea> 
     </div> 
     <div class="form-group"> 
     <label for="email" class="control-label">Email</label> 
     <textarea style="height:35px;" name="email" id="email" class="form-control" ></textarea> 
     </div> 
     <div class="form-group"> 
     <label for="message" class="control-label">Message</label> 
     <textarea style="height:250px;" name="message" id="message" class="form-control" ></textarea> 
     </div> 
     </div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 
     <button type="button" id="submit" class="btn btn-primary">SEND</button> 
     </div>  
     </div> 
     </div> 
     </div> 
     </td> 
      </tr> 
      <?php 
      } 
      ?> 
     </table> 
+1

您是否正在使用當前文件發生錯誤?我建議清理你的問題,只提供必要的代碼,因爲我無法弄清楚你可能會遇到什麼問題。 – arleslie 2015-02-23 17:49:06

+0

我不知道如何用Mysql值填寫AJAX textareas。 因爲我通過foreach獲得所有用戶。 foreach($ list as $ row){ echo $ row ['user_name']; } – senti3rd 2015-02-23 17:53:21

+1

我認爲這個問題缺少必要的代碼,它是處理PM按鈕單擊事件的JavaScript,並打開模式對話框來發送消息。在這一點上,你需要從表格行中提取用戶名和電子郵件,並填充相關的字段。 – GarethD 2015-02-23 17:57:14

回答

0

首先,你不`噸需要在表中每一行一個模式對話框。從for-each循環中取出它。 另外,爲每個@PM按鈕添加一個類以便於選擇。以後,將一些操作綁定到@PM按鈕單擊事件。這裏是代碼,請確保jquery已加載。

foreach ($list as $row) { 
     ?> 
     <tr class=""> 
      <td><?php echo $row['user_name']; ?></td> 
      <td><?php echo $row['user_email']; ?></td> 
      <td> 
      <?php 
      //PHP code of getting user rank. 
      ?> 
      </td> 
      <td> 
      <?php 
      //PHP code to chech is user active. 
      ?> 
      </td> 
      <td> 
       <?php echo $row['user_registration_datetime']; ?> 
      </td> 
      <td> 
       <?php echo $row['user_registration_ip']; ?> 
      </td> 
      <td> 
       <button type="button" class="btn btn-primary data-grab-trigger" data-toggle="modal" data-target="#send-pm">@PM</button>   
      </td> 
     </tr> 
     <?php 
     } 
     ?> 
     </table> 
     <div class="modal fade" id="send-pm" tabindex="-1" role="dialog" aria-labelledby="send-pm-label" aria-hidden="true"> 
      <div class="modal-dialog"> 
       <div class="modal-content"> 
        <div class="modal-header"> 
         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
         <h4 class="modal-title" id="send-pm-label">Send personal message</h4> 
         </div>  
         <div class="modal-body">   
         <div class="form-group"> 
         <label for="name" class="control-label">Receiver name</label> 
         <textarea style="height:35px;" name="name" id="name" class="form-control" ></textarea> 
         </div> 
         <div class="form-group"> 
         <label for="email" class="control-label">Email</label> 
         <textarea style="height:35px;" name="email" id="email" class="form-control" ></textarea> 
         </div> 
         <div class="form-group"> 
         <label for="message" class="control-label">Message</label> 
         <textarea style="height:250px;" name="message" id="message" class="form-control" ></textarea> 
         </div> 
         </div> 
         <div class="modal-footer"> 
         <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 
         <button type="button" id="submit" class="btn btn-primary">SEND</button> 
        </div>  
       </div> 
      </div> 
     </div> 
     <script> 
      $(function(){ 
       $('.data-grab-trigger').on('click', function (e) { 
        _email = $(this).closest('tr').find('td').eq(0).text(); //get user email from the row where button was clicked 
        _name = $(this).closest('tr').find('td').eq(1).text(); //get user name from the row where button was clicked 

        $("#email").val(_email); 
        $("#name").val(_name); 
       }); 


      }); 
     </script> 
+0

我試圖申請你的代碼,但我失敗了。我用我非常需要的信息更新了我的第一篇文章。 – senti3rd 2015-02-24 18:11:31

+0

我仍然沒有看到表格,只有表格。 – 2015-02-24 18:27:22

+0

用PHP和完整的HTML代碼更新。 – senti3rd 2015-02-24 18:33:50