0

This is probably something very simple, and I've seen that there are/have been more people with the same issue. But the solutions provided there did not seem to work.

So, I want to execute a .php file through AJAX. For the sake of testing the php file (consolefunctions) is very small.

<?php
if(isset($_POST['action'])) {
<script>console.log('consolefunctions.php called.');</script>
}
?>

And now for the javascript/ajax part.

$(".startConsole").click(function(){
    var consoleID = $(this).attr("value");
    $.ajax({ url: 'include/consolefunctions.php',
        type: 'post',
        data: {action: 'dosomething'},      
        success: function(output) {
            //alert("meeh");
        }
    });
});

Somewhere, somehow there's an issue because the message from the PHP file never shows. I've tested the location from the php file, which is valid.

4
  • 1
    Why should the message from the PHP "show"? Your JavaScript isn't doing anything with the response it gets. Commented Sep 3, 2014 at 12:57
  • 1
    Even the PHP code is not valid. Commented Sep 3, 2014 at 13:01
  • Your PHP does not run the JavaScript within it - it's not even echoed out. Your PHP should throw an error. Commented Sep 3, 2014 at 13:01
  • Instead of <script>console.log('consolefunctions.php called.');</script> ( javascript ), try some php ( echo 'test'; ) Commented Sep 3, 2014 at 13:01

1 Answer 1

1

First the php code is not correct, you should add an echo

<?php
if(isset($_POST['action'])) {
 echo"<script>console.log('consolefunctions.php called.');</script>";
}
?>

but the problem is, when you send this code to js, you'll get it as a string on your variable output, not as a code that will be executed after making the ajax call, so the best way to do this is to echo only the message to display on your console and then once you receive this message you can call console.log function

<?php
if(isset($_POST['action'])) {
 echo"consolefunctions.php called";
}
?>

in the success function :

console.log(output);
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, sometimes I feel like a donkey. This is one of those moments, so obvious. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.