Issue with JSON sending

Hey guys so I am trying to have a JSON send over a value to another page in wordpress to create a session once a image is clicked on.

Here is the JSON code:

Read More
$('.item-post a').click(function() {
        var prodname = $('#hiddenpostitle');

           $.post('/overviewcheck-515adfzx8522', 
            { 
                'ProdName': prodname.val()

            },
            function(response) {

            },
            'json'
        );
    });

The #hiddenposttitle is the following: <?php echo "<input type='hidden' value='$title' id='hiddenpostitle' name='hiddenpostitle'/> "?>

So this sends to the overviewcheck page, which then has a template that has the follow:

<?php
/*
Template Name: overviewbackcheck
*/
session_start();

$prodname= $_POST['ProdName'];

$_SESSION['overviewprod'] = $prodname;

?>

Someone mentioned something about wordpress destroying sessions from calling session_start() but that’s not the issue because I fixed that awhile ago, and have other sessions being made that work. It has to do something with the JSOn sending over and I am not to sure.

From here the overview page is opened with the following:

$(function()
    {
        $('.item-post a').colorbox({opacity:0.3, href:"../overviewa512454dzdtfa"});
    });

This is the colorbox code that will open the file the within the file I have:

<?php echo $_SESSION['overviewprod']; ?>

to echo it out.

Ultimately I am unable to echo out a session and it does not seem anything is being made.

If you could help me out that would be awesome because I need this finished! 🙂

Related posts

Leave a Reply

1 comment

  1. You need to clarify the program flow. What loads what, when?

    “I am trying to have a JSON send over a value”. JSON is a data format, not a method. JSON cannot send anything, it can be sent.

    If you debug the JavaScript (use the Chrome developer tool or Firebug in Firefox), do you have the product name in your onclick function?

    $('.item-post a').click(function() {
        var prodname = $('#hiddenpostitle');
        alert(prodname.val());
    

    (you can inspect network traffic, or set a breakpoint on the “var prodname …” line and simply add a watch on prodname, hover it etc, to see if you have the correct product name)

    Next, see what you got on the server side from the AJAX request.

    <?php
    
    /* Template Name: overviewbackcheck */
    
    session_start();
    var_dump($_POST);die; // You can see what is dumped here in the Network tab in the browser's Developer tool.
    

    If you don’t know how to use browser debug tool, you could always save to the error log and read that instead.

    <?php
    error_log(print_r($_POST, true));
    

    Finally when you fetch the code from “../overviewa512454dzdtfa”, does that script do session_start(); before you do this?

    <?php echo $_SESSION['overviewprod']; ?>
    

    Compared to

    <?php session_start(); echo $_SESSION['overviewprod']; ?>
    

    Don’t have any more ideas at this time, keep at it.