Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a PHP function in JavaScript?

I have

index.php

<select id="year_list" name="year_list" onchange="check_year_event('year_list', 'event_list');" > . . .  </select>

<select id="event_list" name="event_list" onchange="check_year_event('year_list', 'event_list');" > . . . </select>
.
.
.
<?php 
 function checkYearandEvent($year, $event) {
  $year_event = mysql_query("SELECT * FROM year_event WHERE year = '$event' AND event = '$event'")

  if (mysql_num_rows($year_event) > 0) {
   // do this
  }

 }
?>

myscripts.js

function check_year_event(year_id, event_id) {
 var year = document.getElementById(year_id).value;
 var event = document.getElementById(event_id).value;

 // call PHP function (but I don't know how): checkYearandEvent(year, event);

}

My question is how do I call the PHP function every time the user changes the value of any of the select element.

like image 759
Newbie Coder Avatar asked May 12 '11 21:05

Newbie Coder


People also ask

Can you call PHP function in JavaScript?

You can call PHP function through Javascript by passing the value, you need as output of PHP function as a string in Javascript.

Can I call PHP function in HTML?

Calling a PHP function using the HTML button: Create an HTML form document which contains the HTML button. When the button is clicked the method POST is called. The POST method describes how to send data to the server. After clicking the button, the array_key_exists() function called.

Can I use PHP variable in JavaScript?

We can pass data from PHP to JavaScript in two ways depending on the situation. First, we can pass the data using the simple assignment operator if we want to perform the operation on the same page. Else we can pass data from PHP to JavaScript using Cookies.

Can jQuery call PHP function?

Using jQuery AJAX to Run PHP Code If you are using jQuery on your website, it becomes incredibly easy to call any PHP file with code that you want to run. You can pass one or two parameters to the ajax() function.


2 Answers

You need to use ajax. There is a basic example:

myscripts.js

function AjaxCaller(){
    var xmlhttp=false;
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }

    if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

function callPage(url, div){
    ajax=AjaxCaller(); 
    ajax.open("GET", url, true); 
    ajax.onreadystatechange=function(){
        if(ajax.readyState==4){
            if(ajax.status==200){
                div.innerHTML = ajax.responseText;
            }
        }
    }
    ajax.send(null);
}

function check_year_event(year_id, event_id) {
 var year = document.getElementById(year_id).value;
 var event = document.getElementById(event_id).value;

    callPage('file.php?year='+year+'&'+'event=+'+event,document.getElementById(targetId));
}

file.php

<?php 

     function checkYearandEvent($year, $event) {
      $year_event = mysql_query("SELECT * FROM year_event WHERE year = '$event' AND event = '$event'")

      if (mysql_num_rows($year_event) > 0) {
       // do this
      }

     }
    echo checkYearandEvent($_GET['year'], $_GET['event']);
?>
like image 94
Galled Avatar answered Sep 27 '22 20:09

Galled


You won't be able to do this in the way you might be expeting to. PHP is executed on the server, before the browser receives the HTML. On the other hand, JavaScript runs in the browser and has no knowledge of the PHP (or any other server side language) used to create the HTML.

To "call" a php function, you have to issue a request back to the server (often referred to as AJAX). For example, you could have a checkYear.php script which checks the event and returns some HTML indicating whether the check succeeded. When the HTML fragment gets back to the JavaScript, you inject it into the page.

Hope this helps!

like image 45
jimbo Avatar answered Sep 27 '22 20:09

jimbo