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','')
user3001795
  • 515
  • 1
  • 5
  • 8
  • 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. – h2ooooooo Nov 17 '13 at 14:46
  • 1
    you need use ajax for it. – rray Nov 17 '13 at 14:46

2 Answers2

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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
    });
}
ProgramFOX
  • 6,131
  • 11
  • 45
  • 51