【问题标题】:Changing url from JavaScript code and adding another value从 JavaScript 代码更改 url 并添加另一个值
【发布时间】:2011-09-21 20:29:36
【问题描述】:

我有以下mysql查询:

$colname_profile = "-1";
if (isset($_GET['user'])) {
  $colname_profile = $_GET['user'];
}
mysql_select_db($database_conn, $conn);
$query_profile = sprintf("SELECT * FROM users WHERE user_id = %s", GetSQLValueString($colname_profile, "int"));
$profile = mysql_query($query_profile, $conn) or die(mysql_error());
$row_profile = mysql_fetch_assoc($profile);
$totalRows_profile = mysql_num_rows($profile);

以及我需要更改的以下 JS 部分代码:

url: "loadmore.php?lastid=" + $(".profile_history_results:last").attr("uh_id"),

现在,我如何从上面的查询中在 JS 代码中添加另一个值,其格式应为

user='.$row_profile['user_id'].'

所以我需要保留该 JS 网址中已有的内容,并在最后添加该用户 ID。如果您需要完整的 JS,这里是

<script type="text/javascript">
    $(document).ready(function(){
        $("#loadmorebutton").click(function (){
                $('#loadmorebutton').html('<img src="../images/body/icons/ajax-loader.gif" />');
                    $.ajax({
                        url: "loadmore.php?lastid=" + $(".profile_history_results:last").attr("uh_id"),
                        success: function(html){
                        if(html){
                    $("#historywrapper").append(html);
                $('#loadmorebutton').html('Load More');
                }else{
            $('#loadmorebutton').replaceWith('<center>No more posts to show.</center>');
            }
        }
        });
    });
    });
</script>

感谢您的帮助。

【问题讨论】:

    标签: php javascript url


    【解决方案1】:

    也许把 url 行改成这样:

    url: "loadmore.php?lastid=" + $(".profile_history_results:last").attr("uh_id") + "&user=<?php echo urlencode($row_profile['user_id']); ?>",
    

    【讨论】:

      【解决方案2】:

      考虑到问题的提出方式,这将起作用:

      url: "loadmore.php?lastid=" + $(".profile_history_results:last").attr("uh_id") + "user=<?php echo $row_profile['user_id']; ?>",
      

      但是我不知道这是否是您正在寻找的。 $row_profile['user_id'] 是否只是页面上的一个值,还是类似于用户列表,并且对于表中的每一行,是否可能存在不同的 $row_profile['user_id']?如果这是单个值,例如访问页面的用户的 user_id,并且您希望将其与每个 ajax 请求一起传递,则最好将该值存储在其他地方的会话中,因为除非用户登录,否则它不太可能更改在另一个帐户下进出。

      【讨论】:

      • 这是一个单一的值,您建议的代码运行良好,谢谢
      猜你喜欢
      • 1970-01-01
      • 2012-08-20
      • 2012-12-09
      • 2021-10-21
      • 2021-05-09
      • 2012-03-09
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多