Run A JS function within a PHP if else statement.

Javascript coding ..

Moderators: macek, egami, gesf

stylus
New php-forum User
New php-forum User
Posts: 12
Joined: Tue Oct 18, 2005 11:49 am
Location: Oakland Maine
Contact:

Run A JS function within a PHP if else statement.

Postby stylus » Fri Oct 28, 2005 10:36 am

Can somebody show me how to call a javascript fuction, when a if else statement is true.

Or should I be trying to change a css style from visibility:hidden to visible with PHP?


Here is the snippit from my javascript function:

Code: Select all

<script type="text/javascript">
<!--
function ShowHide(id, visibility) {
    obj = document.getElementsByTagName("div");
    obj[id].style.visibility = visibility;
}
//-->
</script>

<style type="text/css">
<!--
#sub1Div { visibility:hidden; }
#sub2Div { visibility:hidden; }
#sub3Div { visibility:hidden; }
#sub4Div { visibility:hidden; }
-->
</style>



Here is my form:

Code: Select all

<?php
    $admin_priv = $_POST['admin_priv'];
    $specific_date = $_POST['specific_date'];
?>


<?php echo $admin_priv; ?>

<form name="test" action="test.php" enctype="multipart/form-data" method="post">

<input name="admin_priv" type="radio" value="y"  onClick="ShowHide('sub1Div','visible')" <?php if($admin_priv == 'y') { echo "checked"; } ?> /> Yes

<input name="admin_priv" type="radio" value="n"  onClick="ShowHide('sub1Div','hidden')"  <?php if($admin_priv == 'n') { echo "checked"; } ?> /> No

<div id="sub1Div">Specific Date <input type="text" name="specific_date" id="specific_date" value="<?php echo $specific_date; ?>"></div>

<input type="submit" value="submit" name="submit">
</form>




I tried this...
<?php if($required_date == 'Specific date') { echo "checked"; ShowHide('sub1Div','hidden'); } ?>

I got a undefined function error. I tried a "fdf_set_javascript_action" but that didn't seem to help. The only info I can find is to have javascript do php functions, not php do javascript functions.

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Postby ruturajv » Fri Oct 28, 2005 11:19 pm

u can't call javascript fns in php,
PHP: is a server side tech
JavaScript: client side script,
what you can do is echo... the function call statement in the onload attribute of the body
so...

Code: Select all

<body
<?php
if (condition)
{
 echo " onload='function_name()'  ";
}
?>
>

stylus
New php-forum User
New php-forum User
Posts: 12
Joined: Tue Oct 18, 2005 11:49 am
Location: Oakland Maine
Contact:

Postby stylus » Sat Oct 29, 2005 4:30 pm

That makes sense, thanks.

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Sun Oct 30, 2005 1:53 am

There always is a DOM... that will probably work the same way..

stylus
New php-forum User
New php-forum User
Posts: 12
Joined: Tue Oct 18, 2005 11:49 am
Location: Oakland Maine
Contact:

Postby stylus » Sun Oct 30, 2005 7:34 am

I dont know what a "DOM" is.

stylus
New php-forum User
New php-forum User
Posts: 12
Joined: Tue Oct 18, 2005 11:49 am
Location: Oakland Maine
Contact:

Postby stylus » Mon Oct 31, 2005 5:48 am

After some playing around I got this code to work for me. It took me a minute to figure out how to have a multiple "onload" statement.

Code: Select all

if(isset($_POST['create_workorder']))
{ echo "<body onload=Javascript:";
   if ($required_date == 'Specific date') { echo "ShowHide('sub1Div','visible');"; };
   if ($notify_client == 'Yes') { echo "ShowHide('sub2Div','visible');"; };
   if ($purchase_status == 'Demo') { echo "ShowHide('sub3Div','visible')"; };
   echo " > ";
}


Return to “JavaScript”

Who is online

Users browsing this forum: No registered users and 1 guest