Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to Call javascript  sunction through php

Posted on 2011-10-18
5
Medium Priority
?
226 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:savsoft
5 Comments
 
LVL 10

Accepted Solution

by:
gavsmith earned 2000 total points
ID: 36984465
Shouldn't it be like this?

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

<body>
<?php

if(condition)
{

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

Open in new window


Apologise in advance if I'm wrong I only know a little php!
0
 
LVL 10

Expert Comment

by:gavsmith
ID: 36984508
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
0
 
LVL 13

Expert Comment

by:ansudhindra
ID: 36984515
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

0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36984538
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

0
 

Author Comment

by:savsoft
ID: 36984810
Thank You All.... All Answer accepted
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question