使用jQuery .load()将参数传递给php函数并调用它

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

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

jQuery脚本所针对的php div。

 
<?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 "$file"; } } closedir($dir_handle); } thumbnailload($path); ?>

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

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

使用Javascript:

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

thumbs.php

 
$file"; } } closedir($dir_handle); } ?>

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

有用的链接

  • jQuery .load()
  • PHP.net – 来自外部来源的变量

我希望我有所帮助=]

将jQuery脚本更改为:

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

并将PHP代码更改为:

 
$file"; } } closedir($dir_handle); } thumbnailload($path); ?>

对于ref: http : //api.jquery.com/load/