I m trying to pass a php variable to a Javascript function, but it returns undefined.
My php variable '$textarea_name', is used to define a textaream element in a form.
Then I would like to call a javascript function on onclick event and pass the value of my textarea element defined by php variable as its 3d argument.
I added the following script to the bottom of the page after everything:
PHP Code:
<script>
function get_name()
{
name=document.bet_form+<?php echo $textarea_name; ?>;
return name;
}
</script>
And I added following code after declaring my textarea element:
PHP Code:
print "<script>";
print "text=get_name();";
print "</script>";
And on onclick event I call another javascript function with textarea element passed to it as third arguent:
PHP Code:
onclick='send_comment($match_id,$user_id,text.value);'
As you can see, first two arguments are also php variables, but they are not related to any form and form element, that's why they are passed to send_comment function correctly, however, text.value passes 'undefined' value. I have tried all combinations to obtain value of textarea element, but non of them worked.
I tried to set the name of textarea field to a constant string name "description" (not a php variable) and pass it to send_comment() and it worked fine, but I have 10 different dynamic text area in my form and they should have different names. That is, I have to assign a name to each of them based on some criteria (in my case match_id).
The code for passing string value of textarea element which works is like this:
PHP Code:
onclick='send_comment($match_id,$user_id,document.bet_form.description.value);'
I have been working on this small part of code to get it working for a long time with no success.
Any suggestion?
Thanks in Advance