Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing jQuery to call PHP function
    primarykey
    data
    text
    <p>I've been doing some research on calling a PHP function within a file from jQuery and can't seem to find a proper way of doing this without it failing on me. I'm trying to create an intermediary (kind of) CMS with MVVM to create a single-page website with a WordPress base. </p> <p>At the moment I'm stuck on a bit where I'm trying to access PHP functions in allocated files, I've seen examples <a href="https://stackoverflow.com/questions/14600989/ajax-in-wordpress-not-calling-php-function">here</a>, <a href="https://stackoverflow.com/questions/8487172/wordpress-jquery-how-to-call-a-wordpress-function-with-jquery-on-click-event">here</a> and <a href="https://stackoverflow.com/questions/15757750/php-function-call-using-javascript/15757864#15757864">here</a> on how to do it with a function per file, but I'd like to be able to call a function in a file as it gives me a clearer overview. </p> <p>I've checked the link and it's correct, the PHP file is called <code>posts.php</code> and its in the location defined by <code>window.MODELS</code>, however the function <code>fetch_all</code> doesn't get executed. </p> <p>JavaScript</p> <pre><code>//call php function var requestURL = window.MODELS + 'posts/fetch_all'; $.get(requestURL, function(data){ console.log('checking data: ', data); }, 'json') .done(function(passed){ console.log('it worked!'); }) .fail(function(failed){ console.log('it failed :( '); }); </code></pre> <p>PHP</p> <pre><code>public function fetch_all(){ return json_encode("this is fetch_all..."); //replace text to get all posts with get_posts() } </code></pre> <p>The console shows the JavaScript fail: "it failed :( ". How do I get 'fetch_all' to be executed and return data?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload