Prabhu Ganapathy
Prabhu Ganapathy

Reputation: 167

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

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

Upvotes: 3

Views: 1138

Answers (2)

Chad
Chad

Reputation: 19609

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.

Upvotes: 5

Paolo_Mulder
Paolo_Mulder

Reputation: 1289

Do not try to mix javascript with php.

If you want to stop the page from loading you could also redirect the page to an other (php) page where you simply put exit();

If this javascript is in a function just use : return; or return false;

Upvotes: 0

Related Questions