Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we call Php functions based on the Java script If-else condition?

Tags:

javascript

php

How can we call Php functions with in the JavaScript If-else condition.

For Example,

 if (top == self) {

  // not in any kind of frame

} else {

// in a frame

// calling php function

}
</script>

Here, PHP exit() function calling for both if and else conditions. I need to call that function for only in else part.

Please advise me.

Thanks. Prabhu

like image 655
Prabhu Ganapathy Avatar asked May 11 '26 21:05

Prabhu Ganapathy


1 Answers

Technically not possible. However you can make an AJAX call to a PHP page and get the values returned by it.

PHP (test.php):

<?php
$x = 10;
$y = 20;
$val = $y / $x;

echo $val;
?>

Javascript (jQuery):

$(document).ready(function() {
   $.ajax({url: 'test.php',
      type: 'POST',
      success: function(data){
         //takes the value returned by test.php and
         //puts it directly into the element with 
         //id = someElement
         $('#someElement').html(data);
      }
});

As far as calling exit(); and making the page that holds the JavaScript stop processing, you just can't unless you control the logic on that same page in PHP and call exit(); from there.

like image 174
Chad Avatar answered May 14 '26 13:05

Chad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!