php - 在 jQuery Ajax 函数之后返回 HTML(使用 PHP)

标签 php jquery html ajax

我正在尝试将 Ajax 与 jQuery 结合使用,以便当您在我的自定义论坛中单击用户名时弹出一个小窗口。

我当前的脚本代码:

$(document).ready(function () {
    $('#profile_link').click(function () {

        $.ajax({
            type: 'POST',
            url: 'viewuser.php',
            dataType: 'html',
            contentType: 'text/html',
            data: {
                username: $('#username').val()
            },
            success: function (html) {
                $('#message').addClass('success');
                $('#message').hide().append(html).fadeIn();
            }
        });

        return false;
    });
});

我尝试使用 html(),但这根本不起作用,因为它甚至没有显示响应,即使我可以在 Firebug 中看到响应。

这是 viewuser.php 的代码:

        <?php 
      $docRoot = getenv("DOCUMENT_ROOT");

      require_once $docRoot.'/settings.php';

      loginMySQL();

      selectDatabase();

      $user = $_POST['username'];

      $sql = mysql_query ("SELECT `UserName`,`Avatar`,`Biography`,`Birthday`,`UserLevel`,`BanStatus` FROM users WHERE `UserName` = '$user'");

      $UserInfo = mysql_fetch_array($sql);
       $UserAvatar = $UserInfo['Avatar'];
       $UserBio = $UserInfo['Biography'];
       $UserBirth = $UserInfo['Birthday'];
       $UserLevel = $UserInfo['UserLevel'];
       $BanStatus = $UserInfo['BanStatus'];

   // additional code
   ...
      ?>
      <div class="nav_space">
      </div>
// additional code
      <div class="user_profile_wrapper">
       Hello
       <?=$BanStatus;?>
       <i>Username / Power level:</i>
       <div class="user_profile_name" <?=$LevelColour;?>>
       <?=$user;?> <i><?=$UserLevel;?></i>
       </div>
       <i>User avatar:</i>
       <div class="user_profile_avatar">
      <?
       if (strlen($UserAvatar) > 1) {
       ?>
       <img/ src="<?=$UserAvatar;?>" alt="" width="100" />
       <?
       }
      ?>
       </div>
       <i>Biography:</i><br />
       <div class="user_profile_bio">
       <?=$UserBio;?>
       </div>
       <i>Birthdate:</i>
       <div class="user_profile_birthdate">
       <?=$UserBirth;?>
       </div>
       <?=$AUserLevel;?>
      </div>
      <div class="nav_space">
      </div>

问题是,当它返回代码时,它实际上并不显示任何变量($BanStatus、$UserLevel 等)。

有什么想法吗?

最佳答案

理想情况下,您不应在 <?php 之间进行更改和<?因为我相信对短标签的支持正在被放弃。

此外,您还没有清理您的用户名

$user = mysql_real_escape_string  ($_POST['username']);

更新1

下面的代码是一个工作示例,我已经稍微清理了您的代码。但是,随着您的原始代码更改username : $('#username').val() username : $('#username').html()应该适合你。

so.html 代码

  <a href="www.google.com" id="profile_link"><div id="username">Someusername</div></a>

  <div id="message"></div>
<script>
  $("#profile_link").click(function(event){
    event.preventDefault();
    var v_username = $("#username").html();
    $.post("viewuser.php",{"username": v_username},function(data) {
      $('#message').addClass('success').hide().html(data).fadeIn();
    }, "html");

  });
</script>

viewuser.php 代码

<?php
echo "The submitted request was for the username $_POST[username]";
?>

输出:

The submitted request was for the username Someusername

更新2

so.html 代码

  <a href="www.google.com" class="profile_link"><span class="username">Username1</span></a>
    <a href="www.google.com" class="profile_link"><span class="username">Username2</span></a>

  <div id="message"></div>
<script>
  $(".profile_link").click(function(event){
    event.preventDefault();
    var v_username = $(this).children(".username").html();
    $.post("viewuser.php",{"username": v_username},function(data) {
      $('#message').addClass('success').hide().html(data).fadeIn();
    }, "html");

  });
</script>

我已经为您再次更新了。我们使用 $(this).children(".username") 来确保我们从所选链接返回用户名。我们已更改为使用类,因为现在每个元素都有多个实例,并且我已将 html 更改为使用 span 标记,因为它更合适一点。

关于php - 在 jQuery Ajax 函数之后返回 HTML(使用 PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3081223/

相关文章:

php - fatal error : Call to a member function execute() on boolean

jquery - 如何禁用 google web viewer 全屏选项

重复 .ajax 调用导致 jQuery 内存泄漏

javascript - 我如何使用构造函数来编写这段代码?

javascript - 如何在关闭时制作动画?[Javascript]

javascript - 在 JavaScript 中获取名为颜色的 CSS/HTML 的 RGB 值

html - Div 未按预期居中

php - 喜欢按钮到页面照片

php - Yii Booster 日期选择器无法正常工作

PHP CLI : what directory?