views:

93

answers:

5

Hi All,

I have a little problem here, and no tutorials have been of help, since I couldn't find one that was directed at this specific problem.

I have 2 hosting accounts, one on a server that supports PHP. And the other on a different server that does not support PHP.

SERVER A = PHP Support, and SERVER B = NO PHP Support.

On server a I have a php script that generates a random image. And On server b, i have a html file that includes a javascript that calls that php function on server a. But no matter how I do it, it never works.

I have the following code to retrieve the result from the php script:

<script language="javascript" src="http://www.mysite.com/folder/file.php"&gt;&lt;/script&gt;

I know I'm probably missing something, but I've been looking for weeks! But haven't found any information that could explain how this is done. Please help!

Thank you :)

UPDATE

The PHP script is:

$theimgs= array ("images/logo.png", "images/logo.png", "images/logo.png", "images/logo.png", "images/logo.png");

function doitnow ( $imgs) {
    $total = count($imgs);
    $call = rand(0,$total-2);
    return $imgs[$call];
}

echo '<a href="index.php" alt="something"><img src="'.doitnow($theimgs).'" alt="something" /></a>';
+1  A: 

<img src="http://mysite.com/folder/file.php" alt="" /> ?

Māris Kiseļovs
That doesn't work, sorry. I'm going to update my question to include the PHP script, too.
lucifer
+1  A: 

You may be using outdated sources to learn, since the language attribute is deprecated and you should use type="text/javascript" instead. It's also not clear what kind of output does the .php script produce. If it's image data, why are you trying to load it as a script and not an image (i.e., with the <img> tag)?

Update: The script is returning HTML, which means it should be loaded using Ajax, but you can't do that if it's on a different domain due to the same origin policy. The reason nothing is working now is that scripts loaded using the <script> tag aren't interpreted as HTML. To pass data between servers, you should try JSONP instead.

Reinis I.
I have also tried text/javascript, however I didn't mention is as the very last tutorial I read had "language", so I simply used what I last used. But thank you anyway, I did not know it was deprecated. :)
lucifer
Thank you, I have tried the <img> tag but without success. I have updated my question to include PHP script I am using. Thank yo so much for helping. I don't know what I am doing wrong
lucifer
I've updated my answer.
Reinis I.
+1  A: 

It's not clear, why you include a PHP file as JavaScript. But try following:

  1. Modify your PHP Script so that it returns a image file directly. I'll call that script image.php. For further information, look for the PHP function: header('Content-type: image/jpeg')
  2. In your JavaScript file use image.php as you would any normal image.
  3. Include the JavaScript on server B as a *.js file.

UPDATE:

It's still not clear, why you need JavaScript.

Try as image.php:

$theimgs= array ("images/logo.png", "images/logo.png", "images/logo.png", "images/logo.png", "images/logo.png");

function doitnow ( $imgs) {
    $total = count($imgs);
    $call = rand(0,$total-2);
    return $imgs[$call];
}

$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/" . doitnow($theimgs));

And on server b:

<img src="www.example.org/image.php"/>
Christian Strempfer
Thank yo for your help and effort, however it didn't work. It just displays the tiny red X box instead of images, like all the other things i've tried. Even though the images do exist.
lucifer
Christian Strempfer
+1  A: 

It seems that server A generates an HTML link to a random image (not an image). The URL is relative to wherever you insert it:

<a href="index.php" alt="something"><img src="images/logo.png" alt="something" /></a>

That means that you have an images subdirectory everywhere you are using the picture. If not, please adjust the URL accordingly. Forget about JavaScript, PHP or AJAX: this is just good old HTML.


Update

The PHP Script displays pics randomly. Pics are hosted on server A, and they are indeed accessible and readable from the internet. The PHP Script has been tested by itself, and works.

If these statements are true, Māris Kiseļovs' answer should work. So either your description of the problem is inaccurate or you didn't understand the answer...

Álvaro G. Vicario
If I use the above code, a random image is not generated and displayed on the page. What I need to do is generate a random image (which I can do) on the server that supports PHP, and then get that image that was generated with Javascript on a server that does not support server-side languages.
lucifer
The above code is the *output* of the code you posted. Of course it's not random; to begin with, the images to pick from are all the same. I'm sorry but it's hard to help you when you don't appear to understand any single part of your own code. Seriously, you should improve your basic HTML skills before attempting complex programming.
Álvaro G. Vicario
+1  A: 

You didn't specify, but I assume the two servers have different domain/hostnames. You may be running into a browser security model problem (same origin policy).

If that's the case, you need to use JSONP.

Paul Schreiber
Thank you; I will check this out right now :D
lucifer
You're just throwing in some buzzwords. Why do you think it might be a security problem? JSONP will add more complexity, which he can't handle at the moment.
Christian Strempfer
"JSONP will add more complexity, which he can't handle at the moment." - Indeed lol :(
lucifer
No offense meant. But it will add unnecessary complexity and I think there is an easier solution.
Christian Strempfer
-Why do you think it might be a security problem? While he didn't specify, it's likely the two servers have different domain/hostnames.
Paul Schreiber