Bohdan Lashta
Bohdan Lashta

Reputation: 13

POST method in wordpress

I trying to make opening WP posts in popup. Firts part of code its form in posts loop where I get query of posts what should be opened

<form id="postForm" method="POST">
      <input id="postQuery" style="display: none" name="postQuery" value="<?php echo get_the_ID() ?>">
       <input id="sendQueryBtn" data-toggle="modal" data-target="#exampleModalLong" type="submit" value="<?php the_title(); ?>"> 
    </form>

Next is my JS, where I do query check by alert

$(document).ready(function () {
$("form").submit(function () {
    let xuinya = $(this).serialize();
    $.ajax({
        type: "POST",
        url: '.../footer.php',
        data: xuinya,
        success: function (data) {
            alert(xuinya)
        },
        error: function (jqXHR, text, error) {
            $('#modalBody').html(error);
        }
    });
    return false;
});});

And I final, here is part of HTML with modal, where I try to use POST

<div id="modalBody" class="modal-body">
        <?php
            echo $_POST["postQuery"];
            echo apply_filters( 'the_content', get_post( $_POST["postQuery"] )->post_content );
        ?>
     </div>

My problem is because when I check query in JS - qet alert message with correct value, but in php I always qet simple "1".

Upvotes: 0

Views: 742

Answers (1)

Beneris
Beneris

Reputation: 637

I don't get why you posting to footer.php, I think its should be admin-ajax.php just simply add to youfooter.php

<script>
    var ajax_url = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
</script>

Change url value in js to ajax_url, make sure hat with data you posting variable action and then create function in functions.php, something like this(if you sending action value as 'get_pop_posts')

add_action( 'wp_ajax_get_pop_posts', 'get_pop_posts_init' );
add_action( 'wp_ajax_nopriv_get_pop_posts', 'get_pop_posts_init' );

function get_pop_posts_init() {
    $data = $_POST;
    print_r($data);
    die();
}

Upvotes: 1

Related Questions