我有一个(可能是超级简单的)问题。下面的代码应该是_POST(使用AJAX)一个名为'id'的变量到名为getYourData.php的外部文件。将变量传递给外部PHP文件
我认为这个问题在下面。 'data'部分似乎不起作用 - 我甚至试着把[data:'2']简单地放在SELECT语句中'2'。但这甚至不起作用。
$.ajax({
type: 'POST',
url: 'getYourData.php',
data: 'id',
success: function(msg){
//everything echoed in your PHP-File will be in the 'msg' variable:
$('#selectTwo').html(msg)
$('#selectTwo').fadeIn(500);
}
});
这里的代码的其余部分(片段 - jQuery的已导入)
<!-- First Box: click on link shows up second box -->
<div id="selectOne" style="float: left; margin-right: 10px; border: #666 thin solid; padding: 10px;">
<a href="#" id="1">One</a><br />
<a href="#" id="2">Two</a><br />
<a href="#" id="3">Three</a>
</div>
<!-- Second Box: initially hidden with CSS "display: none;" -->
<div id="selectTwo" style="float: left; margin-right: 10px; display: none; border: #666 thin solid; padding: 10px;"></div>
<!-- The JavaScript (jQuery) -->
<script type="text/javascript">
//Do something when the DOM is ready:
$(document).ready(function() {
//When a link in div with id "selectOne" is clicked, do something:
$('#selectOne a').click(function() {
//Fade in second box:
$('#selectTwo').fadeIn(500);
//Get id from clicked link:
var id = $(this).attr('id');
$.ajax({
type: 'POST',
url: 'getYourData.php',
data: '2',
success: function(msg){
//everything echoed in your PHP-File will be in the 'msg' variable:
$('#selectTwo').html(msg)
$('#selectTwo').fadeIn(500);
}
});
//Depending on the id of the link, do something:
if (id == 'one') {
//Insert html into the second box which was faded in before:
$('#selectTwo').html('One<br />is<br />selected')
} else if (id == 'two') {
$('#selectTwo').html('Two<br />is<br />selected')
} else if (id == 'three') {
$('#selectTwo').html('Three<br />is<br />selected')
}
});
});
</script>
getYourData.php - 创建一个基于从主页面传递的 '身份证' 的自定义SELECT语句。出于某种原因,这是行不通的。只能当我故意设置一个哑炮变量($ ID2)
<?php
$username="primary";
$password="testpass";
$database="testdb";
mysql_connect(localhost,$username,$password) or die ('Unable to connect...');
mysql_select_db($database) or die('Error: '.mysql_error());
//Intentionally creating a dud variable will create a good SELECT statement and work
$id2 = "3";
$id = $_POST['id'];
$query = mysql_query('SELECT * FROM members WHERE member_id='.$id);
$result = mysql_fetch_assoc($query);
//Now echo the results - they will be in the callback variable:
echo $result['firstname'].', '.$result['lastname'];
mysql_close();
?>
您的AJAX函数中的'data'必须是'id = xxx'的形式。这是一个错字吗? –
@Andrew:但它是一个变量......它根据用户点击的id(链接)而变化。 xxx会是什么? – Zakman411
@ Zakman441:我看到你在变量'id'中有它。试试''id ='+ id'。混淆我知道大声笑 –