• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

How to : PHP inside Javascript

suraj4u

New Member
I have a .php file I want to add it as a .js file in HTML page


At the end of implementation I need code like this
<script type="javascript" src=".js "></script>

I have a php page but as I am using html pages I need to parse the php page as external and embed as a JS file .

Can you able to get it

eg : stats.php

How can I able to create a JS for stats.php
 
Why don't you just put the JS inside of the normal JS file?
You mean
PHP->JS1->JS2->HTML

Flow of opertaion
fwshelp.jpg


How to workout .. Anyone sample code plz
 
What is inside the stats.php file? Is it JS code?
If it is just JS code, put that inside stats.js and link to it like you have in the stats.html.
 
Stats.php : It a pure php file without javascript


example
<?php
//connection variables
.
.
.
$a=$_GET['a'];
$prodid=$_GET['prodid'];
$prodpb=$_GET['prodpb'];
$prodt1=$_GET['prodt1'];
$mname=$_GET['mname'];

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

if ($prodid=="nano")
{ $t1="nano";
} elseif($prodid=="shuffle")
{
$t1="shuffle";
} elseif($prodid=="classic")
{
$t1="classic";

} elseif($prodid=="touch")
{
$t1="touch";
}

if ($mname=="Amazon")
{ $t2="Amazon";
} elseif($mname=="Play.com")
{
$t2="Play.com";
} elseif($mname=="Comet")
{
$t2="Comet";

} elseif($mname=="PC World")
{
$t2="PC World";
} elseif($mname=="Apple Store")
{
$t2="Apple Store";
} elseif($mname=="M&S")
{
$t2="M&S";
}

switch ($a){
case 1:
if ($prodid!= NULL) {
?>
<table width="500" border="1" cellspacing="0" cellpadding="0">

.............
.
.

?>
 
Try looking up on $.get(). Should be able to load a php page from a javascript via that function.
 
It would seem you're going about it wrong.

If you really have to do it this way for some reason, then simply using a script tag pointing at php will not print the output of the page. However, what you could do is after the execution of your PHP code, I see you're drawing a table, instead of using HTML markup, use javascript statements such as document.createElement or document.write to build your table from the dynamic data. The only things that will be displayed will have to be the result of a javascript statement.
 
Cael : Sorry,I can't find solution

alternate solution plz

Why not do this;

In your PHP file, where you are putting your output, instead of outputting it, cache it all into a variable, then strip slashed. Finally add;
Code:
document.getElementById('statsDIV').innerHTML = <?=$phpVAR?>

in your HTML file, make a DIV where you want your stats to appear, and give it the ID of statsDIV;
Code:
<div id="statsDIV"></div>

You cannot actively load a PHP file into a HTML file using <script> it is impossible, Javascript is Client side programming, PHP is server side, you can have the PHP file generate javascript, then use the HTML file to load that javascript.

Otherwise, you can use server side includes (SSI) within your HTML to load the PHP file, it is a little bit complicated. Best solution is to do what I recommended before.
 
Back
Top