Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript line in php page

Status
Not open for further replies.

Steve-vfp9user

Programmer
Feb 5, 2013
336
GB
Hi guys

I am struggling to find the find the syntax for what I am trying to achieve. Any way I try it I cant get it to show the records.

I am trying to show this on one of my pages:

Code:
<script language="javascript" src="[URL unfurl="true"]http://www.mysite.com/myreviews/mypage.php?cid=2"></script>[/URL]

The above displays a list of reviews from a MySQL table.

I found the command:

Code:
<?php echo 'Test'; ?>
(which shows the word Test... obviously!)

When I try to add the line of code as above I either get parsing errors or just nothing.

I wouldn't normally ask for a sollution but I'm pulling my hair out with this one and still learning.

Any assistance would be most beneficial.

Thank you

Steve
 
You are probably having a problem with unescaped characters. See the PHP manual:

Try:
Code:
print <<<END
<script language="javascript" src="[URL unfurl="true"]http://www.mysite.com/myreviews/mypage.php?cid=2"></script>[/URL]
END;
 
Or:
Code:
echo '<script language=\"javascript\" src=\"[URL unfurl="true"]http://www.mysite.com/myreviews/mypage.php?cid=2\"></script>';[/URL]
 
Hi

Many thanks for the quick response.

I tried the first example but that didn't show any data and the second example threw up a parsing error.

Please allow me to show you what is before and after where I want to insert the code:

Code:
<div class="">
<?php echo TEXT_REVIEWSOTHER; ?>
<?php echo 'Reviews'; ?>

[b][b]I need the[/b] <script language="javascript" src="[URL unfurl="true"]http://www.mysite.com/myreviews/mypage.php?cid=2"></script>[/URL] [b]here[/b][/b]
		    
</div>

Thank you

Steve
 
The line...
Code:
I need the <script language="javascript" src="[URL unfurl="true"]http://www.mysite.com/myreviews/mypage.php?cid=2"></script>[/URL] here
...is outside of any PHP tags so it is not being parsed by PHP in the page where you are embedding the javascript. There should not be any problem, unless the output of is not javascript.

What is the output of this mypage.php file? Is it javascript?
 
1. As spamjim mentioned: What is the output of
Unless it returns fully qualified JS functions that are also being run. That will do nothing. As you can't include a PHP page using javascript tags.

2. Even if the code is fully qualified JS, it still need to be run, some some type of JS function call needs to take place either inside the file or just after it.

3. What exactly are you trying to accomplish?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thank you for the posts.

The line of code displays a list of reviews from a MySQL table. I'm thinking here that it may have to go between HTML but as the page is php that was my logic.

Let me give that a go first and I will post back.

Still learning, thanks for your help so far guys.

Steve
 
so the line of code results in an output of html? NOT javascript?

if so you want this

Code:
<?php echo file_get_contents('[URL unfurl="true"]http://mysite/etc');?>[/URL]

assuming that the site is on a separate server/host. if not then you can just 'include' the file and reference its variables as normal.
 
Hi jpadie

Thank you

The site is on the same server so where do I put the 'include'?

Steve
 
just run the script from your page.

include 'path/to/script.php';
this will bring the functions of that script into the current scope.
you can then reference its functions and variables normally.

 
When I add the following:

Code:
include '[URL unfurl="true"]http://www.mysite.com/myreviews/mypage.php?cid=2;[/URL]

All I see on the web page is:


I have placed it in between:

Code:
<div class="contentContainer">
  <div class="contentPadd txtPage">
  <div class="">
    <?php echo TEXT_REVIEWMAIN; ?>

    include '[URL unfurl="true"]http://www.mysite.com/myreviews/mypage.php?cid=2;[/URL]
	
    </div>
</div>

Steve
 
Wrap it in PHP

Code:
<div class="contentContainer">
  <div class="contentPadd txtPage">
  <div class="">
    <?php 
       echo TEXT_REVIEWMAIN;
       include '/myreviews/mypage.php?cid=2';
   ?>
    </div>
</div>
 
Hi

Unfortunately it is still showing:

include '/myreviews/mypage.php?cid=2';

on the actual page.

I think I will have to look at alternaitve way or set up.

Thank you for your time guys

Steve
 
Does the page containing the PHP include end in PHP?

PHP runs on the server, as such server configurations indicate that a page that ends in .php will get executed trough the PHP parser.

If your page ends in HTML then the include will do nothing as the PHP will not get run unless your server is configured to parse HTML files through the PHP parser.

In any case, the most basic approach would be to use an iframe for this if your actual page is an HTML page.

Code:
<iframe src="[URL unfurl="true"]http://www.mysite.com/myreviews/mypage.php?cid=2"></iframe>[/URL]

This should technically load and run your PHP page inside a small frame in your base HTML page.

Alternatively using the <object> tag may also work:

Code:
<object width="50%" height="50%" type="text/html" data="[URL unfurl="true"]http://localhost/Sitios/toppings.php">[/URL]
</object>




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Unfortunately it is still showing:

include '/myreviews/mypage.php?cid=2';

on the actual page.

That is because you do not have <?php preceding it and ?> following it. You need to tell your server to recognize this as PHP.

Code:
<?php
include '/myreviews/mypage.php?cid=2';
?>
 
Hey

Thanks for the updates. I need to get my head around this so tomorrow is another day which I'll have a look and let you know my findings.

Appreciate your time

Steve
 
spamjim said:
That is because you do not have <?php preceding it and ?> following it. You need to tell your server to recognize this as PHP.
You are very correct.


The php tags end right before the include: I just never noticed the closing tag so i though it was still opened for it.

Code:
 [b][COLOR=#A40000]<?php[/color][/b] echo TEXT_REVIEWMAIN; [b][COLOR=#A40000]?>[/color][/b]

    include '[URL unfurl="true"]http://www.mysite.com/myreviews/mypage.php?cid=2;[/URL]

it should be:

Code:
 [b][COLOR=#A40000]<?php[/color][/b] echo TEXT_REVIEWMAIN; 

    include '[URL unfurl="true"]http://www.mysite.com/myreviews/mypage.php?cid=2;[/URL]

[b][COLOR=#A40000]?>[/color][/b]





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hello Phil, Steve, world.
Yes I'm a newbie, too and a past VFP/Clipper Guru.
PLEASE, let me know if this is the Answer.

I agree with Phils' last msg above, but I noticed the "Ending single quote was MIA".
I have searched on the found a quote below.

include "myfile.php?var=123"; will not work. PHP searches for a file with this exact name and does not parse the parameter
include " does work. PHP regards this as a regular URL and parses the parameters.

 
consider this situation: I want a main script to 'call' another script and output some information.

Let's say the script to be called looks like this

Code:
$var = $_GET['myvar'];
echo "hello $var";


and let's say the main script looks like this

Code:
echo 'This is my site <br/>';
/* include goes here */

there are a range of options open to us

Code:
echo 'This is my site <br/>';
include '[URL unfurl="true"]http://mydomain.php/includeFile.php?myvar=jpadie';[/URL]

Code:
echo 'This is my site';
$_GET['myvar'] = 'jpadie'; //bad practice as we reassign superglobals
include 'includeFile.php';

Code:
/* rewrite includeFile */
$var = isset($myvar) ? $myvar : $_GET['myvar']; //check to see whether myvar already exists in the scope
echo "hello $var";
/* end rewrite */

/* main file */
echo 'This is my site';
$myvar = 'jpadie';
include 'includeFile.php'; //ok practice

Code:
/* rewrite includeFile */
function sayHi($myvar){
 echo "hello $myvar";
}
/* end rewrite */

/* main file */
echo 'This is my site';
$myvar = 'jpadie';
include_once 'includeFile.php'; 
sayHi($myvar); //most modular practice

Essentially, imo, it is seldom a good idea to 'include' a file over http. there are use cases that make this a good choice (for example consuming web services) but in normal usage, when you have control over all the scripts, it is better to change your design pattern so that you are using file operations rather than tcp.

There is _always_ the question of scope to consider. If you are unclear on how global and functional scopes differ then this would be a great time to step back and re-read the relevant manual sections. Even after many years programming you can still get tripped up by scope - this very morning I spent 30 minutes debugging some javascript where I had assumed that some variables would be in scope, and they were not - hence aberrant results. Turns out to be some closures that were missing. Anyway, goes to show that you need to pay attention to scope.
 
I'd have to agree with jpadie above.

There's should be very few instances when you would actually need to include a file over an HTTP request with a querystring parameter.

In most cases the included file will have access to the superglobals available to the script that is including the file. In which case passing a querystrng parameter would be redundant.

The only reason I can see to do that is when the script produces output on its own based on the querystring parameter. And that is something I would not recommend you do anyway.

Its better to call the content producing functions from your main script rather than having the include file run on its own, as there can be issues with premature output in many cases, and you get also more overhead if you need to include the file more than once to do different things for different parameters.

Its instead better practice to include the file once and use its functions when you need them with the appropriate parameters passed to them.

If you need the script to run on its own for whatever reason, I'd use an iframe or an object tag get it to run from the server rather than including it into another PHP script.







----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top