我正在尝试使用 jquery 和 ajax 将 html 文件中的变量传递给我想要处理它的 php 文件。我有两个测试文件:
ajax.html
<html>
<head>
<title>ajax</title>
<script type="text/javascript" src="jquery.js"></script>
<link href="ajax.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function () {
$("#call_back").click(function () {
$.post(
"ajax.php", {
value: $("#input_text").val()
},
function (data) {
$("#response_text").val(data);
}
);
});
});
</script>
</head>
<body>
<div id="wrapper">
<h3>Ajax</h3>
<div class="entry-wrapper">
<h4>Data to send to the server</h4>
<input type="text" size="50" id="input_text" />
<input type="button" value="Ajax Callback" id="call_back" />
</div>
<div id="response_wrapper">
<textarea id="response_text"></textarea>
</div>
</div>
</body>
</html>
和 ajax.php
<?php
$value = $_POST['value'];
echo "Returned from the server: $value";
?>
html 工作正常。如果我在文本字段中输入一个值,它将被传递到 php 文件,处理并发送回以显示在 textarea 中。我查看了 firebug,控制台中的 post 标记显示了我输入的值。
问题是我希望能够使用 php 文件中的变量进行算术运算或使用它运行 sql 查询,但是当我打开 php 文件时它显示为未定义的索引。
我已经查看了将变量从 jquery 传递到 php 的多个线程,但仍然没有找到答案。我不知道问题是什么。非常感谢您的建议。谢谢。