开发者

Pass from PHP to Javascript

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"></script>

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. Plea开发者_如何学编程se 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>';


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


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"/>


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.


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.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜