calling OOP PHP function via AJAX

Nirgan

I have OOP php file and I would like to call one of the function by AJAX, i have read a lot about this (mostly stackoverflow), but for some reason, it just doesnt work.

I know that ajax function is called (I tried to add some alert into success function and that alert appeared after clicking the button), but somehow it ignores everything from file ajax.php (i even tried to add some echo at the top of ajax.php file, but nothing happened)

also note that I need button, not input (although that would be much easier)

this is my button:

<button class='formular-button' type='button' onclick='prihlasPA()'> vypiš přihlaš </button>

this is my script for button:

<script>
function prihlasPA() {
$.ajax({
    type: "POST",
    url: "ajax.php",
    data: {action: 'prihlasP'},
    success: function(){},
    error: function(){
        alert("chyba");
    }
  });    
}
</script>

and this is inside ajax.php, which is called by AJAX:

include( 'navstevnik.php' );
echo "aaa";
if(isset($_POST['action']) ) {
    $navstevnik = new navstevnik; //navstevnik is name of my class btw
    $navstevnik->vypisPrihlas();
} 

I dont think it matters, but in my function vypisPrihlas() is this code:

public function vypisPrihlas(){
    $this->index=1;
    echo ' 
        <fieldset class="formular">
        <div class="pure-control-group stred" >
            <input id="nick" type="text" name="nickP" placeholder="Nickname">
        </div>
        <div class="pure-control-group stred">
            <input id="password" type="password" name="hesloP" placeholder="Heslo">
        </div>
        <input class="pure-button pure-button-primary stred" style=width:188px; type="submit" name="buttonP" value="přihlásit" />
    </fieldset>'; 
}
Dave Chen

I don't see any issues with the above code.

When the function vypisPrihlas echos the HTML, you won't see it on your main page. Instead, you need to load it in from your success function and append the HTML somewhere on your page.

$.ajax({
    type: "POST",
    url: "ajax.php",
    data: {action: 'prihlasP'},
    success: function(data){
        $( ".your_favorite_div" ).append(data);
    },
    error: function(){
        alert("chyba");
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a JS function via PHP

From Dev

calling a phalcon php MVC controller via ajax

From Dev

Calling static function returns NULL if object is not instanced? PHP OOP

From Dev

Calling static function returns NULL if object is not instanced? PHP OOP

From Dev

Calling Wordpress PHP function from AJAX

From Dev

Calling a specific function from PHP with Jquery & Ajax

From Dev

Ajax calling multiple php files in same function

From Dev

Calling a php function in a separate file using AJAX

From Dev

Removing a WordPress filter by calling a PHP function with Ajax

From Dev

Wordpress ajax is not calling function in functions.php

From Dev

Calling PHP Web service via ajax in jQuery mobile

From Dev

Call a php function via Ajax Request

From Dev

Executing a PHP function via JQuery ajax

From Dev

Calling function via pointer

From Dev

Calling function via pointer

From Dev

ajax is calling the error function

From Dev

Ajax function not calling

From Dev

Calling Php Function With Ajax Request to change session variable

From Dev

Calling php function with ajax and passing return value to div

From Dev

AJAX not calling PHP file

From Dev

Variable in jQuery dialog to be passed for use in php function via ajax

From Dev

Getting 500 error when trying to call PHP function via AJAX

From Dev

Calling a Javascript function via ssl

From Dev

Passing arguments to a function and calling it via "&"

From Dev

How to make php/ajax req into OOP

From Dev

Checking OOP PHP $_SESSION with Jquery Ajax

From Dev

OOP php and ajax login wrapper response

From Dev

Calling an Oracle function via Ajax for on-the-spot validation purposes in Oracle APEX v4.2.2

From Dev

Rails form submit via ajax post not calling success or error callback function

Related Related

  1. 1

    Calling a JS function via PHP

  2. 2

    calling a phalcon php MVC controller via ajax

  3. 3

    Calling static function returns NULL if object is not instanced? PHP OOP

  4. 4

    Calling static function returns NULL if object is not instanced? PHP OOP

  5. 5

    Calling Wordpress PHP function from AJAX

  6. 6

    Calling a specific function from PHP with Jquery & Ajax

  7. 7

    Ajax calling multiple php files in same function

  8. 8

    Calling a php function in a separate file using AJAX

  9. 9

    Removing a WordPress filter by calling a PHP function with Ajax

  10. 10

    Wordpress ajax is not calling function in functions.php

  11. 11

    Calling PHP Web service via ajax in jQuery mobile

  12. 12

    Call a php function via Ajax Request

  13. 13

    Executing a PHP function via JQuery ajax

  14. 14

    Calling function via pointer

  15. 15

    Calling function via pointer

  16. 16

    ajax is calling the error function

  17. 17

    Ajax function not calling

  18. 18

    Calling Php Function With Ajax Request to change session variable

  19. 19

    Calling php function with ajax and passing return value to div

  20. 20

    AJAX not calling PHP file

  21. 21

    Variable in jQuery dialog to be passed for use in php function via ajax

  22. 22

    Getting 500 error when trying to call PHP function via AJAX

  23. 23

    Calling a Javascript function via ssl

  24. 24

    Passing arguments to a function and calling it via "&"

  25. 25

    How to make php/ajax req into OOP

  26. 26

    Checking OOP PHP $_SESSION with Jquery Ajax

  27. 27

    OOP php and ajax login wrapper response

  28. 28

    Calling an Oracle function via Ajax for on-the-spot validation purposes in Oracle APEX v4.2.2

  29. 29

    Rails form submit via ajax post not calling success or error callback function

HotTag

Archive