[英]PHP: How do I can get data from SQL in my Javascript based on a value?
I've an application, developed using Yii2 framework. 我有一个使用Yii2框架开发的应用程序。 In my application, I've a view.php that has an element and a button .
在我的应用程序中,我有一个view.php,其中包含一个元素和一个button 。 The element
<div id="userId">
contain the id
of login user, and I want to use the button to get data from column that called header_name
from header_table
in my SQL based on the id
that has got from the element . 元素
<div id="userId">
包含登录用户的id
,我想使用该按钮基于从元素中获得的id
从SQL中的header_table
中从名为header_name
列中获取数据 。
This is the code 这是代码
<?php
//some code
?>
<div id="userId" style="display: none"> <?php echo $userId; ?> </div>
<p>
<button type="button" onclick="getHeader()"class="headerBtn">Use Saved Header</button>
<p>
and 和
function getHeader(){
var id = document.getElementById('userId').textContent;
}
I've successfully get the $userid
from view.php in my upload_header.js. 我已经从我的upload_header.js中的view.php中成功获取了
$userid
。 But I dont't know how to get the data from sql. 但是我不知道如何从sql获取数据。
Thanks, any help will be appreciated :) 谢谢,任何帮助将不胜感激:)
I've read How to get data from database in javascript based on the value passed to the function and try the suggestion, but it didn't solve my case. 我已经阅读了如何基于传递给函数的值从javascript中的数据库中获取数据并尝试了建议,但是它并不能解决我的问题。 Thanks
谢谢
Use AJAX in getHeader function. 在getHeader函数中使用AJAX。
ie 即
$.ajax({url: "server_page.php?userId="+id, success: function(result){
//Your Result here
}});
You can return data in JSON format from server page and parse in ajax success area. 您可以从服务器页面以JSON格式返回数据,并在ajax成功区域中进行解析。
You can use ajax for this 您可以为此使用ajax
upload_header.js upload_header.js
function getHeader(){
var id = document.getElementById('userId').textContent;
$.ajax({
type:"POST", // or GET
url:"<?= Url::to(['controller-name/function-name']) ?>",
data:{id:id},
success:function(response){
console.log(response); //your data from sql
}
});
}
on_your_contoller.php on_your_contoller.php
public function function-name(){
$id = $POST['id'];
return User::findOne($id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.