Using jQuery Ajax to call a php function

First of all, thanks for checking out my problem and for any help you can give!

Ok, as the name says, I need to call the php function from my index page, which adds a new record to my database as a vote using jQuery Ajax. This function will return a single integer, which will then be printed inside the button of the form in which it was called.

Does anyone have an idea on how I will do this? Any guidance appreciated!

Edit:

So if I'm using a template object I can just call the
function using ajax and it will return the output? 
What would I use as the URL? index.php?  

Such as...
function DoVote() {
    $.ajax({
    type:'POST',
    url: 'index.php',
    success: function(data) {
    $('#voteText').html(data);
 }
});

And the action action attribute is hosted, I guess?

+3
source share
2 answers

, PHP , . php AJAX.

PHP - , jQuery (JavaScript) - , PHP , . , - <?php echo($object->function()); ?>, , .

, ( ) .

PHP:

<?php
    // Called "otherfile.php"

    // Function you're trying to call
    function doSomething($obj)
    {
        $ret = $obj + 5;

        return($ret);
    }
?>

( ajaxcall.php), AJAX.

<?php
    include("otherfile.php");   // Make the file available, be aware that if that file 
                                // outputs anything (i.e. not wrapped in a function) it
                                // may be executed
    // Grab the POST data from the AJAX request
    $obj = $_POST['obj']; 

    echo($doSomething());
?>
+3

ajax URL- , php. ( GET) , .

0

Source: https://habr.com/ru/post/1745613/


All Articles