Jump to content

Adding javascript into PHP?


Petty_Crim

Recommended Posts

I'm having a go at making bbcode for a forum i'm making. Basically a user pushes a button and the javascript sticks something into the textarea. I'm starting with the bold button but I can't seem to get it working, theres no syntax errors but the page just reloads a blank page and sits there loading for some reason.

 

<?php

echo "<script type='text/javascript'>

<!-- Begin

function write() {

form.body.focus();

form.body.value = '';

}

// End -->

</script>";

echo "Create Post<br>";

echo "<form>";

echo "<table border='0'>";

echo "<tr><td><input name='bold' type='button' value='Bold' onClick='write()'/></td></tr>";

echo "<tr><td><textarea name='body' cols='40' rows='10'></textarea></td></tr>";

echo "</table></form>";

?>

Link to comment
https://forums.phpfreaks.com/topic/72166-adding-javascript-into-php/
Share on other sites

ok - why exactly do you need to enclose this in php tags?

 

Rewrite your code:

<?php 
//if you want to do something in php you can do it here
?>
<script type='text/javascript'>
<!-- Begin
function write() {
form.body.focus();
form.body.value = '';
}
// End -->
</script>
Create Post
<form>
<table border='0'>
<tr><td><input name='bold' type='button' value='Bold' onClick='write()'/></td></tr>
<tr><td><textarea name='body' cols='40' rows='10'></textarea></td></tr>
</table></form>
<?php 
//if you want to do something in php you can do it here
?>

 

For the actual code - don't be ashamed to look at other peoples javascript (hint, when you reply to this post, right click and view the source  ;)) Anyone who is learning a new style of coding should be able to learn from other peoples work.

  • 2 weeks later...

ok - why exactly do you need to enclose this in php tags?

 

Rewrite your code:

<?php 
//if you want to do something in php you can do it here
?>
<script type='text/javascript'>
<!-- Begin
function write() {
form.body.focus();
form.body.value = '';
}
// End -->
</script>
Create Post
<form>
<table border='0'>
<tr><td><input name='bold' type='button' value='Bold' onClick='write()'/></td></tr>
<tr><td><textarea name='body' cols='40' rows='10'></textarea></td></tr>
</table></form>
<?php 
//if you want to do something in php you can do it here
?>

 

For the actual code - don't be ashamed to look at other peoples javascript (hint, when you reply to this post, right click and view the source  ;)) Anyone who is learning a new style of coding should be able to learn from other peoples work.

Will this work with an include?

 

ie I have a main page template called main.php which I use for all my pages, inside that main I use include to include the different pages in a content area. Will having the javascript out of the php tags still work?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.