Current location: Hot Scripts Forums » Programming Languages » PHP » php wont work in external js file???


php wont work in external js file???

Reply
  #1 (permalink)  
Old 10-20-07, 09:36 PM
darksniperx darksniperx is offline
Newbie Coder
 
Join Date: Mar 2007
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
php wont work in external js file???

Code:
document.getElementById("pub_year_id").value = '<?php print date('Y'); ?>';
when the code is inside an html it works. when the code is placed in external js file, it wont work, it just prints <?php print date('Y'); ?> instead of the value it suppose to generate.

why???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-20-07, 11:18 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by darksniperx View Post
Code:
document.getElementById("pub_year_id").value = '<?php print date('Y'); ?>';
when the code is inside an html it works. when the code is placed in external js file, it wont work, it just prints <?php print date('Y'); ?> instead of the value it suppose to generate.

why???
It does that because an input field converts everything to text.
If you want to get it to output the current year you will have to
use Javascript instead of PHP.

A simple example:

HTML Code:
<input name="pub_year_id" id="pub_year_id">
<script src='date.js'></script>
And the contents of date.js:

date.js
Javascript Code:
  1. var d = new Date();
  2. var y = d.getYear();
  3. document.getElementById("pub_year_id").value = y;

There are other options to using Javascript.
You could have a variable set to the current year using PHP
in an external file and then include() the file in your page.

Like this:

External date.php file:

date.php
PHP Code:

$date date(Y); 

Your page:
PHP Code:

<?php
include "date.php";
?>
<input name="pub_year_id" value="<?php echo $date ?>">

-Or-

<?php
include "date.php";
echo 
'<input name="pub_year_id" value="'.$date.'">';
?>
__________________
Jerry Broughton

Last edited by job0107; 10-20-07 at 11:48 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-20-07, 11:25 PM
darksniperx darksniperx is offline
Newbie Coder
 
Join Date: Mar 2007
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
<?php
include "date.php";
?>
<input name="pub_year_id" value="<?php echo $date ?>">
that would not be the problem if I needed to set the date once and thats it, also I am not only using date php function, I have alot of other functions that do interactions between php and js, and they just started to get too big so I put them into a separate js file.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 10-20-07, 11:43 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
If you want to use the js in a function then you could do it like this:

date.js
Javascript Code:
  1. function get_year(id)
  2. {
  3.  var d = new Date();
  4.  var y = d.getYear();
  5.  document.getElementById(id).value = y;
  6.  }

Your page:
HTML Code:
<html>
<head>
<script src='date.js'></script>
</head>
<body>
<input name="pub_year_id" id="pub_year_id">
<input name="current_year" id="current_year">
<script>
get_year("pub_year_id");
get_year("current_year");
</script>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 10-20-07 at 11:46 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Freelance PHP Developer needed to work with Amberfly Ltd petebarr Job Offers & Assistance 3 07-03-09 01:23 PM
PHP displaying in .HTML file tennis4you PHP 6 12-31-06 09:17 PM
PHP - Unable to remove items from a file AdrianLewis PHP 5 03-17-06 06:14 AM
PHP multi-dimensional array sorting issue aqw PHP 2 06-25-05 12:09 AM
PHP File counter brambulkens PHP 7 05-14-05 07:20 AM


All times are GMT -5. The time now is 07:09 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.