0

I need help with this code.

What I want to accomplish is to insert a textarea with a name value which is retrieved from a database (not shown), by a onclick event. I should have a php code, where there is a button, when I click this button, I call a function which inserts this textarea into a position, given by a identified by this name.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="pragma" content="no-cache" />
<title>Example</title>
<SCRIPT language="javascript"> 

    function add_comments($name) {
     var element = document.createElement("textarea");
     var docplace = document.getElementById($name);

     docplace.innerHTML = docplace.innerHTML ;
     element.setAttribute("name", $name);
     element.setAttribute("cols",50);
     element.setAttribute("rows",5);
     element.setAttribute("value", $name);

    docplace.appendChild(element);
    docplace.innerHTML = docplace.innerHTML + "<br/>";
}

</SCRIPT>
</head>
<?php

echo "<br>";
$TempTask = 'thistask';
echo '<form>';
echo "<br>";
echo $TempTask;
echo "<br>";

?>
<input type="button" value="Text Area" onclick="add_comments('$TempTask');">
<?php

echo '<div id=$TempTask>Right here!</div>';
echo '</form>';
?>
</html>

2 Answers 2

4

Just use an inline PHP tag, and echo it:

onclick="add_comments('<?php echo $TempTask ?>');"

Or echo the whole thing and let PHP interpolate the value:

echo "<input type=\"button\" value=\"Text Area\" onclick=\"add_comments('$TempTask');\">";

Be aware that it may break if your variable contains quotes, line breaks, etc.

Sign up to request clarification or add additional context in comments.

5 Comments

These suggestions did not work. I forgot to say, I get the textarea.value = $Temptask. I want to have textare.value = thistask. I am using PHP from WAMP, 06 Jan 2011, PHP 5.3.5, IE8.
Sorry, but I don't think I understand what you're asking then.
Thanks for your interest. What I want to do is to insert a textarea in a while loop. I will retrieve several records and post them with PHP. I create a division so that I know where to insert the text area . If the reader want to place a comment, he will click on a comment button and I will be able to insert this textarea into the appropriate record. Pretty much like this comment area in stackoverflow.com.
@user2245746 Here is your code without the PHP: jsfiddle.net/MbXkN. It seems to work. Could you look at it, and tell me what you would like to change?
Found a way to pass the value from PHP to Javascript. I just broke php, inserted javascript and continued. ?> <script type="text/javascript"> var anyname = "<?php echo $TempTask; ?>"; </script> <?php So, this anyname variable is a javascript global and available for the javascript functions.
0

Found a way to pass the value from PHP to Javascript. I just broke php, inserted javascript and continued.

?>
<script type="text/javascript">
var anyname = "<?php echo $TempTask; ?>";
</script>
<?php

So, this anyname variable is a javascript global and available for the javascript functions.

1 Comment

Great, that's what I meant by "inline PHP tag" in my answer. Sorry if I wasn't clear.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.