使用JQuery / AJAX从PHP文件获取变量

时间:2022-10-08 17:34:43

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).

这是我在这里的第一篇文章,我希望有人能够帮助我。在过去的一周里,我一直在研究我的一个项目。显然,我坚持最后一部分。基本上,我有一个AJAX聊天,当我提交一行时,我发送(使用Post方法)整行进行分析(到一个名为analysis.php的文件)。正在分析聊天行,并通过在MySql数据库上进行查询来查找我需要的变量。我现在需要的是将这个变量与JQuery-AJAX一起使用并将其放在我的html文件中的div上(因此它可以显示在左右 - 聊天的任何内容中)。

Here are my files :
analysis.php

这是我的文件:analysis.php

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

ajax-chat.html

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>

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.

所以,我基本上试图从analyze.php文件中获取$ advert(在完成整个分析之后),并使用JQuery / AJAX将其最终传递给ajax-chat.html文件。任何帮助都非常感谢。我用谷歌搜索了一切,但没有找到帮助我的东西。提前致谢。

1 个解决方案

#1


31  

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

如果我理解正确,您需要使用JSON。这是一个样本。

In your PHP write:

在你的PHP写中:

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

Then, if you are using jQuery, just write:

然后,如果您正在使用jQuery,只需写:

    $.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");
        }
    })

And that's all. This is JSON - it's used to send variables, arrays, objects etc between server and user. More info here: http://www.json.org/. :)

就这样。这是JSON - 它用于在服务器和用户之间发送变量,数组,对象等。更多信息:http://www.json.org/。 :)

#1


31  

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

如果我理解正确,您需要使用JSON。这是一个样本。

In your PHP write:

在你的PHP写中:

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

Then, if you are using jQuery, just write:

然后,如果您正在使用jQuery,只需写:

    $.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");
        }
    })

And that's all. This is JSON - it's used to send variables, arrays, objects etc between server and user. More info here: http://www.json.org/. :)

就这样。这是JSON - 它用于在服务器和用户之间发送变量,数组,对象等。更多信息:http://www.json.org/。 :)