Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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] => 
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
286 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>";
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...