Page 1 of 1

call javascript from php code

Posted: Wed May 14, 2008 7:04 am
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!

Re: call javascript from php code

Posted: Wed May 14, 2008 11:40 am
by suresh_2220
Try Something like this

<?php

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

?>

Re: call javascript from php code

Posted: Wed May 14, 2008 11:39 pm
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.