简体   繁体   English

PHP:如何基于值从Java语言中的SQL获取数据?

[英]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 ,我想使用该按钮基于从元素中获得的idSQL中header_table中从名为header_name列中获取数据

This is the code 这是代码

View.php View.php

<?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

upload_header.js upload_header.js

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获取数据。

How do I can get data from SQL through javascript? 如何通过javascript从SQL获取数据?

Thanks, any help will be appreciated :) 谢谢,任何帮助将不胜感激:)

Note: 注意:

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.

相关问题 如何将我的脚本值从javascript显示到php - How do I show my script value from javascript to php 如何从本地存储中获取特定数据,以便在 JavaScript 中以模态显示它们? - How do I get specific data from my localstorage so I can show them in a modal in JavaScript? 如何通过PHP从客户端向服务器获取javascript整理的数据 - How can I get my javascript gererated data from the client to the server via PHP 如何从 javascript 获得价值 - How do I get value from javascript 如何合并我的代码? 基于JavaScript和PHP的问题 - How can I consolidate my code? JavaScript and PHP based issues 如何根据URL数据显示数据? - How do I get my data to display based on URL data? 如何基于v-for值在模态窗口(单击按钮)上获取所选数据? - How can I get the selected data on my modal window(on button click) based on the v-for value? 如何将数据从文本文件读取到变量中,以便可以根据值更改颜色? - How do I read the data from text file into a variable so I can change the color based on the value? 我需要将变量的值从 javascript 传递给 php 我该怎么做? - I need to pass the value of the variable from javascript to php how can I do it? 在 Rails 中,如何从 JavaScript 查询我的数据库以便保存查询值? - In Rails, how do I query my database from JavaScript so I can save the value of the query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM