从使用JQuery / AJAX的PHP文件获取变量 [英] Get variable from PHP file using JQuery/AJAX

查看:135
本文介绍了从使用JQuery / AJAX的PHP文件获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这里的第一篇文章,我希望有人能够帮助我。 在过去的一周,我一直在我的一个项目。很显然,我已经坚持了最后一部分。
所以基本上,我有一个AJAX聊天,当我提出我行发送(使用POST方法),整条生产线进行分析(以一个名为analysis.php文件)。
聊天线进行分析,然后找到我需要做的查询上一个MySQL数据库变量。
所有我现在需要的,就是有这个变量采取与jQuery,AJAX,并把它放在一个div在我的HTML文件(这样就可以在显示右 - 左 - 无论聊天)。

This is my first post here and I hope that someone will be able to help me. For the past week I have been working on a project of mine. Apparently, I have stuck with the last part.
So basically, I have an AJAX chat and when I submit a line I send (using a Post method) the whole line to be analyzed (to a file named analysis.php).
The chat line is being analyzed and find the variable I needed by doing queries on a MySql Database.
All I need now, is to have this variable taken with JQuery-AJAX and put it on a div in my html file(so it can be displayed on the right-left-whatever of the chat).

下面是我的文件:
analysis.php

Here are my files :
analysis.php

<?php
$advert = $row[adverts];
?>

AJAX-chat.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX Chat</title>

<link rel="stylesheet" type="text/css" href="js/jScrollPane/jScrollPane.css" />
<link rel="stylesheet" type="text/css" href="css/page.css" />
<link rel="stylesheet" type="text/css" href="css/chat.css" />

</head>

<body>

<div id="chatContainer">

    <div id="chatTopBar" class="rounded"></div>
    <div id="chatLineHolder"></div>

    <div id="chatUsers" class="rounded"></div>
    <div id="chatBottomBar" class="rounded">
        <div class="tip"></div>

        <form id="loginForm" method="post" action="">
            <input id="name" name="name" class="rounded" maxlength="16" />
            <input id="email" name="email" class="rounded" />
            <input type="submit" class="blueButton" value="Login" />
        </form>

        <form id="submitForm" method="post" action="">
            <input id="chatText" name="chatText" class="rounded" maxlength="255" />
            <input type="submit" class="blueButton" value="Submit" />
        </form>

    </div>

</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="js/jScrollPane/jquery.mousewheel.js"></script>
<script src="js/jScrollPane/jScrollPane.min.js"></script>
<script src="js/script.js"></script>
</body>
</html>

所以,我基本上试图让从anal​​ysis.php文件$广告(整个分析完成后),并通过使用JQuery / AJAX最终将它传递给Ajax-chat.html文件。 任何帮助真的是AP preciated。我用Google搜索的一切,但没有发现什么来帮助我。 先谢谢了。

So, I am basically trying to get the $advert from the analysis.php file(after the whole analyze is done) , and by using JQuery/AJAX pass it eventually to the ajax-chat.html file. Any help is really appreciated. I have googled everything but haven't found something to help me. Thanks in advance.

推荐答案

如果我理解正确的,你需要使用JSON。有样品

If I understand right, you need to use JSON. There is a sample.

在你的PHP写:

<?php
// filename: myAjaxFile.php
// some PHP
    $advert = array(
        'ajax' => 'Hello world!',
        'advert' => $row['adverts'],
     );
    echo json_encode($advert);
?>

比,如果你正在使用jQuery,只是写:

Than if you are using jQuery, just write:

    $.ajax({
        url : 'myAjaxFile.php',
        type : 'POST',
        data : data,
        dataType : 'json',
        success : function (result) {
           alert(result['ajax']); // "Hello world!" alerted
           console.log(result['advert']) // The value of your php $row['adverts'] will be displayed
        },
        error : function () {
           alert("error");
        }
    })

而这一切。这是JSON - 它是用来发送变量,数组,对象等服务器和用户之间。更多资讯: http://www.json.org/ 。 :)

这篇关于从使用JQuery / AJAX的PHP文件获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆