调用使用JQuery阿贾克斯PHP函数() [英] Calling PHP function using JQuery .ajax()

查看:130
本文介绍了调用使用JQuery阿贾克斯PHP函数()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个PHP函数读取图像的服务器目录。那么这些图像将需要使用阿贾克斯()用于幻灯片使用被传递回JQuery的。我使用PHP函数希望简化添加/为我的用户删除的图像。基本上,他们不得不做的就是添加或从目录中删除图像修改幻灯片,并没有任何编辑code。

I'm trying to use a PHP function to read a server directory for images. The images will then need to be passed back to JQuery using .ajax() for use in a slideshow. I'm using the PHP function hoping to simplify adding/removing images for my user. Basically, all they'd have to do is add or remove images from the directory to modify the slideshow and not edit any code.

我努力让jQuery函数返回我所期待的。它已经有一段时间,因为我做了PHP的,所以我可以俯瞰别人。这是我的PHP code:

I'm struggling to get the JQuery function to return what I'm expecting. It's been a while since I've done PHP so I may be overlooking someone. Here's my PHP code:

<?php
    $path = "./";
while (false !== ($file = readdir($handle)))
    {
        if ($file != '.' && $file != '..')
            echo "<img src=\"" . $path.$file. " alt=\"" . $file . "\>";
        {
    }
?>

下面是我的readImages jQuery函数:

Here's my readImages JQuery function:

function readImages() {
    $.ajax({
        type: "POST",
        url: "getimages.php",
        dataType: "html",
        success: function(imagepath){
            $(".slideshow").html(imagepath);
            }
    });

}

我想要做的就是让PHP返回的HTML格式标记的文件名添加。 jQuery的然后将返回的标签到HTML code。

What I'm trying to do is get the PHP to return an HTML formatted tag with the file name added. The JQuery then adds the returned tag to the HTML code.

到目前为止...所有它的返回是;?{}>

So far... all it's returning is "; { } ?>

任何想法,我做错了什么?

Any ideas as to what I'm doing wrong?

推荐答案

主要的问题是在博肯如果结构罗布·奥尔莫斯指出。但是,如果使用PHP5您选择我也切换到目录迭代器,而不是用readfile

The main issue is the borken if structure Rob Olmos pointed out. But i would also switch to directory iterator instead of readfile if youre using php5.

<?php
    $path = "./";
    $dir = new DirectoryIterator(realpath($path));
    $img = '<img src="%s" alt="%s" />';

    foreach($dir as $file){
      if(!$dir->isDot() && 0 !== strpos('.', $file->getFilename()) && !$file->isDir()){
        $filepath = $path . $file->getFilename();
        echo sprintf($img, $filepath, $filepath);
      }
    }
?>

这篇关于调用使用JQuery阿贾克斯PHP函数()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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