Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

php - $.post not posting data

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...