PHP代码中插入并调用JS
我使用旧的 CMS 和我想要插入投票 +1 或 - 1 系统的模块之一。 问题是该模块是用 PHP 编写的,无法调用 JS。也许我做错了什么。
我尝试更改路径 - 这是无用的...直接调用脚本“modules/Forum/down_vote.php”受保护的服务器。
初始化 jQuery,我需要 PHP 中的一个函数:
print ("<script type=\"text/javascript\" src=\"modules/Forum/jquery.js\"></script>
<script type=\"text/javascript\">
$(function() {
$('.vote').click(function() {
var id = $(this).attr(\"id\");
var name = $(this).attr(\"name\");
var dataString = 'id='+ id ;
var parent = $(this);
if(name=='down')
{
$(this).fadeIn(200).html('<img src=\"modules/Forum/dot.gif\" align=\"absmiddle\">');
$.ajax({type: \"POST\", url: \"modules/Forum/down_vote.php\", data: dataString, cache: false, success: function(html)
{ parent.html(html);}
});
}
else
{
$(this).fadeIn(200).html('<img src=\"modules/Forum/dot.gif\" align=\"absmiddle\">');
$.ajax({type: \"POST\", url: \"modules/Forum/up_vote.php\", data: dataString, cache: false, success: function(html)
{ parent.html(html);
} });
}
return false;
});
});
</script>");
投票按钮:
echo "<div class=\"box1\"><div class=\"up\"><a href=\"#\" class=\"vote\" title=\"+ 1\" alt=\"+ 1\" id=".$row["id"]." name=\"up\">".$up."</a></div>"
."<div class=\"down\"><a href=\"#\" class=\"vote\" title=\"- 1\" alt=\"- 1\" id=".$row["id"]." name=\"down\">".$down."</a></div></div>\n";
I use old CMS and one of the modules that I want to insert a system of voting +1 or - 1.
The problem is that the module is written in PHP and does not work call the JS. Maybe I'm doing something wrong.
I tried to change the path - it is useless ... Direct call script "modules/Forum/down_vote.php" protected server.
Initialize jQuery and I need a function in PHP:
print ("<script type=\"text/javascript\" src=\"modules/Forum/jquery.js\"></script>
<script type=\"text/javascript\">
$(function() {
$('.vote').click(function() {
var id = $(this).attr(\"id\");
var name = $(this).attr(\"name\");
var dataString = 'id='+ id ;
var parent = $(this);
if(name=='down')
{
$(this).fadeIn(200).html('<img src=\"modules/Forum/dot.gif\" align=\"absmiddle\">');
$.ajax({type: \"POST\", url: \"modules/Forum/down_vote.php\", data: dataString, cache: false, success: function(html)
{ parent.html(html);}
});
}
else
{
$(this).fadeIn(200).html('<img src=\"modules/Forum/dot.gif\" align=\"absmiddle\">');
$.ajax({type: \"POST\", url: \"modules/Forum/up_vote.php\", data: dataString, cache: false, success: function(html)
{ parent.html(html);
} });
}
return false;
});
});
</script>");
Vote buttons:
echo "<div class=\"box1\"><div class=\"up\"><a href=\"#\" class=\"vote\" title=\"+ 1\" alt=\"+ 1\" id=".$row["id"]." name=\"up\">".$up."</a></div>"
."<div class=\"down\"><a href=\"#\" class=\"vote\" title=\"- 1\" alt=\"- 1\" id=".$row["id"]." name=\"down\">".$down."</a></div></div>\n";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1.- 看来你真的不需要
打印
所有的js代码。这将工作相同:如果您需要满足某些条件来编写/运行此代码,请执行以下操作:
2.- 显然您的 jquery 代码是正确的。确保您的 CMS 提供了获取网站 URL 的方法。类似 codeigniter 或 wordpress 中的
site_url()
。我建议您使用它来确定 AJAX 调用中的 $path_to_your_modules 。1.- Seems that you really don't need
print
all that js code. This will work the same:If you need satisfy some condition to write/run this code do it like this:
2.- Apparently your jquery code is right. Sure that your CMS provides a way to get the URL of your site. Something like
site_url()
in codeigniter or wordpress. I suggest you use it to determine $path_to_your_modules in your AJAX call.