0

I hope to run a php code inside a javascript code too and I do like that :

<?php function categoryChecking(){

return false;
}?>

....

function openPlayer(songname, folder)
{
if(<?php echo categoryChecking() ?> == true)
{
if (folder != '')
{
    folderURL = "/"+folder;
}else
{
    folderURL = '';
}
    var url        = "/users/player/"+songname+folderURL;
    window.open(url,'mywin','left=20,top=20,width=800,height=440'); 
 }else{

    alerte('you should click on a subcategory first');

}
}

....

<a href='javascript:void();' onClick="openPlayer('<?php echo $pendingSong['id']; ?>','')">

finally I get this error instead the alert message "you should click on a subcategory first"

ReferenceError: openPlayer is not defined


openPlayer('265','')
2
  • 1
    PHP is run on the server side once when the client requests the page. JS is run on the client side. AJAX is the only form of communication between JS -> PHP you'll have. Commented Nov 17, 2013 at 14:46
  • 1
    you need use ajax for it. Commented Nov 17, 2013 at 14:46

2 Answers 2

0

You're reduced your test case too far to see for sure what the problem is, but given the error message you are receiving, your immediate problem has nothing to do with PHP.

You haven't defined openPlayer in scope for the onclick attribute where you call it. Presumably, the earlier JS code is either not inside a script element at all or is wrapped inside a function which will scope it and prevent it from being a global.

Update: @h2ooooooo points out, in a comment, that your PHP is generating the JS:

if( == true)

Check your browser's error console. You need to deal with the first error messages first since they can have knock on effects. In this case the parse error in the script will cause the function to not be defined.


Once you resolve that, however, it looks like you will encounter problems with trying to write bi-directional code where some is client side and some is server side.

2
  • The problem is also that echo false outputs (empty string) so the JS would be if ( == true) Commented Nov 17, 2013 at 14:47
  • @h2ooooooo — Good spot. Commented Nov 17, 2013 at 14:51
0

You cannot run PHP code from JavaScript, because PHP is a server-side language (which runs on the server) and JavaScript is a client-side language (which runs in your browser).

You need to use AJAX to send a HTTP request to the PHP page, and then your PHP page should give a response. The easiest way to send a HTTP request using AJAX, is using the jQuery ajax() method.

Create a PHP file ajax.php, and put this code in it:

<?php
   $value = false; // perform category check
   echo $value ? 'true' : 'false';
?>

Then, at your JavaScript code, you should first add a reference to jQuery:

<script type="text/javascript" src="jquery.js"></script>

Then, use this AJAX code to get the value of the bool:

<script type="text/javascript">
    $.ajax('ajax.php')
        .done(function(data) {
            var boolValue = data == 'true'; // converts the string to a bool
        })
        .fail(function() {
             // failed
        });
</script>

So, your code should look like this:

function openPlayer(songname, folder) {
    $.ajax('ajax.php')
        .done(function (data) {
        var boolValue = data == 'true'; // converts the string to a bool
        if (boolValue) {
            if (folder != '') {
                folderURL = "/" + folder;
            } else {
                folderURL = '';
            }
            var url = "/users/player/" + songname + folderURL;
            window.open(url, 'mywin', 'left=20,top=20,width=800,height=440');
        } else {

            alert('you should click on a subcategory first');

        }
    })
        .fail(function () {
        // failed
    });
}
5
  • yes you upodate my code above to do this because I'm not professional in ajax...? Commented Nov 17, 2013 at 15:11
  • @user3001795: I updated my answer and I added some code. Commented Nov 17, 2013 at 15:41
  • here you gave me a general example and I know to do that but what I talked about is how to call ajax automatically inside my "openPlayer" javascript function? Commented Nov 17, 2013 at 16:06
  • You just need to move the code of your openPlayer function into the .done function of the AJAX, and you need to replace <?php echo categoryChecking() ?> with boolValue. That's all. Commented Nov 17, 2013 at 16:09
  • I dont get you and that will not work I guess can you do an update of code to understand you? Commented Nov 17, 2013 at 16:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.