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
Share on other sites

i would advise using a link instead of a button.

here is a example, i dont know to much about java script but i think this is how you would do it:

<a href="javascript:void(0);" onclick="java function"><img src"button.jpg></a>

Link to comment
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.

Link to comment
Share on other sites

  • 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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.