Forum Moderators: coopster

Message Too Old, No Replies

A Javascript and PHP problem

Calling a PHP function from a javascript script

         

technossomy

12:01 am on Dec 30, 2004 (gmt 0)

10+ Year Member



I have a function in javascript called "foo" and a function in a PHP include file called "foo2" and I want to call foo2 from foo in an event:

<script language="JavaScript" type="text/javascript">
function foo( str ) {
var strOnclick = "<?php foo2('" + str + "');?>";
return "<a href=\"#\" onclick=\"" + strOnclick + "\"><img src=\"mypic.gif\" alt=\"" + str + "\"></a> ";
}
</script>

The value of strOnclick is correct, with proper apostrophes included. Yet, the debugger mentions a syntax error. Is there another way of what I am trying to achieve?

Thanks in advance

Tech Nossomy

CaseyRyan

12:14 am on Dec 30, 2004 (gmt 0)

10+ Year Member



The problem is that your javascript is evaluated on the client and the php is evaluated on the server. Executing your javascript code will only put a value into the onclick that it can't evaluate.

You should have the javascript post a form and have php code that interprets the post on the server and executes foo2.

-=casey=-

technossomy

9:18 am on Dec 30, 2004 (gmt 0)

10+ Year Member



Thanks for this and I will try that.