-1

This is the code I am executing inside PHP condition

<?php
    if(is_null($model)) {
        echo "<script>window.parent.closeIframe(true)</script>";
    }
?>

It doesn't work even If I put alert or calling a function inside my PHP condition.

Is it possible to run a Javascript code inside PHP ? Is there any PHP extensions needed to be enabled ?

The Codesee
  • 3,714
  • 5
  • 38
  • 78
  • 1
    php is run at server side, javascript is run at client side. – weigreen Jun 06 '16 at 15:23
  • 1
    Possible duplicate of [How to call a JavaScript function from PHP?](http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php) – Styphon Jun 06 '16 at 15:25
  • echo a function in the script tag, and at the bottom of the page call for that function. However, then you should echo something else when `$model` is not null or... if you don't care about the error log, don't. You can also use the [jquery document ready](https://api.jquery.com/ready/) method. – Xorifelse Jun 06 '16 at 15:27
  • That's not jquery, that is javascript.. – chris85 Jun 06 '16 at 15:28
  • I think you certainly have to read [What is the difference between client-side and server-side programming?](http://stackoverflow.com/q/13840429/218196) – Felix Kling Jun 06 '16 at 15:40
  • @FelixKling I was expecting this comment from someone. – Amit Ray Jun 06 '16 at 15:56

4 Answers4

0

Was working on a recent project to execute JavaScript after PHP was finished executing. Try placing the script tag after the PHP tag:

test.php:

<?php
    echo "hello";
    include("example.html") // To insert HTML page
?>
<script>
    alert("hello");
</script>
agorapax
  • 1
  • 2
  • What if the OP wanted to run the ` – The Codesee Jun 06 '16 at 15:45
  • I used a PHP boolean variable then echoed that valued to a hidden input field. Called it in the – agorapax Jun 06 '16 at 15:50
0

This worked for me. You can try it. I forced $model to be null.

<?php
    $model = null;
    if(is_null($model)) {
        echo "<script>alert('Hello World')</script>";
    }
?>
Amit Ray
  • 3,445
  • 2
  • 19
  • 35
0

You can't call close from client Javascript from server PHP.
but you can call from HTML client. put close in bottom of HTML body tag and make sure Javascript is turned on in your browser.

0

Hi if you are executing a query and then checking if result is null then this script to run then please correct. PHP is_null is true when it get NULL php datatype and a query never returns this. So if i am right then you can use this one this is my example which i used to check your method.

<?php
$con = mysqli_connect("localhost","root","","user") or die("error");
$res = mysqli_query($con, "select * from user where id =-99");
$model = mysqli_num_rows($res);
        if(empty($model)) {
            echo "<script>window.parent.closeIframe(true)</script>";
        }
    ?>

just to check you can refer

$model = 0;
 <?php
        if(empty($model)) {
            echo "<script>alert("hello")</script>";
        }
    ?>
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44