gpt4 book ai didi

javascript - 使用ajax将jquery变量插入到php中?

转载 作者:行者123 更新时间:2023-11-29 11:57:47 24 4
gpt4 key购买 nike

我执行 mysql 查询 SELECT * FROM table WHERE mp3link = '$songname' 我希望变量 $songname = 一个名为 'key' 的 jquery 变量,该变量根据播放的歌曲而变化。到目前为止,当我设置 $songname ='Illuminati.mp3' 时,一切正常,它将在文本中获取并回显“Illuminati”。但是当我尝试退出 php 并获取 jquery 变量时,它不起作用。我读到 PHP 是服务器端,Jquery 是客户端,所以这可能就是为什么?但后来我听说一种叫做 AJAX 的语言也许可以做到这一点?

 $(".blocksong").on('click', function () {
var key = $(this).attr('key');
EvalSound(this, key);
var this_play = $(this);
$(".blocksong").each(function () {
if ($(this)[0] != this_play[0]) {
$(this).removeClass("blockpause");
}
});
$(this).toggleClass("blockpause");
$(".nowplaying").text("<?php
$songname = " key "; // how do I insert var key here?
$nowplay=mysql_fetch_assoc(mysql_query("SELECT * FROM `soundplum` WHERE mp3link = '$songname'"));
echo $nowplay['name'];

?>");

最佳答案

在我发表所有评论之后,给您一个答案,希望对您有所帮助:

您想要执行一个(客户端)JavaScript 函数来从服务器/数据库获取数据。为此,您需要 AJAX 功能。AJAX 函数调用服务器(在您的例子中它将调用 php 脚本)并等待答案。一旦得到答案,它就会执行您指定的另一个函数。看看这里: http://www.w3schools.com/ajax/

既然您已经使用了 jQuery,也可以看看 jQuery 的相同版本: http://api.jquery.com/jquery.ajax/

因此,您可以执行以下操作(jQuery 版本,教程的副本):

// in your html / script
// after $(this).toggleClass("blockpause");
$.ajax({
url: "getsongname.php?songname=" + key,
context: document.body
}).done(function(response) {
$(".nowplaying").text(response);
});

其中有一个名为 getsongname.php 的 php 脚本,其中包含如下内容:

<?php
//...something before that to establish db-connection and stuff...

$songname = $_POST['songname'];

// this is a copy of your code!! Please don't use msql_... anymore!!
$nowplay=mysql_fetch_assoc(mysql_query("SELECT * FROM `soundplum` WHERE mp3link = '$songname'"));
echo $nowplay['name'];
?>

注意,1.不要再使用mysql了。2. 您在 php 中回显的内容将是您在 AJAx 调用中的响应!

希望这会有所帮助。

关于javascript - 使用ajax将jquery变量插入到php中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32929272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com