Jquery Function is saying not defined when running a php script

前端 未结 3 1403
悲&欢浪女
悲&欢浪女 2021-01-25 02:12

I am essentially making it so when you click on a button to \"vote up\"

at the moment i have

clients.php



        
相关标签:
3条回答
  • 2021-01-25 02:17

    Why do I get an error in the console saying "ReferenceError: voteUp is not defined"

    Scope.

    The voteUp() and voteDown are defined in the startup block (within the {} of $(document).ready(function() ... so are not available outside this block.

    You need to move the two functions outside:

    $(document).ready(function() {
        ...
    });
    
    function voteUp()
    {
        $.get("voteup.php");
        return false;
    }
    
    function voteDown()
    {
        $.get("votedown.php");
        return false;
    }
    
    0 讨论(0)
  • 2021-01-25 02:24

    Why not use jQuery instead of inline JS? Give your div's classes:

    <div class="voteup" data-clientid="{$client['ClientID']}">...</div> 
    

    Then use jQuery instead of onclick() as it would be much cleaner:

    $(document).on('click', '.voteup', function(){
        var clientid = $(this).data('clientid');
        $.get("voteup.php", { clientid: clientid });
        return false;
    })
    

    Since you're already using jQuery this will help you to keep your markup cleaner, giving you the freedom to make changes much more easily to your markup and your JavaScript.

    In addition, this will remove scoping issues so that you do not have to add namespaces.

    The only other problem I see is that you're not passing $_POST['clientid'] to voteup.php, so your query will never run. You can include data-client-id in your div declaration but you will have to change your test in your PHP to be:

    if (isset($_GET['clientid'])) 
    

    As you would be sending the value along wwith a GET request.

    0 讨论(0)
  • 2021-01-25 02:30

    You have scoped out the methods.

    Instead you can attach them to windows to be accessible from any place.

    window.voteUp  = function()
    {
        $.get("voteup.php");
        return false;
    }
    
    window.voteDown = function()
    {
        $.get("votedown.php");
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题