从 mysql 数据库中获取数据以在 javascript 中使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5568965/
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-23 17:51:07  来源:igfitidea点击:

get data from mysql database to use in javascript

javascriptmysqldatabaseajax

提问by user1892770

I have a javascript that dynamically builds an html page. In the html page there are textarea boxes for the user to type information in. The information already exists in a database. I would like to populate the textarea boxes with the database in the mysql database. I have php code that will connect to the database and build an html table with the data, so I know how to do this with php, but I don't know how to do this from the javascrip. I've studied ajax get requests, etc., but I'm still not sure of how to do this.

我有一个动态构建 html 页面的 javascript。在 html 页面中有 textarea 框供用户输入信息。该信息已存在于数据库中。我想用 mysql 数据库中的数据库填充 textarea 框。我有 php 代码可以连接到数据库并用数据构建一个 html 表,所以我知道如何用 php 做到这一点,但我不知道如何从 javascrip 做到这一点。我研究了 ajax get requests 等,但我仍然不确定如何做到这一点。

回答by wlangstroth

Probably the easiest way to do it is to have a php file return JSON. So let's say you have a file query.php,

可能最简单的方法是让一个 php 文件返回 JSON。所以假设你有一个文件query.php

$result = mysql_query("SELECT field_name, field_value
                       FROM the_table");
$to_encode = array();
while($row = mysql_fetch_assoc($result)) {
  $to_encode[] = $row;
}
echo json_encode($to_encode);

If you're constrained to using document.write (as you note in the comments below) then give your fields an id attribute like so: <input type="text" id="field1" />. You can reference that field with this jQuery: $("#field1").val().

如果你要限制使用document.write(如你在下面的评论请注意),然后给你的领域的id属性,像这样:<input type="text" id="field1" />。您可以参考该领域与这个jQuery: $("#field1").val()

Here's a complete example with the HTML. If we're assuming your fields are called field1and field2, then

这是一个完整的 HTML 示例。如果我们假设您的字段被称为field1and field2,那么

<!DOCTYPE html>
<html>
  <head>
    <title>That's about it</title>
  </head>
  <body>
    <form>
      <input type="text" id="field1" />
      <input type="text" id="field2" />
    </form>
  </body>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
  <script>
    $.getJSON('data.php', function(data) {
      $.each(data, function(fieldName, fieldValue) {
        $("#" + fieldName).val(fieldValue);
      });
    });
  </script>
</html>

That's insertion after the HTML has been constructed, which might be easiest. If you mean to populate data while you're dynamically constructing the HTML, then you'd still want the PHP file to return JSON, you would just add it directly into the valueattribute.

这是在构造 HTML 之后插入,这可能是最简单的。如果您打算在动态构建 HTML 时填充数据,那么您仍然希望 PHP 文件返回 JSON,您只需将其直接添加到value属性中即可。

回答by Josh

To do with javascript you could do something like this:

要使用 javascript,您可以执行以下操作:

<script type="Text/javascript">
var text = <?= $text_from_db; ?>
</script>

Then you can use whatever you want in your javascript to put the text var into the textbox.

然后,您可以在 javascript 中使用任何您想要的内容将文本变量放入文本框。

回答by Matt

Do you really need to "build" it from javascript or can you simply return the built HTML from PHP and insert it into the DOM?

您真的需要从 javascript 中“构建”它,还是可以简单地从 PHP 返回构建的 HTML 并将其插入到 DOM 中?

  1. Send AJAX request to php script
  2. PHP script processes request and builds table
  3. PHP script sends response back to JS in form of encoded HTML
  4. JS takes response and inserts it into the DOM
  1. 将 AJAX 请求发送到 php 脚本
  2. PHP 脚本处理请求并建表
  3. PHP 脚本以编码的 HTML 的形式将响应发送回 JS
  4. JS 获取响应并将其插入到 DOM 中

回答by Mike Baranczak

You can'tdo it with only Javascript. You'll need some server-side code (PHP, in your case) that serves as a proxy between the DB and the client-side code.

不能只用 Javascript做到这一点。您将需要一些服务器端代码(在您的情况下为 PHP)作为数据库和客户端代码之间的代理。