如何使用Ajax从PHP文件检索数据(使用JQuery)

如何使用Ajax从PHP文件检索数据(使用JQuery),php,javascript,jquery,html,ajax,Php,Javascript,Jquery,Html,Ajax,我试图在html页面上使用JQuery使用Ajax调用来检索使用PHP存储的数据字段“page” 我想从input.php检索$Page,或者从output.php检索$f1,这是相同的数据。并将此数据放入div id=菜单a href容器中,例如id=homebox 我已经尝试过Ajax调用,但是我需要一些关于将哪些参数放入其中的建议 Ajax调用: $.ajax({ type:"POST", url:"output.php", // Or insert.php ? data: // I am

我试图在html页面上使用JQuery使用Ajax调用来检索使用PHP存储的数据字段“page”

我想从input.php检索$Page,或者从output.php检索$f1,这是相同的数据。并将此数据放入div id=菜单a href容器中,例如id=homebox

我已经尝试过Ajax调用,但是我需要一些关于将哪些参数放入其中的建议

Ajax调用:

$.ajax({
type:"POST",
url:"output.php", // Or insert.php ?
data: // I am not sure what to put in here, 
success: function( data ){
alert(data);
return;
}
});
以下是三个文件中的代码:

insert.php:

$ID=$_POST['ID'];
$Page=$_POST['Page'];
$Description=$_POST['Description'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = ("INSERT INTO Pages (ID, Page, Description)
VALUES 
('$ID','$Page','$Description')");

mysql_query($query);
output.php:

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM Pages";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Page</font></th>
<th><font face="Arial, Helvetica, sans-serif">Description</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"Page");
$f2=mysql_result($result,$i,"Description");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
</tr>

<?php
$i++;
}
?>
index.html:

<script type="text/javascript"> 

$(document).ready(function() 
{

$('#homebox').click(function() 
{  
$('ul:first a').filter(function(i) { return $.trim($(this).text()) == '';   }).first().text('Home')
$('#headingtochange').html('Home');
$('#paragraphtochange').html('The user is currently on the Home page.');
});
});
</script>
<div id="menu">
    <ul>
      <li><a href="#" id="homebox">Home</a></li>
      <li><a href="#" id="kuwebsitebox">KU Website</a></li>
      <li><a href="#" id="studyspacebox">StudySpace</a></li>
      <li><a href="#" id="osisbox">OSIS</a></li>
      <li><a href="#" id="librarybox">Library</a></li>
      <li><a href="#" id="studenthubbox">StudentHUB</a></li>
    </ul>
您可以使用jQuery,例如:

$('#paragraphtochange').load( 'output.php');
它还允许您在callback oncomplete中使用以下简单代码更改标题:

<input type='hidden' name='_page_title' value='Home' />

或者他可以对数据使用序列化字符串,而不必是json对象。有没有办法只选择页面字段内容$f1?请从代码中删除alertdata。例如,添加一个id=out\u put的div。然后添加$'out\u put'.htmldata;没有警觉。如果有任何问题,请告诉我。谢谢,这正是我想要的,但是我只想显示PHP文件中的$Page/$f1值。这可以通过更改参数来完成吗?您可以从php文件中执行echo json_encodearray'page'=>$page,'f1'=>$f1。然后您可以将ajax调用响应中的这些数据用作data.page或data.f1。感谢您的回复,.load方法似乎可以满足我的要求,但是,有没有办法只加载页面字段的内容而不加载整个表?@user1199649最简单的方法是编写$'Page'。load'script/that/generations/only/Page.php'谢谢,这很有意义,但是如果我想在1 href中输出1个页面值,然后在下一个页面值中输出相同的值。我怎么能为每个href只选择一个值?@user1199649-huh?加上你需要做的例子,我不明白-/我希望1$Page值进入1 href,然后进入下一个,依此类推:页面值来自output.php中的$f1
$('#headingtochange').html( $( 'input[name=_page_title]').val()).
var params = {'action': 'save' , 'id':5};
$.ajax({
type:"POST",
url:"output.php", // Or insert.php ?
data: params , 
success: function( data ){
alert(data);
return;
}
});