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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10341434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:58:02  来源:igfitidea点击:

Get variable from PHP file using JQuery/AJAX

phpjqueryhtmlajax

提问by harris21

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.

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

回答by suricactus

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/。:)