Reputation: 1
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>
Upvotes: 0
Views: 727
Reputation: 1
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.
Upvotes: 0
Reputation: 71939
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.
Upvotes: 4