如何在javascript中动态调用php函数 [英] How to dynamically call a php function in javascript

查看:94
本文介绍了如何在javascript中动态调用php函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数returnImageString(){
return <?php include'inc / image.functions.php'; echo getRandomImages(7);?>; //这不是动态的;这将始终返回相同的图像。我该如何解决?
}

然而,当页面加载时,php脚本被调用并且结果是添加到源代码是这样的:

  function returnImageString(){
returnimages / 20.11.A1B9.jpg |图像/ 8.14.47683.jpg |图像/ 19.10.FBB9.jpg |图像/ 21.12.9A.jpg |图像/ 8.11.1474937909.jpg |图像/ 8.15.99404.jpg |图像/ 8.10.jpg |; //这不是动态的;这将始终返回相同的图像。我该如何解决?
}

每当我调用js函数(returnImageString)时,我希望它每次都调用php函数(因为php函数返回一串随机图像位置),而不是在js函数中硬编码字符串。

有人指出我正确的方向?谢谢!

解决方案

这是不可能的,因为您将客户端行为与服务器端行为混​​合在一起。你需要做的是向服务器创建一个AJAX请求。



如果你使用的是类似
$ b

PHP代码 (也许是randomImages.php?)



  //查询所有图片
// $ _GET ['limit']会有图像
//的限制,因为我们从Javascript
传递它//将它们全部放入像这样的数组中:
$ images = array('images / 20.11.A1B9。 JPG, '图像/ 20.11.A1B9.jpg',...);
print json_encode($ images); //以JSON格式将它们返回给客户端。
出口;



客户端Javascript



 函数getRandomImages(limit){
//向服务器发出请求
$ .getJSON('/ path / to / randomImages.php',{limit:limit},function数据){
//数据现在是一个包含所有图像的数组
$ .each(data,function(i){
//对每张图片做一些操作
// data [i]将具有图像路径
});
});





$ b另外,如果图像的数量是有限的,你可以跳过所有这些疯狂只需要一个包含所有图像的数组,并从Javascript本身生成8个随机数。对于较小的数据集甚至更大的数据集,这可能会更好。


I have an index.php with the following js function:

function returnImageString() {
    return "<?php include 'inc/image.functions.php'; echo getRandomImages(7); ?>";        //This isn't dynamic; this will always return the same images.  How do I fix this?
}

However, when the page loads, the php script is called and the result is added to the source code like this:

function returnImageString() {
    return "images/20.11.A1B9.jpg|images/8.14.47683.jpg|images/19.10.FBB9.jpg|images/21.12.9A.jpg|images/8.11.1474937909.jpg|images/8.15.99404.jpg|images/8.10.jpg|"; //This isn't dynamic; this will always return the same images. How do I fix this?
 }

What I want to happen is whenever I call the js function (returnImageString), I want it to call the php function each time (since the php function returns a string of random image locations) instead of having the string hardcoded in the js function.

Can someone point me in the right direction? Thanks!

解决方案

This is not possible because you're mixing client-side behavior with server-side behavior. What you need to do is create an AJAX request to the server.

If you were using a library like jQuery (which you really want to, because it makes AJAX a breeze) you would do something like this:

PHP Code (maybe randomImages.php?)

// query for all images
// $_GET['limit'] will have the limit of images
// since we passed it from the Javascript
// put them all in an array like this:
$images = array('images/20.11.A1B9.jpg','images/20.11.A1B9.jpg',...);
print json_encode($images); // return them to the client in JSON format.
exit;

Client Side Javascript

function getRandomImages(limit) {
    // make request to the server
    $.getJSON('/path/to/randomImages.php', {limit: limit}, function(data) {
        // data is now an array with all the images
        $.each(data, function(i) {
            // do something with each image
            // data[i] will have the image path
        });
    });
}

Alternatively, if the amount of images is finite, you can skip all this crazyness and simply have an array with all the images and generate 8 random ones from the Javascript itself. This would probably be better for smaller data sets and even some bigger ones.

这篇关于如何在javascript中动态调用php函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆