• I need a button that calls a php code, the button is written in the single.php file, so I’m trying it with AJAX post function, but i don’t know what URL should i put to call theme files, since the Theme API is php, not javascript.

    I was trying to use window.location.href, dumb move, yeah, the single.php file isn’t loaded directly.

    Here is my current code

    HTML:

    <button type="button" onclick="return likeTrigger();" class="btn btn-info btn-lg pulse" style="color: #000;">
    	<div id="likesCount" style="display: inline;"><?php echo getLikes(get_the_ID()); ?></div>
    	<span class="glyphicon glyphicon-thumbs-up" aria-hidden="true"></span>
    </button>
    <button type="button" onclick="return unlikeTrigger();" class="btn btn-danger btn-lg pulse" style="color: #000;">
    	<div id="unlikesCount" style="display: inline;"><?php echo getUnlikes(get_the_ID()); ?></div>
    	<span class="glyphicon glyphicon-thumbs-down" aria-hidden="true"></span>
    </button>

    JS:

    function likeTrigger(){
        $.post(
            window.location.href,
    		{event: "like", csIP: getIP()},
            function (response) {
                $('#likesCount').html(response.responseText);
            }
    	);
        return false;
    }
    function unlikeTrigger(){
        $.post(
            window.location.href,
    		{event: "unlike", csIP: getIP()},
            function (response) {
                $('#unlikesCount').html(response.responseText);
            }
    	);
        return false;
    }

    php(just a test, not the actual code)

    if($_POST["event"] == "like" or $_POST["event"] == "unlike")
    {
    	echo "hello";
    }

    I already tested the following stuff

    1. Button is triggering
    2. Like and unlike trigger functions are being called
    3. PHP file isn’t being accessed

    Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘what url should i use on AJAX to execute a specific file?’ is closed to new replies.