0

This is my code below for page.php file.

<?php session_start(); ?>
<script type="text/javascript" src="js/jquery-1.8.2.js"></script>
<script type="text/javascript" src="js/jquery.colorbox.js"></script>
<script type="text/javascript" src="js/new-landing.js"></script>
<script type="text/javascript">
    var ans1 = "home";
    function aa(){
        $.post("ajax.php", { "ans": "test" }, function(data){
            alert("Posted");
        }, "html");
    };
</script>
<a href="#" id="q1" onClick="javascript:aa();" >click</a>

and this is where i want to see if my data is posted.

<?php
    session_start();
    $te = $_POST['ans'];
    $_SESSION['demo'] = $te;
    echo "<pre>".print_r($_SESSION,'/n')."</pre>";
?>

when i click the anchor tag. the alert box is shown. but when i refresh the ajax.php page. it shows an error..Notice: Undefined index: ans in ajax.php on line 3

and the print of session is also empty.

Array(
   [demo] => 
)
mnist
  • 6,571
  • 1
  • 18
  • 41
Harsh
  • 11
  • 1
  • 1
  • 5
  • Hi Harsh - did we resolve your problem? If not, please update your question and/or post a comment under an answer so that we will be alerted that you need more assistance. If this question has been answered, though, please close the question by choosing a "best answer". – cssyphus Sep 13 '13 at 16:53

2 Answers2

0

but when i refresh the ajax.php page. it shows an error

It sounds like you want to set the session variable when a value is posted, and get the session variable otherwise:

<?php
session_start();
if (isset($_POST['ans'])) {
    $te = $_POST['ans'];
    $_SESSION['demo'] = $te;
}
echo "<pre>".print_r($_SESSION,'/n')."</pre>";
?>
000
  • 26,951
  • 10
  • 71
  • 101
  • its just for the test purpose. need to just find out why is it not getting data stored in session in ajax.php – Harsh Sep 10 '13 at 16:18
  • but still where is the data been posted ? – Harsh Sep 10 '13 at 16:20
  • When you visit `ajax.php` directly, there is no `$_POST` variable. You need to test if the posted variable exists or not, which I provided in the answer. – 000 Sep 10 '13 at 16:21
0

$.post and $.get are just shorthand versions of the more structured $.ajax(), so I prefer using the latter. The additional structure keeps me straight.

Since you are using jQuery anyway, I would re-structure your code like this:

$('#q1').click(function() {
    var test = "Hello there";
    $.ajax(function() {
        type: "POST",
        url: 'ajax.php',
        data: 'ans=' +test+ '&anothervarname=' + anothervarvalue,
        success: function(recd_data) {
            alert('Rec'd from PHP: ' + recd_data );
        }
    });
});

Note that the data: line is for example purposes and does not match with your code -- just showing you how to pass variables over to the PHP side.

Of course, the above includes removing the inline javascript -- never a good idea -- from your anchor tag HTML, thus:

 <a href="#" id="q1" >click</a>

Also, on the PHP side, you can verify that things are working by adding a test at the top. Matching with the data: line in the example AJAX code, it would look like this:

ajax.php

<?php
$a = $_POST['ans'];
$b = $_POST['anothervarname'];

$response = '<h1>Received at PHP side:</h1>';
$response .= 'Variable [ans] has value: ' . $a . '<br>';
$response .= 'Variable [anothervarname] has value: ' . $b . '<br>';

echo $response;

Important: Note the use of echo, not return, to send values back to the AJAX script.

Also note that you must deal with the stuff returned from PHP in the AJAX success: function ONLY. If you need access to that data outside of the success: function, then you can stick the data into a hidden <input type="hidden" id="myHiddenInput"> element, like this:

success: function(recd_data) {
    $('#myHiddenInput').html(recd_data);
}

Here are some additional examples of simple AJAX constructions:

A simple example

More complicated example

Populate dropdown 2 based on selection in dropdown 1

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Although this is a nice answer, it doesn't answer the question at hand whatsoever! – 000 Sep 10 '13 at 17:00
  • Actually, it does. If the OP places a test at the top of the PHP file (`ajax.php`) and echos back any variables he has POSTed (or even just `die('Here I am');` then he will see exactly what is going on. Restructuring his ajax code will also simplify the situation and help with solving the problem. – cssyphus Sep 10 '13 at 17:09