tags:

views:

70

answers:

6

How can I add script inside a php code? suppose i want to give an alert for a button click.. how can i do that??

A: 

Exactly the same way you add HTML tags. Echo it

Col. Shrapnel
A: 
<?php
  echo"<script language='javascript'>

</script>
";
?>
Haim Evgi
+1  A: 

You mean JavaScript? Just output it like anything else in the page:

<script type="text/javascript">
  <?php echo "alert('message');"; ?>
</script>

If want PHP to generate a custom message for the alert dialog, then basically you want to write your JavaScript as usual in the HTML, but insert PHP echo statements in the middle of your JavaScript where you want the messages, like:

<script type="text/javascript">
  alert('<?php echo $custom_message; ?>');
</script>

Or you could even do something like this:

<script type="text/javascript">
  var alertMsg = '<?php echo $custom_message; ?>';
  alert(alertMsg);
</script>

Basically, think about where in your JavaScript you want PHP to generate dynamic output and just put an echo statement there.

Jeff
+1  A: 

You mean you want to show a javascript alert when a button is clicked on a PHP generated page?

echo('<button type="button" onclick="alert(\'Alrt Text!\');">My Button</button>');

Would do that

Dai
Thanxx all it worked..
champ
thanxx all! my problem is solved...
champ
+2  A: 

You can just echo all the HTML as normal:

<?php
   echo '<input type="button" onclick="alert(\'Clicky!\')"/>';
?>
Pat
A: 

To avoid escaping lot of characters:

   echo <<<MYSCRIPT

    ... script here...

    MYSCRIPT;

or just turn off php parsing for a while:

?>
...your script here
<?php
killer_PL