2011-08-11 28 views
1

我有一个(可能是超级简单的)问题。下面的代码应该是_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(); 
?> 
+1

您的AJAX函数中的'data'必须是'id = xxx'的形式。这是一个错字吗? –

+0

@Andrew:但它是一个变量......它根据用户点击的id(链接)而变化。 xxx会是什么? – Zakman411

+0

@ Zakman441:我看到你在变量'id'中有它。试试''id ='+ id'。混淆我知道大声笑 –

回答

0

data在你的AJAX功能需要这样的形式 'ID = XXX' 的。我看到你在变量ID中有它。试试data: 'id=' + id。混淆我知道。

这里的解释是POST数据应该是a=b,c=d,...等等的形式。这样PHP就会将它作为$ _POST字典中的键/值对进行提取。现在你有一个变量id你想发送(价值),你也希望这是(密钥)的名称。因此你需要做data: 'id=' + id。如果id=2,那么将评估为data: 'id=2',这是正确的。

最终,正如@Stephen指出的那样,最好使用Object作为数据字段,因为它可以说更加优雅。 data: {'id': id}也应该可以工作,并且您可以在将来添加更多变量。

+0

但是如果他以后需要添加更多的数据,他会不断地有这个混乱的字符串来处理..你使用对象更好.. – Stephen

+0

我完全同意你@Stephen,我已经编辑过我的答案反映了这一点。 –

0

你试过数据:{ID:2} - 对象,而不是一个数组。

+0

这实际上工作!但是,我需要传输id变量,而不是'2'。有没有办法只是做数据:{id}?这对我来说是一个错误.. – Zakman411

+0

data:{'id':$(this).attr('id')} – Stephen

+0

此外,你已经把它放在一个名为id的变量..所以语法是小怪但数据:{'id':id} – Stephen

0

我相信你的ajax调用中的数据是错误的。 php在你的调用中引用$ _POST ['id'],但不会发送var ID。

从:http://api.jquery.com/jQuery.ajax/

dataObject时,字符串

数据要被发送到服务器。如果 不是字符串,它将被转换为查询字符串。它附加到GET请求的url。请参阅 processData选项以防止此自动处理。对象必须是 是键/值对。如果value是一个数组,则jQuery将基于传统设置 (如下所述)的值使用相同密钥对多个 值进行序列化。

应该更像这样:

数据: “ID = 2”,

+0

这是行得通的,但只返回2的member_id。我怎样才能发送基于用户输入而改变的'id'变量? – Zakman411