Hi,

I wonder if anyone has tried to implement PHP with Javascript. I have the code here like this:

if($name=='Mary') 
{
    echo "<script type=\"text/javascript\">window.alert(\"Hi, Your Name is " . $name . "
\")</script>";
}  

For some reason, I don't get anything on the screen. Could I embed javascript in PHP at all? Thanks for your help.

I do a lot crazier stuff with PHP and JavaScript than that, so I know it works. I tried your code, and it worked. The only problem was a line break after the name variable.

if ($name == 'Mary') {
 echo "<script type=\"text/javascript\">window.alert(\"Hi, Your Name is " . $name . "\")</script>";
}

I do a lot crazier stuff with PHP and JavaScript than that, so I know it works. I tried your code, and it worked. The only problem was a line break after the name variable.

if ($name == 'Mary') {
 echo "<script type=\"text/javascript\">window.alert(\"Hi, Your Name is " . $name . "\")</script>";
}

Is there a browser or system issue? I even tried by doing document.write instead of window.open and even that did not work. I designed it on Linux Ubuntu and ran it on Firefox, which did not work. I only get a blank screen when I tried to do something like that.

Here is what is not working on my system but should be,

if(($start_time !="") && (($start_time < 6)|| ($start_time > 11))) {
   echo "<script type=\"text/javascript\">window.alert(\"Sorry, service is not available at " . $start_time . "\")</script>";
      }

However, without the javascript, it falls into the above if statement, it does print out the html and php below:

if(($start_time !="") && (($start_time < 6)|| ($start_time > 11))) {
    echo      

        <p style="font-weight:bold">Sorry, service is not available at <span style="color:red"><?=$start_time?></span>.</p>
      <?
      }

What have I done wrong here?

Have you viewed the source to ensure that it's actually printing out the JavaScript? Also, is JavaScript disabled? (I have to ask. :P)

Try this

<?
if($name=='Mary')
{
?>
<script type="text/javascript">alert("Hi, Your Name is <?=$name?>");</script>
<?php }
?>

if you have not enabled short tag then use this code :

<?php
if($name=='Mary')
{
?>
<script type="text/javascript">alert("Hi, Your Name is <?=$name?>");</script>
<?php }
?>

if you have not enabled short tag then use this code :

<?php
if($name=='Mary')
{
?>
<script type="text/javascript">alert("Hi, Your Name is <?=$name?>");</script>
<?php }
?>

For some reason, this example works without the "window" in the code. In other words, alert works. and not window. alert. Strange, isn't it?

I've never actually used it as window.alert, but there must be a difference between that and just alert. Either way, I'd say just use alert.

Member Avatar for rajarajan2017
<?php
if($name=='Mary')
{
?>
<script type="text/javascript">alert("Hi, Your Name is <?php echo $name;?>");</script>
<?php 
} 
?>
Member Avatar for rajarajan2017
<?php
if($name=='Mary')
{
?>
<script type="text/javascript">alert("Hi, Your Name is <?php echo $name;?>");</script>
<?php 
} 
?>
<?php
if($name=='Mary')
{
?>
<script type="text/javascript">alert("Hi, Your Name is <?php echo $name;?>");</script>
<?php 
} 
?>

Thanks, this is a great answer. I would go a little further and do

<?php
if($name=='Mary')
{
?>
<script type="text/javascript">alert("Hi, Your Name is <?=$name;?>");</script>
<?php 
} 
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.