0

I am trying to pass a JQuery variable into PHP code within a single page. It appears that the JQuery variable is firing (I get the confirmation alert back), but it is not being read by the PHP. How do I fix this? *edited for clarity

 <!DOCTYPE html>
    <html>
    <head>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script>
                $(document).ready(function(){
                    $("div").click(function(){
                        var pastDATE = $(this).text();
                        $.post("testclick.php", 'pastDATE', function() {
                            alert(pastDATE, "post success");
                        });
                    });
                });
                /*
                var pastDATE = (div.calendar-head
                $.ajax({
                    type: "POST",
                    url: "testclick.php",
                    data: {pastDATE: pastDATE},
                    success: function(data){
                        alert(data);
                        }
                    });
                */      
        </script>
    <style>

    .calendar-head {
        display: block;
        line-height: 32px;
        font-weight: bold;
        text-align: center;
        background: #2F4F4F;
        color:  #2F4F4F;
        background: rgba(0, 0, 0, 0.35);
        }

    </style>    
<body>

<div class="calendar-head">This</div>

<?php

/*attempted to start a session so when page is refreshed the variable remains*/

session_start();
    $_SESSION['pastDATE'] = $pastDATE;

$pastDATE = $_POST['pastDATE'];

if(isset($_POST['pastDATE'])){
    $pastDATE = $_POST['pastDATE'];
    echo "set";
    }

echo "$pastDATE";
?>

</body>
</html>
6
  • You're sending the name of a post variable with no value. It should be something like pastDATE=2009-03-01. Commented Nov 17, 2014 at 15:41
  • { pastDate: '2009-03-01' } Commented Nov 17, 2014 at 15:42
  • The post variable should be the data that is clicked. In other words, pastDATE = This would be the variable I am trying to send. Does that make sense? Commented Nov 17, 2014 at 15:43
  • Move your session_start(); to the top of your file. Session starts only, if there was no output before. A simple line break or a space is output, so start the session before any output! Commented Nov 17, 2014 at 15:45
  • And what do you do in your PHP? $_SESSION['pastDATE'] = $pastDATE; is useless, because $pasteDATE has no value. Why do you create that variable? Commented Nov 17, 2014 at 15:48

2 Answers 2

1
$.post("testclick.php", { pastDATE: pastDATE }, function() {
    alert(pastDATE, "post success");
});

Should do the trick

Sign up to request clarification or add additional context in comments.

Comments

0

Change JavaScript code to:

$(document).ready(function(){
    $("div").click(function(){
        var pastDATE = $(this).text();
        $.post("testclick.php", { data:pastDATE }, function(result) {
            alert(pastDATE, "post success");
        });
    });
});

And read on PHP like $_REQUEST['data']

2 Comments

The JQuery is working perfectly as far as I can tell. However, 'data' is still being read as NULL within the PHP after I click and refresh the page.
I don't know what you want about $pastDATE? $_SESSION['pastDATE'] = $pastDATE; is not work because $pastDATE undefined before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.