LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2007, 07:25 AM   #1
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Rep: Reputation: 30
how to pass a php variable to javascript functions?


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
 
Old 10-05-2007, 09:27 PM   #2
mjmwired
Member
 
Registered: Apr 2004
Distribution: CentOS6, CentOS5, F16, F15, Ubuntu, OpenSuse
Posts: 620

Rep: Reputation: 39
I am not clear what you are trying to do? Do you wish your PHP to read JS vars? Or your JS to read PHP vars?
PHP is parsed on the server, Javascript is parsed on the client.
By the time your JS is running, all your PHP code has been run and dumped into static output into text.

In your first JS box:
name=document.bet_form+<?php echo $textarea_name; ?>;
will be
name=document.bet_form+ "textarea_name"

How would JS add a string to a form element? This should return as undefined.

If you did
name=document.bet_form.<?php echo $textarea_name; ?>;
That would be accessing an element of the form which should work in JS.



From the looks of this I do not exactly understand what your are trying to do. Can't you do more in JS?

For example if your onclick event handler just passed the 'this' object you should be able access any methods of the form node?
Also if your textarea's are dynamic, the document.getElementById or document.getElementsByTagName should be able to acess any form node in the document tree.

Anyways I hope this helps, obviously I'm not clear on what function the PHP serves for this.
 
Old 10-07-2007, 12:44 AM   #3
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
This is my first time working on Javascript and I am not familiar with methods. How can I use "this" to get the value of textarea?

If I could submit the form, I could add an onsubmit handler to textarea and used this.value to send the value of textarea element.

But how can I send same value when onclick handler in another element is called? Unfortunately my form in nested and it took me long time to make it working, so that I don't want to provide any element of submit type.

Thanks
 
Old 10-07-2007, 01:00 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Uh - you do know that (like mjmwired already said), that Javascript is executed on the client, and PHP is executed on the server. They're two different things, run in two completely different places at completely different times. So depending on exactly what you're trying to do, there may (or may not) not be any way to "pass a variable" between them.

Maybe you want to do something with Ajax?

http://24ways.org/2005/edit-in-place-with-ajax

Last edited by paulsm4; 10-07-2007 at 01:03 AM.
 
Old 10-07-2007, 01:37 AM   #5
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
Basically, I don't think I need to pass PHP variable to javascript one. As it was mentioned by mjmwired I think it's possible to use javascript to send my data.

This is what I'm trying to do now. Send values of a textarea element using "this" method, by giving onclick handler to another element of the form.
 
Old 10-07-2007, 07:59 AM   #6
mjmwired
Member
 
Registered: Apr 2004
Distribution: CentOS6, CentOS5, F16, F15, Ubuntu, OpenSuse
Posts: 620

Rep: Reputation: 39
http://www.google.com/search?q=javascript+this
http://www.quirksmode.org/js/this.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
communicate a php variable to a javascript function Kanaflloric Programming 4 08-11-2006 12:58 PM
Looking for a more effecient way to pass arguments to functions. RHLinuxGUY Programming 10 05-01-2006 10:52 PM
pass javascript variable to php ALInux Programming 6 01-06-2006 06:20 AM
how to pass argv[] & argc to functions? skie_knite007 Programming 2 05-13-2005 11:12 PM
pass by address between functions Xiangbuilder Programming 2 10-10-2003 06:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration