Php 使用MySQL会产生javascript
我有一个web应用程序,需要从MySQL数据库中获取值 活动系列如下:Php 使用MySQL会产生javascript,php,jquery,html,mysql,ajax,Php,Jquery,Html,Mysql,Ajax,我有一个web应用程序,需要从MySQL数据库中获取值 活动系列如下: function myfunction($cat) { $db = mysqli_connect('localhost','myuser','mypass','dbname'); $stmt = $db->prepare("SELECT question, answer FROM t_da_questions WHERE category_id=?;"); $stmt->bind_par
function myfunction($cat) {
$db = mysqli_connect('localhost','myuser','mypass','dbname');
$stmt = $db->prepare("SELECT question, answer FROM t_da_questions WHERE category_id=?;");
$stmt->bind_param("s", $cat);
$stmt->execute();
$retval = false;
if($result->num_rows > 0) {
$row = $result->fetch_assoc();
if(!is_null($row['question']) && !is_null($row['answer'])) {
$retval = new stdClass();
$retval->question = row['question'];
$retval->answer = row['answer'];
}
}
mysqli_close($db);
return $retval;
}
<div id="popup" class="popup" data-popup="popup-1">
<div class="popup-inner">
<h2 id="popup-headline"></h2> //Headline, created from a cookie. Could be "Geography"
<div id="dialog"></div> //From Will's suggestion
<p id="question"></p> //String 1 from online MySQL DB goes here "A question in Geography"
<p id="answer"></p> //String 2 from online MySQL DB goes here "The answer to the question"
<p class="popup-small-button"><a data-popup-close="popup-1" href="#"><br>Close</a></p> // Hides the popup
<a class="popup-close" data-popup-close="popup-1" href="#">x</a>
</div>
</div>
//标题,由cookie创建。可能是“地理”
//根据威尔的建议
//在线MySQL数据库中的字符串1出现在“地理问题”中
//在线MySQL数据库中的字符串2出现在这里“问题的答案”
//隐藏弹出窗口
然后我就有了带有自定义函数的文件。无论何时显示弹出窗口,它都会执行:
<script>
jQuery(function() {
jQuery('[data-popup-open]').on('click', function(e) {
function myfunction(myparams) {
// your logic here: testing myparams for valid submission, etc.
alert("hey");
jQuery.ajax({
type: 'post',
url: 'server.php',
data: {
my_var1: 'question',
my_var2: 'answer'
},
success: function(data) {
data = JSON.parse(data);
jQuery('#question').html(data["question"]);
jQuery('#answer').html(data["answer"]);
},
error: function(jqxhr, status, exception) {
alert('Exception:', exception);
}
});
}
});
});
</script>
jQuery(函数(){
jQuery(“[data popup open]”)。在('click',函数(e)上{
函数myfunction(myparams){
//这里的逻辑是:测试myparams是否有效提交,等等。
警惕(“嘿”);
jQuery.ajax({
键入:“post”,
url:'server.php',
数据:{
我的“问题”,
我的_var2:'答案'
},
成功:功能(数据){
data=JSON.parse(数据);
jQuery(“#问题”).html(数据[“问题]);
jQuery('#answer').html(数据[“answer”]);
},
错误:函数(jqxhr、状态、异常){
警报(“异常:”,异常);
}
});
}
});
});
My server.php文件现在包含以下内容:
<?php
require("db.php");
if(isset($_POST['my_var1']) && isset($_POST['my_var2'])) {
myfunction($_POST['my_var1'], $_POST['my_var2']);
}
?>
my db.php包含以下内容:
<?php
function myfunction($var1, $var2) {
$db = mysqli_connect('MyOnlineSQLPath','username','password','database1_db_dk');
$stmt = $db->prepare("SELECT question, answer FROM t_da_questions WHERE category_id=?;");
$stmt->bind_param("s", $_COOKIE('category'));
$stmt->execute();
$retval = false;
if($result->num_rows > 0) {
$row = $result->fetch_assoc();
if(!is_null($row['question']) && !is_null($row['answer'])) {
$retval = new stdClass();
$retval->question = row['question'];
$retval->answer = row['answer'];
}
}
mysqli_close($db);
return $retval;
}
我需要的是SELECT查询中的“问题”和“答案”
TL;DR I需要问答字符串进入HTML中的
和
,而无需刷新页面。getCookie('category')
是本地存储的cookie,它包含最后选择的问题类别。函数getCookie('category')
返回一个整数
如果您需要更多信息,请告诉我。这里有一些AJAX模板,可能会对您有所帮助。我在另一个项目中使用了这个。这不需要刷新页面。您必须在“数据”部分包含发送cookie数据的代码
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<meta charset="utf-8">
</head>
<body>
// your HTML here
<script>
<div id="dialog"></div>
function myfunction(myparams) {
// your logic here: testing myparams for valid submission, etc.
$.ajax({
type: 'post',
url: 'myphpfile.php',
data: {
my_var1: 'myval',
my_var2: 'myval2'
},
success: function(data) {
$("#dialog").html("<span>Success!</span>");
$("#dialog").fadeIn(400).delay(800).fadeOut(400);
}
});
}
</script>
</body>
</html>
更新
上面的函数是运行更新查询,因此返回的结果只是指示您是否成功更新了数据。如果要返回实际结果,必须按如下方式从查询中提取结果:
function myfunction($cat) {
$db = mysqli_connect('localhost','myuser','mypass','dbname');
$stmt = $db->prepare("SELECT question, answer FROM t_da_questions WHERE category_id=?;");
$stmt->bind_param("s", $cat);
$stmt->execute();
$retval = false;
if($result->num_rows > 0) {
$row = $result->fetch_assoc();
if(!is_null($row['question']) && !is_null($row['answer'])) {
$retval = new stdClass();
$retval->question = row['question'];
$retval->answer = row['answer'];
}
}
mysqli_close($db);
return $retval;
}
然后,您的server.php文件将如下所示:
<?php
require("db.php");
if(isset($_COOKIE['category'])) {
json_encode(myfunction($_COOKIE['category']));
}
?>
这是未经测试的——我可能得到了一个错误的论点,但如果它还没有出现,这至少是非常接近的。这似乎是最新的(但你仍然必须理解,你在做什么):你尝试过AJAX吗?您的案例可以通过非常简单的AJAX应用程序完成。找到一个简单的示例,复制并尝试,然后将其应用到您的应用程序中。您需要发布一些您迄今为止尝试过的代码。@EmilOlsen:是的,它说的是相同的事情。“我做了一些事情,但它没有做我想做的。我不会告诉你我做了什么,我想做什么,或者事情做了什么。”哦,你做了什么很重要。。。所以我们知道你所做的是否应该有效,但你还有其他一些与之相冲突的问题。。。或者知道你甚至没有做你应该做的第一件事。这里没有人喜欢猜谜或读心术;)不要与其他人争论,要求澄清。。。就像是给他们中指。饼干不是自动发送的吗?这就是它们存在的全部理由:)Cookies存储在用户的浏览器中,当页面加载时,站点可以访问它们。然而,听起来用户正在谈论在他们自己的数据库中使用cookie数据,这不会自动发生。我的意思是,cookie将与ajax请求一起发送,而无需用户方面的任何努力(与“常规”页面加载相同)。然后他可以在服务器端自由地使用(或忽略)它们。所有这些都很好——当我想在PHP MySQL数据库连接中使用它时,问题就出现了——因为PHP是服务器端的,我不知道如何解决它……而且,如果cookie被创建为httponly,您将无法在javascript中访问它,以显式地将其用作数据my_var1等。。。(但它将在ajax请求头中发送它们,并在php端提供$\u COOKIE['mycookie1']
)。所以这里不需要将它们作为POST-args传递(除非是跨域的情况,但现在这已经不是问题了lol)。
<?php
require("db.php");
if(isset($_COOKIE['category'])) {
json_encode(myfunction($_COOKIE['category']));
}
?>
jQuery('[data-popup-open]').on('click', function(e) {
function myfunction(myparams) {
// your logic here: testing myparams for valid submission, etc.
alert("hey");
jQuery.ajax({
type: 'post',
url: 'server.php',
// data section not needed (I think), getting it from the cookie
success: function(data) {
data = JSON.parse(data);
jQuery('#question').html(data["question"]);
jQuery('#answer').html(data["answer"]);
}
});
}
});