FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


including javascript in php file





menino
Is there a way to include javascript code in a PHP file?

I have the following code I want to embed in a php file, which was originally an HTML file :-

<script language="JavaScript" type="text/javascript">
<!--
function checkform ( form )
{

if (form.fname.value == "") {
alert( "Please enter your Name. الرجاء إدخال الإسم" );
form.fname.focus();
return false ;
}
else if (form.civilid.value == "") {
alert( "Please enter your Civil id. الرجاء إدخال الرقم المدني " );
form.civilid.focus();
return false ;
}

----end----of---code---
if anybody knows, please do respond.
menino
ok, I found the answer / workaround.

I basically copy the same javascript code, and place it in another file called formchk.js

and reference it from the php file, like so, using the following line of code :-

echo "<script language='JavaScript' src='Scripts/formchk.js' type='text/javascript'></script>";

simple and it works.

woohoooo Laughing
Hogwarts
Why not just end your PHP block?

I.e., the contents of index.html

Code:
<?php
function foobar()
return 'foo';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>Foo website</title>
  </head>
  <body>
      This is the website contents. You can include XHTML anywhere in a PHP file as long as it is outside of the PHP tags!
     <?php echo foobar(); ?>
  </body>
</html>

You can, effectively, include XHTML anywhere as long as you put a ?> before it and a <?php after it (to end and resume the parsing of PHP). Saying that, it's a bad practice to entwine your XHTML and PHP too much (All of your functions/objects/etc. should be defined in other files to your view/template/XHTML/whatever you wish to call it)
rvec
or use this:
Code:
echo <<<TEXT
<script language="JavaScript" type="text/javascript">
<!--
function checkform ( form )
{

if (form.fname.value == "") {
alert( "Please enter your Name. الرجاء إدخال الإسم" );
form.fname.focus();
return false ;
}
else if (form.civilid.value == "") {
alert( "Please enter your Civil id. الرجاء إدخال الرقم المدني " );
form.civilid.focus();
return false ;
}
TEXT;
tukun2009manit
menino wrote:
ok, I found the answer / workaround.

I basically copy the same javascript code, and place it in another file called formchk.js

and reference it from the php file, like so, using the following line of code :-

echo "<script language='JavaScript' src='Scripts/formchk.js' type='text/javascript'></script>";

simple and it works.

woohoooo Laughing


yes it should work let me try this code and i will reply
binsmyth
Well The good thing about php is you can include both html and the php codes in a same php file. The php just ignores the html or javascript code and processes the php code. Whenever a client opens the file or website only those html/javascript codes are shown in the browser rest is just processed by the server. Just try putting both codes in the same file and keep trying to get it right you will get the logic and idea for it.

Code:
<html>
<script .......>
<!--
function checkform(form)
{

}
-->
</script ........>

<?php
echo "PHP can be placed anywhere server will process it.";
?>

<form .....>

<?php
echo "PHP can be placed anywhere server will process it.";
?>
</form>

<?php
echo "PHP can be placed anywhere server will process it.";
?>
</html>



Yeah another way is also echo everything or put javascript in separate file and echo the link.
blueray
The other way to do that was work with some templating system.
Such as phpBB3 $template object, or smarty $smarty object.

Both of this allows you to separate base code with the layout of a page
that displayed on the screen.

phpBB3
Code:

1. <!-- INCLUDE your-file-goes-here -->


smarty
Code:

1. {include file="your-file-goes-here"}
2. {include file="your-file-goes-here" assign="js_script"}


But, for the simple thing as javascript file.
You need not to use these method.
Any ways mentioned above would work much better.
Related topics
Cron jobs - running a php file
[PHP doubt]Knowing the directory location of PHP file
is there any way to trigger a .php file without refreshing?
Uploading a .php file! NEED HELP thanks
How to create an advanced editor for PHPNUKE V. 7.9 - 8.0...
Layout Gap PRoblems with PHP/Javascript Combo
User Viewing A PHP File
send variable from php tp php file
Using Javascript to Encrypt Data then POST to PHP
Limiting php file functions with .htaccess
Parsing a file with Javascript?
Remote php file access and reading
Hide PHP file extension
[TuT]JOOMLA Installation Guide(Easiest Way)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.