1

I have a question regarding my portfolio website creaded with a wordpress theme. I want to call a function in a php file from a jquery script. But the php file is not responding. I am not sure if I call it the right way. I would like if someone can take a look at.

jQuery(document).ready(function($) {
    $('#allbuttons').click(function(event){
        $.ajax({url: 'wp-content/themes/scope/single-portfolio.php',
            data: {action: 'test'},
            type: 'post',
            success: function(output) {
                alert(output);
            }
    });

And this is the php:

<?php if(isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];
    echo "ACTIE!!!!";
    switch($action) {
        case 'test' : 
            echo "done!!";
            break;
        // ...etc...
    }
 } else {
     echo "mislukt:(!!!!";
 }?>

Maybe it is not a recommed way but i need to change the content which is currently being displayed which is being controlled in this php file. I also tried the accepted answer from: How can I call PHP functions by JavaScript?

But that also did not give me any response. I am not familair with both scripting languages.

Thank you in advance

Community
  • 1
  • 1
Kim
  • 73
  • 2
  • 9
  • 2
    `}{else{` possibly a typo but figured I'd point it out – JasonSec May 15 '14 at 13:58
  • Check what is passed thought the network using firebug, IE Developer Tool or Chrome Developer Tool. – sergiogarciadev May 15 '14 at 14:00
  • Yes I saw it I wrote it wrong in here. But ty :) – Kim May 15 '14 at 14:00
  • In the element inspector i can see that the php file has been called. or at least I think so because in the network tab, when I press the button which is suppose to call the function, I see another php file apear in the network list. Is that what you mean? – Kim May 15 '14 at 14:08
  • @Kim Try this; In chrome open the _Network_ tab, then filter by _XHR_. Look for the post/call to _single-portfolio.php_. Click on _Preview_ and what do you see there? Also, in the _Headers_ tab what is the Status Code? – L4DD13 May 15 '14 at 14:11
  • 1
    are you sure to call your ajax ? add an alert to check it before $.ajax – pietro May 15 '14 at 14:11
  • I receive something! I got the echo "mislukt". which means I am receiving an empty $_POST['action'], I believe. – Kim May 15 '14 at 14:14
  • Looks like you're making progress, also since you are using $.ajax, you can add in `error: function(output) { console.log(output); }` to see if your script is causing a PHP error – viion May 15 '14 at 14:17
  • I think so because the alert in the success appears, only empty. – Kim May 15 '14 at 14:18
  • I've tried you're code and it appears to work fine. Any chance of a screenshot of the headers and the response panel in Chrome? – L4DD13 May 15 '14 at 14:19
  • http://www.kimverweij.com/?portfolio=user-interface This is the website and when you press the gallery of video button the php must be called. I do not see anything by XHR? @L4DD13 – Kim May 15 '14 at 14:23
  • @Kim could you add the following to the top of your single-portfolio.php script? `';var_dump( $_POST ); echo ''; ?>`, let me know when and if you've done that so I can have another look. – L4DD13 May 15 '14 at 14:32
  • I can see the php file appear in XHR. Is posting the website oke? or do you want a screen shot – Kim May 15 '14 at 14:32
  • @L4DD13 Yes i have put the code in the script – Kim May 15 '14 at 14:33
  • Also, try moving the call to `buttonclick.js` to the bottom after the JS call, it's a long shot though – L4DD13 May 15 '14 at 14:34
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52769/discussion-between-l4dd13-and-kim) – L4DD13 May 15 '14 at 14:35
  • 1
    One little remark: `isset()` is part of `!empty()`, so you could do with`only `!empty()` – Michel May 15 '14 at 14:38

1 Answers1

1

The problem was: I was calling a wp script in a way which it should not be. I was trying to call the php directly while it requires to be called as part of the wordpress load process. But @L4DD13 helped me to find a way to work around it:) ty all for the help and advice!

Kim
  • 73
  • 2
  • 9