简体   繁体   English

将数据从Mysql输出到js / HTML

[英]outputting data From Mysql to js/HTML

So Basically this is what i want to do , I want to take data from my database and then put that data on a page after the user logs in , however i am havinng problems understanding a way , So here's some sample code for clarity : 所以基本上这就是我想要做的,我想从数据库中获取数据,然后在用户登录后将这些数据放在页面上,但是我在理解一种方式时遇到了问题,因此,为了清楚起见,下面是一些示例代码:

//user has logged in:
//fetched data from database to php file
$name = $result['name'] //result being the object after the queried row is fetched

now i want to add this name in lets say a div in my HTML 现在我想在HTML中说一个div来添加这个名字

<div id="name"><!--$name comes here--></div>

So what is the way to do this using JS ? 那么,使用JS执行此操作的方法是什么? is AJAX the answer? AJAX是答案吗?

I guess that you have $name in the login.php file and the div is in profile.php. 我猜你在login.php文件中有$name ,而div在profile.php中。 You can use session variables or ajax: 您可以使用会话变量或ajax:

By php: login.php 通过php:login.php

<?php
session_start();
$_SESSION["name"] = $result['name'] 
?>

profile.php profile.php

<?php
session_start(); 
?>
<div id="name"><?php echo $_SESSION["name"]; ?></div>

By ajax: 通过ajax:

getName.php getName.php

$name = $result['name'];
echo $name;

profile.js profile.js

$.get("getName.php", function(data, status){
    $("#name").append(data);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM