使用jQuery .load()将参数传递给并调用php函数 [英] Using jQuery .load() to pass a parameter to and call a php function

查看:180
本文介绍了使用jQuery .load()将参数传递给并调用php函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我拥有的jQuery .load函数.我想做的是使用jQuery传递的值运行php函数.我想我只是不明白如何将jQuery的值传递给服务器.我可能需要使用.get或.post吗?请告知.

This is the jQuery .load function I have. What I want to do is run the php function with a passed value from the jQuery. I think I just don't understand how to pass values with jQuery to the server. Do I perhaps need to use .get or .post? Please advise.

$(document).ready(function(){
  $("#scotland").click(function() {
    $("#Main").load('thumbs.php #Main', $path=./scotland);
  });
});

jQuery脚本定位的

php div.

php div that the jQuery script targets.

<div id="main">
<?php
function thumbnailload($path)
{
$dir_handle = @opendir($path) or die("Unable to open folder");
while (false !== ($file = readdir($dir_handle))) {

if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $file)){
echo "<a href='$path/$file'><img class='thumbnail' src='$path/thumbs/thumb_$file' alt='$file'></a>";
}
}
closedir($dir_handle);
}
thumbnailload($path);
?>
</div>

推荐答案

这种问题通常很早,不知道如何与客户端语言(JavaScript)交互到服务器端(PHP) ).但是,我们走吧!

It is usually in early to have this kind of problem, not knowing how to interact with a client-side language (JavaScript) to a server-side (PHP). But let's go!

对于"thumbs.php"页面接收参数,您必须通过GET或POST发送数据.

For the page "thumbs.php" receives a parameter, you must send the data via GET or POST.

JavaScript:

Javascript:

$(document).ready(function(){
    $("#scotland").click(function() {
        $("#Main").load('thumbs.php', {'path': 'scotland'});
    });
});

thumbs.php

thumbs.php

<div id="main">
    <?php
        if(!empty($_REQUEST['path'])) { // i used $_REQUEST because it receives the data from POST or GET.
            $path = $_REQUEST['path'];
            $dir_handle = @opendir($path) or die("Unable to open folder");
            while (false !== ($file = readdir($dir_handle))) {
                if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $file)) {
                    echo "<a href='$path/$file'><img class='thumbnail' src='$path/thumbs/thumb_$file' alt='$file'></a>";
                }
            }
            closedir($dir_handle);
        } 
    ?>
</div>

如果未传递数据,则jquery中的.load()方法使用GET请求类型.在我们的例子中,我正在传递数据({'path':'scotland'}),因此请求的类型为POST.

The method .load() in jquery uses a GET request type if the data is not passed. In our case, I am passing data ( {'path': 'scotland'} ), so the request is of type POST.

有用的链接:

  • jQuery .load()
  • PHP.net - Variables From External Sources

我希望我能帮上忙=]

I hope I was helpful =]

这篇关于使用jQuery .load()将参数传递给并调用php函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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