...

View Full Version : Including PHP within Javascript code



martinjsmith
07-24-2002, 03:35 PM
Is is possible to include PHP code within a section of Javascript? The code below shows that I am trying to include the PHP echo command in the following javascript function but it does not work.

Many thanks,

Martin


<script language="JavaScript">
function echoSession(num)
{
window.document.newForm.mybox.value=num;
<?PHP echo("string"); ?>
}
</script>

Spookster
07-24-2002, 03:58 PM
It worked just fine for me. You are running this in a .php file right?

martinjsmith
07-24-2002, 04:43 PM
Yes, its a .php file. The code I am using is in this format but doesnt seem to echo anything to the screen.

<html>

<script language="JavaScript">
function echoSession(num)
{
window.document.newForm.mybox.value=num;
<?PHP echo("string"); ?>
}
</script>


<?PHP
require("functions.php");
adminHeader($cookie);
makeConnection();

$numSessions=1;

echo("<INPUT TYPE=BUTTON onClick=\"echoSession(".$numSessions.");\" NAME='addSession' VALUE='Add a session!'>");

?>

</html>

Spookster
07-24-2002, 05:55 PM
The echo command doesn't necessarily mean print it to the screen. It just echos it to the file it's in. In the case of your first post it echoed it within the javascript. This is the output of that:

<script language="JavaScript">
function echoSession(num)
{
window.document.newForm.mybox.value=num;
string
}
</script>


As for your second post.....it works just fine as well. Here is the output of that:

<script language="JavaScript">
function echoSession(num)
{
window.document.newForm.mybox.value=num;
string
}
</script>


<INPUT TYPE=BUTTON onClick="echoSession(1);" NAME='addSession' VALUE='Add a session!'>



Note that if you do not put that input tag inside a form then it will not show up in Netscape4.x

martinjsmith
07-25-2002, 04:44 PM
Thanks, that makes sense now but what I really wanted to do all along is EXECUTE A PHP FUNCTION on the click of the 'addSession' button. As you saw before I tried to execute a javascript function on clicking the button and then using PHP within that javascript function. Based on your previous post I dont think that will work. Do you know of any way to execute a PHP function (say, phpFunction($num) below) on the click of a button?

Many thanks,

Martin


<html>

<script language="JavaScript">
function echoSession(num)
{
window.document.newForm.mybox.value=num;
<?PHP echo("string"); ?>
}
</script>


<?PHP
require("functions.php");
adminHeader($cookie);
makeConnection();

function phpFunction($num)
{
echo("<TR><TD><SELECT NAME='signUpListDay[$num]'>");
echoDays();
echo("</SELECT></TD>");
echo("<TD><SELECT NAME='signUpListTime[$num]'>");
echoTimes();
echo("</SELECT></TD>");
echo("<TD><INPUT TYPE=TEXT NAME='signUpListLocation[$num]' SIZE=40></TD>");
echo("<TD><INPUT TYPE=TEXT NAME='signUpListWeeks[$num]' SIZE=15></TD>");
echo("<TD><a href='#' onClick=\"window.document.newForm.elements['signUpListDay[$num]'].value='';
window.document.newForm.elements['signUpListTime[$num]'].value='';
window.document.newForm.elements['signUpListLocation[$num]'].value='';
window.document.newForm.elements['signUpListWeeks[$num]'].value='';\">Reset</a></TD></TR>");
}

$numSessions=1;

echo("<INPUT TYPE=BUTTON onClick=\"echoSession(".$numSessions.");\" NAME='addSession' VALUE='Add a session!'>");

?>

</html>

mordred
07-25-2002, 06:42 PM
To understand why your approach is not working, it's really important to remember that JavaScript runs on the client and PHP on the server. As Spookster has already said above, when PHP is done with processing a file, it sends the output as a HTTP response back to the client. So generally, these two technologies can't be interoperable, since they don't run in the same environment.

Nevertheless, you can trigger PHP scripts through JavaScript by doing a simple operation that includes a GET request:

var testImg = new Image();
testImg.src = "http://adomain.com/test.php";

This will run the script on the server, but you don't get any output aside from image information back to the client.

With a load of DHTML and a small PHP backend you can achieve something called Remote Scripting. Heres a good resource (http://ashleyit.com/rs/) that contains detailed info about how to use this method.

For your script, it's purpose is not quite clear to me. I would favor a different approach that involves a refresh of the page, so when you click the button a new page gets loaded with the appropriate new output derived from the PHP function.

Hope that cleared things up. :)

firepages
07-25-2002, 06:43 PM
PHP is parsed serverside and your button (& associated script) is clientside so basically you can not do that.

All PHP code is parsed before the HTML & Script ever gets to the users browser so no direct interaction is possible.

(you can use hidden frames etc to emulate this but its really a pain)

martinjsmith
07-25-2002, 09:54 PM
Many thanks guys, I now understand the client/server side reasoning why it isnt possible. Im now starting work on a way to do it by refreshing the page as suggested by Mordred.

Thanks again,

Martin

firepages
07-26-2002, 05:05 AM
doh ! - did not see your post there Mordred which explained it better than mine - you beat me by 1 minute... but I reckon it was closer to 30 secods or so :p

sgwd
11-25-2005, 08:58 PM
Hello!
How to call to a function in javascript sending a numerical parameter? This parameter will be used in php code inside javascript:

call the javascript function:
<A href='javascript:void(0)' onClick='ShowFAQ(25)'>text</A>

<script type="text/javascript">
function ShowFAQ(faqid){
window.document.labeltitlefaq.innerText='<?php echo($arrayFAQtitle[faqid])?>' ;
}
</script>

The previous code does not work, and if I replace "faqid" to a number, it works fine.

Thanks for your support

Velox Letum
11-25-2005, 09:44 PM
Besides this topic being three years old, the reason was explained above. PHP is a server-side language. This means that when you load the page, the webserver runs the code through PHP, then PHP sends output back, and the webserver sends it to you, thus using javascript (a client-side language) to call PHP code will not work, as the code will have already been removed and the output in its place. You could load the faq elements into a javascript array with PHP, which would mean the data persisted in the form of javascript. Or if you wanted to load it on demand, you could look into XMLHTTP Requests.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum