简体   繁体   中英

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

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

So what is the way to do this using JS ? is AJAX the answer?

I guess that you have $name in the login.php file and the div is in profile.php. You can use session variables or ajax:

By php: login.php

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

profile.php

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

By ajax:

getName.php

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

profile.js

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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