0

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";
3
  • 4
    Damn. Please use HEREDOC or normal inline-html next time! Commented Dec 4, 2011 at 13:42
  • This is not HTML, so I'm trying initiate JS in PHP. Therefore i post such code. Maybe this will help to find my mistake. Commented Dec 4, 2011 at 13:45
  • 2
    PHP does not call JavaScript functions. It generates code on the server side that is sent to the browser which in turn might call the javascript on the client side. When you are not aware of the different layers of execution, you should work yourself through this. Commented Dec 4, 2011 at 14:00

1 Answer 1

2

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);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.