View Full Version : Php in javascript?

04-27-2006, 10:42 AM

I've got a problem with my javascript function. I'd like to make sql query from it and pass its result back to function. What more I need to pass javascript variable to php select. Function must be in javascript bacause it's called by onchange event.

function foo(my_var)
$MyVar = "?>document.write(my_var);<?php";
$MyVar = str_replace("?>", "", $MyVar2);

mysql_connect('localhost', 'user', 'pass');
$result = mysql_query("SELECT * FROM tableA WHERE id = $MyVar")
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$var2 = $row[0]."=".$row[1];
echo $var2;

Can I do sthg like that or is it totally wrong? How can I make it work?

Thanks in advance.

04-27-2006, 10:56 AM
Can I do sthg like that or is it totally wrong?It's rather wrong. PHP cannot be called by a client-side event, although you can use it to generate Javascript that will be. The easiest way to do this is to create a form that will be submitted to the PHP script.

04-27-2006, 11:06 AM
The other way is using Ajax, but that's more complex.

Either way, through JS in the background (ajax) or refreshing/submitting a form/clicking a link, you need to make the page reload to have the php get reevaluated.