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>
";
1.- Seems that you really don't need print
all that js code. This will work the same:
<?php
// php code
?>
<script type="text/javascript" src="modules/Forum/jquery.js"></script>
<script type="text/javascript">
$(function() {
// ...
});
</script>
<?php
// php code
?>
If you need satisfy some condition to write/run this code do it like this:
<?php
// php code
if ($condition) : // start js code
?>
<script type="text/javascript" src="modules/Forum/jquery.js"></script>
<script type="text/javascript">
$(function() {
// ...
});
</script>
<?php
endif; // end js code
// php code
?>
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.
$.ajax({
type: "POST",
url: "<?php echo $path_to_your_modules; ?>/modules/Forum/down_vote.php",
data: dataString,
cache: false,
dataType : "html", // add this to format as html
success: function(html){
parent.html(html);
}
});