使用jQuery从.php文件检索数据


Retrieving data with jQuery from .php file

我正在玩一些脚本,下面是我所拥有的:

<!doctype html>
<html>
<head>
    <title>Home page</title>
    <link rel="shortcut icon" href="/images/favicon.ico" />
    <link rel="icon" href="/images/favicon.ico" type="image/x-icon" />
            <link rel="stylesheet" href="/css/base.css" type="text/css" media="all" />
            <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/base/jquery-ui.css" type="text/css" media="all" />
            <link rel="stylesheet" href="http://static.jquery.com/ui/css/demo-docs-theme/ui.theme.css" type="text/css" media="all" />
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
            <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js" type="text/javascript"></script>
            <script src="http://jquery-ui.googlecode.com/svn/tags/latest/external/jquery.bgiframe-2.1.2.js" type="text/javascript"></script>
            <script src="http://jquery-ui.googlecode.com/svn/tags/latest/ui/minified/i18n/jquery-ui-i18n.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        function get() {
            $.post('data.php',
                function(output) {
                    $('#container').html(output).show();
            });
    </script>
</head>
<body>
    <script>
    $(function() {
        $( "#tabs" ).tabs();
    });
    </script>
<div class="demo">
<div id="tabs">
    <ul>
        <li><a href="#tabs-1">Статии</a></li>
        <li><a href="#tabs-2">Коментари</a></li>
        <li><a href="#tabs-3">Европа</a></li>
        <li><a href="#tabs-4">Свят</a></li>
    </ul>
    <div id="tabs-1">
        <a id="BB" onClick="get();">Тайният проект Бойко Борисов</a>
    </div>
    <div id="tabs-2">
        <p>Защо се изкупува земя от северозападна България</p>
    </div>
    <div id="tabs-3">
        <p>Бъдещето на Гърция</p>
    </div>
    <div id="tabs-4">
        <p>Мястото на САЩ е политиката на ЕС</p>
    </div>
</div>
<div id="container">
</div>
</body>
</html>

稍后,我想使用data.php文件连接到数据库并从中提取数据,但现在我只想让它适用于存储在data.php中的任何数据。

我使用这个脚本,我认为主要的问题是:

<script type="text/javascript">             
    function get() {
        $.post('data.php',              
        function(output) {                          
            $('#container').html(output).show();                    
    });        
</script>

我想把数据放在<div id="container></div>

最后的想法是从容器div中的菜单加载链接,我尝试这样做:

<div id="tabs-1">
    <a id="BB" onClick="get();">Тайният проект Бойко Борисов</a>

data.php中,我只有一个echo "Hello world";,但当我单击链接时,什么都没有显示。我想问题不止一个,但我们非常感谢您的帮助。

谢谢,Leron。

您应该关闭get()函数范围。

  function get() {
            $.post('data.php',
                function(output) {
                    $('#container').html(output).show();
            });
  }

你刚刚忘记了花括号。


$.post("test.php",{名称:"John",时间:"2pm"},函数(数据){});

你可以发送这样的帖子数据。

你想选择标签还是想发送一些数据?

$('#BB').click(function() { get(); });

您可以像上面的代码一样将函数绑定到。

这是你想要的吗?