calling javascript function from php code

Codes here !

Moderators: macek, egami, gesf

sheeshkamal
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Aug 30, 2012 9:03 pm

calling javascript function from php code

Postby sheeshkamal » Thu Aug 30, 2012 9:07 pm

Hi All,

I have problem in follwing script.

<?php
if(isset($_GET['submit'])){
if(!empty($_GET['name'])){
$name=$_GET['name'];

echo'<script type="text/javascript">show();</script>';

}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
#display{ display:none; border:2px dashed #0033FF; width:200px}
</style>
<script type="text/javascript">
function show(){

document.getElementById("display").style.display="block";
}
</script>
</head>

<body>
<form method="get" action="form.php">
<input type="text" name="name" />
<input type="submit" name="submit" />

</form>
<div id="display">

<?php echo $name; ?>
</div>
</body>
</html>

I am trying to make visible div when php code executes.

Can Anyone please help me on this????

NigelRen
php-forum Active User
php-forum Active User
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

Re: calling javascript function from php code

Postby NigelRen » Fri Aug 31, 2012 11:27 pm

You cannot call Javascript from PHP, PHP is only executed on the server, whilst Javascript is a client side language.
What you can do with PHP is to create the HTML that reflects what you want, so if you had...

Code: Select all

if(isset($_GET['submit'])){
if(!empty($_GET['name'])){
$name=$_GET['name'];
    ?>
     <div id="display">
   
    <?php echo $name; ?>
    </div>
    <?php
}
}

Then this part of the html will only be included only when you have the fields set.


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 2 guests