-4

I'm trying to use my PHP function in JavaScript like this:

PHP code:

function checkevents($date){
    $ev = new CalendarModelEventss();
    $wh = "`event_date` = '".$date."'";
    $all = $ev->getAllEvents('event_date', $wh);
    if($all == $date){
        //echo 'event is available'.$all;   
        return true;
    } else {
        return false;
        //echo 'event is NOT available' .'--'. $all;
    }
}

and JavaScript code:

var yyyy = date.getLocalFullYear(true, this.dateType);
var mm = date.getLocalMonth(true,this.dateType);
var dd = iday;
var ddddd = yyyy+'/'+mm+'/'+dd;

if(<?php checkevents( ?>ddddd<?php ) ?>){
     cell.className += " eventsss ";
}

but it doesn't work, and I have PHP error (syntax error, unexpected ';', expecting ')' in ...).

stealthyninja
  • 10,343
  • 11
  • 51
  • 59

3 Answers3

3

As the previous speaker said, it's impossible to directly call php functions from javascript. PHP could be used to output javascript though.

In your case you would need to send an ajax-request from the client side (with javascript) that executes the php code (on the server side), fetches the output and puts it into your if-statement.

If you do not know alot about javascript/AJAX you could take a look at jQuery or another similar framework.

Niclas Larsson
  • 1,317
  • 8
  • 13
2

Javascript is parsed on client side and PHP on Server side.
Without AJAX this is impossible.

Example with JQuery:

PHP:

...
    echo "true";//or something else that indicates the success
} else {
    echo "false";//or something else that indicates the failure
}
...

Javascript:

$.ajax({
  url: "URL TO YOU PHP SCRIPT",
  cache: false
}).done(function( html ) {
  if(html.match(/true/g)){
     cell.className += " eventsss ";
  }
});
Florian Kasper
  • 496
  • 3
  • 8
0

You cannot directly call php function using javascript directly as both of them are on different sides.So what you do is make an AJAX call with certain parameters and after that call that php function directly from that php file with the parameters to be validated.

And Send back the output back to javascript by echoing the output and do whatever with it on javascript.

ameyav
  • 28
  • 3