$。员额没有发布数据 [英] $.post not posting data
问题描述
您好,这是我的$ C $下page.php
hi this is my code for page.php
<?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>
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>";
?>
当我点击锚标记。显示警告框。但是当我刷新ajax.php页面。它显示了一个error..Notice:未定义指数:ANS在
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
C:\xampp\htdocs\healthqueens\hello\ajax.php on line 3
和会话的打印也是空的。
阵列
(
[试玩] =&GT;
)
请,如果任何人都可以告诉我的错误。
and the print of session is also empty.
Array
(
[demo] =>
)
Please if anyone can show me the mistake.
推荐答案
$。交
和 $。获得
是的更有条理只是简写 $。阿贾克斯()
,所以我使用后者preFER。额外的结构让我直。
$.post
and $.get
are just shorthand versions of the more structured $.ajax()
, so I prefer using the latter. The additional structure keeps me straight.
既然你无论如何使用jQuery,我会重新构造你的code是这样的:
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 );
}
});
});
注意数据:
线作示例,不与你的code匹配 - 只是显示你如何通过传递变量到PHP端
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.
当然,上面包括去除内嵌的JavaScript - 不是一个好主意 - 从你的锚标记HTML,是这样的:
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>
此外,在PHP端,你可以验证事情都是由在顶部增加了测试工作。与数据匹配:
在这个例子中的AJAX code线,就成了这个样子:
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;
重要提示:请注意使用回声
,不是返回
,以发送回值到AJAX脚本
Important: Note the use of echo
, not return
, to send values back to the AJAX script.
另外请注意,您必须处理的东西,从PHP的在AJAX 返回成功:
函数仅。如果您需要访问成功的数据之外的:
函数,那么你能坚持数据到一个隐藏的&LT;输入类型=隐藏 ID =myHiddenInput&GT;
元素,像这样的:
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);
}
下面是简单的AJAX建设一些额外的例子:
Here are some additional examples of simple AJAX constructions:
<一个href="http://stackoverflow.com/questions/13734395/form-inside-of-load-not-posting-correctly/13734466#13734466">A简单的例子
<一个href="http://stackoverflow.com/questions/17887098/two-post-requests-one-after-the-other-second-post-request-doesnt-get-execut/17887642#17887642">More复杂的例子
这篇关于$。员额没有发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!