0

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.

Edited by mike_2000_17: Fixed formatting

4
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by ajwei810192
Featured Replies
  • 1

    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. [code=php]if ($name == 'Mary') { echo "<script type=\"text/javascript\">window.alert(\"Hi, Your Name is " . $name . … Read More

1

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>";
}

Edited by MVied: n/a

0

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?

0

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

0

Try this

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

0

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 }
?>

0

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?

0

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.

0
<?php
if($name=='Mary')
{
?>
<script type="text/javascript">alert("Hi, Your Name is <?php echo $name;?>");</script>
<?php 
} 
?>
0
<?php
if($name=='Mary')
{
?>
<script type="text/javascript">alert("Hi, Your Name is <?php echo $name;?>");</script>
<?php 
} 
?>
0
<?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 
} 
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.