Link to home
Start Free TrialLog in
Avatar of savsoft
savsoftFlag for India

asked on

How to Call javascript sunction through php

i want to call javascript function through php

<head>
<script language="javascript">
function freeplan(id)
{
alert(id);
}
</script>
</head>

<body>
<?php

if(condition)
{

}

else
{
javascript:freeplan(id);
}
?>
</body>


It Is not working..... Plz Tell me the way how to call javascript function
ASKER CERTIFIED SOLUTION
Avatar of gavsmith
gavsmith
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm sorry, getting mixed up with asp! It's been a while, I've just dusted my old book off and had a look (should have done that first), from what I've just read in the book it should be something like this:

<head>
<script language="javascript">
function freeplan(id)
{
alert(id);
}
</script>
</head>

<body>
<?php

if(condition)
{

}

else
{
 print <<< _HTML_
  <script language="javascript">
  freeplan(id);
  </script>
 _HTML_;
}
?>
</body>

Open in new window


Hope that helps
you cannot call javascript function from PHP. PHP is server side (runs at server) and javascript is client side (runs in user browser).
If you want to call function from PHP, then you have to create a function in PHP. again this function is available to only PHP.
below code may help you...
function freeplan(id)
{
alert(id);
}
</script>
</head>

<body>
<?php

if(condition)
{
    //some logic here
}
else
{
?>
<script language="javascript">
  freeplan(<?php echo $iid; ?>);
</script>
<?php
}
?>
</body>

Open in new window

Avatar of Kiran Sonawane
OR


<head>
<script language="javascript">
function freeplan(id)
{
alert(id);
}
</script>
</head>

<body>
<?php

if(condition)
{

}
else
{
   <?php echo "<script>freeplan('123');</script>"; 
}
?>
</body>

Open in new window

Avatar of savsoft

ASKER

Thank You All.... All Answer accepted