call javascript from php code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
prokopis
Forum Newbie
Posts: 1
Joined: Wed May 14, 2008 6:58 am

call javascript from php code

Post by prokopis »

Hi,
I am new in php programming and I would like your help.

I have made a script that appears and disappears text. This is the javascript :

Code: Select all

function tog(submenu) {
        if (document.getElementById(submenu).style.display == 'none'){
            document.getElementById(submenu).style.display = 'block'
        } else {
              document.getElementById(submenu).style.display = 'none'
        }
    }
Also, I have in html a text like this

Code: Select all

<span class="style1" id="text"> TEXT</span>


I would like when something happen to disappear the text.

So, I am trying something as

Code: Select all

 
<?php
    if ($variable==1){
    ?>    
        <span class="left" tog('text'); return false;></span>
<?php
}
?>
Unfortunately, it does'nt work.
Could somebody help me?

Thanking you in advance!


P.s.: I try also

Code: Select all

<input value="ere" type="submit" id="button1" onClick="toggle('text1','text2','textfield','button1','<?php $variable ?>'); return false"/>
with javascript

Code: Select all

function toggle(a,b,c,d,variable) {
      if ((variable=='yes')&&(document.getElementById(a).style.display != 'none'))
        {
            document.getElementById(a).style.display = 'none'
             document.getElementById(b).style.display = 'none'
              document.getElementById(c).style.display = 'none'
              document.getElementById(d).style.display = 'none'
        }
    }
but nothing!
suresh_2220
Forum Newbie
Posts: 23
Joined: Mon May 12, 2008 3:13 pm

Re: call javascript from php code

Post by suresh_2220 »

Try Something like this

<?php

echo "<script type=\"text/javascript\">javascript:tog('text');</script>";

?>
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: call javascript from php code

Post by dbemowsk »

You are mixing 2 different scripting types. Javascript is considered client side scripting, and PHP is considered server side scripting.

I have an article called "Introduction to PHP" on my website http://www.phpwebscripting.com. This post may prompt me to write a more in depth article on using the two different scripting types to get a desired output.
Post Reply