【发布时间】:2014-01-07 18:00:15
【问题描述】:
我正在尝试使用 jquery 从 ajax 运行我的 question.php。 这段代码完美运行
<script>
$(':radio').click(function() {
if($(':radio:checked').length === 1) {
var val = $(this).val();
alert(val);
}
});
</script>
但是当而不是
警报(val);
我正在放 ajax 代码,这里是相同的示例,但有变化
<script>
$(':radio').click(function() {
if($(':radio:checked').length === 1) {
var val = $(this).val();
$.ajax({
type: "POST",
url: "questions.php",
//data: {"a":val}
});
}
});
</script>
ajax 不运行 questions.php
questions.php位于webroot目录
编辑: 看起来它返回了我的所有页面,但我的表格没有发生任何变化 questions.php负责向mysql表中插入数据
questions.php
<?php
$selected_button = $_POST['a'];
$conn = mysql_connect('localhost','root','');
if(!$conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "INSERT INTO answers (member_id, question_id, answer) VALUES (1, 2,3)";
mysql_select_db('ipad',$conn);
$result = mysql_query($sql, $conn);
if(!$result)
{
return false;
}
return true;
mysql_close($conn);
?>
问题已解决 问题在于指向文件
<script>
$(':radio').click(function () {
if ($(':radio:checked').length === 1) {
var val = $(this).val();
$.ajax({
type: "POST",
url: "../../../questions.php",
data: {"a": val}
})
.error(function (msg) {
alert(msg);
})
.done(function (msg) {
alert(msg);
});
}
});
</script>
【问题讨论】:
-
questions.php 或 question.php ?
-
你遇到了什么错误,试试 firebug 来捕捉错误
-
ofcourse questions.php ,我在这篇文章中犯了错误,我的siede上的所有东西都有questions.php