Page 1 of 1

calling javascript with php

Posted: Sat Jan 06, 2007 12:39 pm
by sarris
hi there
i am calling a javascript function within a php file in that manner

Code: Select all

echo '<script language="javascript">myfunction()</script>';
works ok.
But when i try to pass a parameter it doesnt work.

Code: Select all

echo '<script language="javascript">myfunction($row[price])</script>';
where $row[price] is result from a sql querry using mysql_fetch_array().

in the first case the function is

Code: Select all

function myfunction(){
     alert("Hallo");
}
in the second case the function is

Code: Select all

function myfunction(x){
     alert(x);
}
I guess the javascript doesnt recognise the $row[price] variable...
any ideas?

Posted: Sat Jan 06, 2007 1:29 pm
by onion2k
I guess the javascript doesnt recognise the $row[price] variable
Very true, it doesn't. There's no intrinsic link between PHP and Javascript. In essence what happens is that PHP dumps a load of text into a "file", the browser downloads that text, and then it displays the HTML and runs the Javascript. If you want Javascript to access something you'll need to make your PHP script echo it out as if you were coding it into a static HTML page.

Posted: Sat Jan 06, 2007 4:27 pm
by sarris
hmmm...i get what you mean but i am not sure if i know how to do that.
so far this same php file gets the data from the database and forms a variable $display_block which is the html code. in the end of the html code i do echo $display_block and everything is ok.
But in html you call javascript functions when events are held...I want this function "myfunction" to be called for each result (each row) of the mysql query result.
I am not sure if i make my self clear...sorry

Posted: Mon Jan 08, 2007 11:22 am
by sarris
is there anyone who can tell me a simple way to just show a php variable on javascript?

this is what i do

Code: Select all

echo '<script language="javascript">
                   corx = <?php echo $row[coords_X] ?>;
                   cory = <?php echo $row[coords_Y] ?>;
                   myfunction(corx,cory)</script>';
my function is

Code: Select all

function myfunction(x,y){
alert(x);
}
$row[coords_X] and $row[coords_Y] are valid and contain values. Have confirmed that passing the value to a textbox in html.
The alert box that comes up is empty


plz any help will be great

Posted: Mon Jan 08, 2007 11:39 am
by feyd
You may want to check the source code. You should notice something odd.

Posted: Mon Jan 08, 2007 11:47 am
by sarris
i guees you mean about the different names on the functions..hehehe...well thats not it. i just changed the name of the function on the php code that i posted and forgot to change it on the javascript...
Its not like this on my code.Anyways i told you i get empty alert boxes which means that the function is called

Posted: Mon Jan 08, 2007 12:08 pm
by Kieran Huggins
You need to provide quotes to the alert function in javascript, otherwise it will assume it's a variable, create a new empty one if it doesn't exist, then display the contents (nothing).

Posted: Mon Jan 08, 2007 12:12 pm
by sarris
but x is a variable...its passed to the function as corx

Re: calling javascript with php

Posted: Mon Jan 08, 2007 12:14 pm
by GM
sarris wrote: But when i try to pass a parameter it doesnt work.

Code: Select all

echo '<script language="javascript">myfunction($row[price])</script>';
try

Code: Select all

echo '<script language="javascript">myfunction(\''.$row[price].'\')</script>';

Re: calling javascript with php

Posted: Mon Jan 08, 2007 12:26 pm
by programmingjeff
sarris wrote:I guess the javascript doesnt recognise the $row[price] variable...any ideas?
The problem isn't Javascript...it's PHP. You are incorrectly placing the variable within the quotes.


-Strings with embedded variables must have double quotes.

-If `price` is a column in the MySQL query, it should have quotes around it.

-Array variables within strings must have {} brackets around it



Either of the following codes should work (the first one will be slower to parse in PHP):

Code: Select all

echo "<script language='javascript'>myfunction('{$row['price']}')</script>";

Code: Select all

echo '<script language="javascript">myfunction("'.$row['price'].'")</script>';

UPDATE: Added quotes within the Javascript functions, as noted by GM

Posted: Mon Jan 08, 2007 12:34 pm
by GM
@programmingjeff: I think you may missing the quotes within the parentheses of the call to the js function - without them javascript would look for a javascript variable called whatever $row['price'] contains. This could be the desired behaviour, of course, if $row['price'] contains the name of the javascript variable to pass to the function, although I can't think why you'd want to do this.

@sarris: The best thing to do when writing javascript to the screen is to check the source code of the page produced for anomalies, as feyd has already pointed out.

Posted: Mon Jan 08, 2007 3:29 pm
by sarris
thanks gm
thanks programmingjeff
worked ok now!