【问题标题】:how to get and pass a link parameter using html and javascript如何使用 html 和 javascript 获取和传递链接参数
【发布时间】:2016-09-17 20:24:06
【问题描述】:

我想知道如何从这个选项卡中的 ajaxbtn.php 将“包含”url 中的专辑 ID# 传递给实际请求该 URL 的文件

  1. 主页.php: HTML:<a href="javascript:;" id="album" class="ajax_tab">ALBUM ID #22</a>

JAVASCRIPT:

$( document ).ready(function GoAlbum() {
  //alert("pageload event fired!");
    $.ajax({
        url: 'assets/ajaxbtn.php',
        type: 'POST',
        data: {
        action:'album'
        },
        success: function(response){
            //alert(response);
            $("#content").html(response);
            $("#loading_image").hide();
            window.history.pushState("object or string", "Title", "/album");
        }
    }); 

现在我有另一个处理选项卡操作的文件

ajaxbtn.php:

if($_POST['action'] == 'album'){
   include("http://gypsychristiannetwork.com/assets/album.php?AlbumID=22");
    }

这将是album.php 的内容

if(isset($_GET['AlbumID'])){
    $AlbumID = $_GET['AlbumID'];
        echo '<h1>TEST: '.$AlbumID.'</H1>';
    }

正如您在此处看到的,“id”已被 html 链接使用,并且 href 设置为 javascript

所以我不确定要找到解决方案的方向

感谢您的帮助

【问题讨论】:

    标签: javascript php html ajax parameters


    【解决方案1】:

    在您的 ajax 调用中,您可以添加:

    id: 22
    

    原来是这样:

    $.ajax({
            url: 'assets/ajaxbtn.php',
            type: 'POST',
            data: {
                action:'album',
                id: 22,
            },
            success: function(response){
                //alert(response);
                $("#content").html(response);
                $("#loading_image").hide();
                window.history.pushState("object or string", "Title", "/album");
            }
    

    然后在PHP文件中:

    if($_POST['action'] == 'album'){
       include("http://gypsychristiannetwork.com/assets/album.php?AlbumID=".$_POST['id']);
    }
    

    如果您需要来自 HTML 部分的 id:

    <a href="javascript:;" id="album" data-album-id="22" class="ajax_tab">ALBUM ID #22</a>
    

    然后,在 ajax 调用中:

    data: {
        action:'album',
        id: $(this).attr("data-album-id"),
    },
    

    【讨论】:

      猜你喜欢
      • 2018-02-25
      • 2012-10-16
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多