views:

63

answers:

1

I want to trigger javascript alert using PHP. Is it possible

I want to use it in head section, for displaying it at load time.

<head>
    <?php 
    $valid="valid";
        if(!isset($valid))
            echo "<script type=\"text/javascript\"> alert('Hi');</script>";
    ?>
</head>

EDIT

i want to display javascript alert() at load time after checking existance of session

+3  A: 

Of course this is possible.

All you are doing is outputting JavaScript, it's all the same thing.

The only issue is that you are nesting <script> tags, which is an HTML error, so get rid of the tags in the echo string.

<script type="text/javascript">
<?php 
    $valid="valid";
    if(!isset($valid))
        echo "alert('Hi');";
?>
</script>

By the way, as i'm sure you already know, this specific code will ALWAYS echo the "alert('Hi');"

Jacob Relkin
@Jacob: Actually I m new to web development and done a lot of work on .Net that's too in window application. That's why I no many things but there are many holes still left to be filled.
Shantanu Gupta
@Jacob: THx this worked out. I although tried this solution provided by you but session was not being set that's why i was not able to trace my error and thought thr is some problem in javascript. Thx anyway
Shantanu Gupta
@Shantanu That's ok. We were all new to this stuff once too.
Jacob Relkin
@Shantanu, If this is the answer you were looking for, accept it so it doesn't linger in the 'Unanswered' section.
Jacob Relkin