Billy Bragg in the UK
Billy Bragg in the UK

Reputation: 105

Trying to echo the data-id values

I'm trying to grab the data-id from a link that then opens up a modal, in that modal I want to use the data held in the data-id within a php function. But when I echo it I get back nothing.

I can't work out where I'm going wrong and am looking for some pointers.

Thanks.

I have the following data-id link.

    <div class="package-list-item">
        <a href="javascript:;" class="group-link" data-id="1235"> 
            <figure>
                <img src="<?php echo get_the_post_thumbnail_url( 12121 , 'thumbnail'); ?>" class="img-responsive" alt="<?php echo get_the_title( 12121 ); ?>"><br>
                <figcaption><?php echo get_the_title( 12121 ); ?></figcaption>
            </figure>
        </a>
    </div>

And the following in my modal - channel.php

if(isset($_POST['package_id']) && !empty($_POST['package_id'])) { 
  echo $package_id;
};

And the following JavaScript

$(document).on('click', '.group-link', function() {
                var package_button = $(this),
                    package_id = package_button.data('id'),
                    token = "STFXQAJ0yYLdjiYAI35hrcEZzXRplq3tQRx7EgD0",
                    channels_window = $('#channels_window'),
                    body = $('body');

                channels_window.load("channel.php", { package:package_id, _token:token }, function() {
                    channels_window.fadeIn('slow');
                    body.addClass('window-opened');
                });
            });

Upvotes: 0

Views: 908

Answers (2)

KubiRoazhon
KubiRoazhon

Reputation: 1839

Following your code, You should get package and not package_id since it is the name of the parameter that you give.

$package_id = $_POST['package'];

if($package_id) { 

 echo $package_id;

};

Upvotes: 1

Muhammad Akber Khan
Muhammad Akber Khan

Reputation: 757

use .attr function to get data attribute value

$(document).on('click', '.group-link', function() {
                var package_button = $(this),
                    package_id = package_button.attr('data-id'),
                    token = "STFXQAJ0yYLdjiYAI35hrcEZzXRplq3tQRx7EgD0",
                    channels_window = $('#channels_window'),
                    body = $('body');

                channels_window.load("channel.php", { package:package_id, _token:token }, function() {
                    channels_window.fadeIn('slow');
                    body.addClass('window-opened');
                });
            });

Upvotes: 0

Related Questions