Javascript 如何使用ajax将参数从html文件传递到php?

Javascript 如何使用ajax将参数从html文件传递到php?,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我有protfolio.html文件,我有一个带有类别的表格库。我想更新#gallery的内容,具体取决于使用ajax选择的类别。为了实现这一点,我有一个php文件,它扫描特定文件夹中与类别相关的图像并返回html,但我不知道如何将位置字符串传递给php脚本。 到目前为止,我有: index.html文件: <li><a href="javascript:void(0);" onclick="getImages("/images/portfolio/gallery/*/")"&

我有protfolio.html文件,我有一个带有类别的表格库。我想更新#gallery的内容,具体取决于使用ajax选择的类别。为了实现这一点,我有一个php文件,它扫描特定文件夹中与类别相关的图像并返回html,但我不知道如何将位置字符串传递给php脚本。 到目前为止,我有:

index.html文件:

<li><a href="javascript:void(0);" onclick="getImages("/images/portfolio/gallery/*/")"><h5>view all</h5></a></li>
<li><a href="javascript:void(0);" onclick="getImages("/images/portfolio/gallery/webdesign/")"><h5>webdesign</h5></a></li>
php文件:

# To prevent browser error output

# Path to image folder
$imageFolder = $_POST["location"];

# Show only these file types from the image folder
$imageTypes = '{*.jpg,*.JPG,*.jpeg,*.JPEG,*.png,*.PNG,*.gif,*.GIF}';

# Set to true if you prefer sorting images by name
# If set to false, images will be sorted by date
$sortByImageName = false;

# Set to false if you want the oldest images to appear first
# This is only used if images are sorted by date (see above)
$newestImagesFirst = true;

# The rest of the code is technical
# Add images to array
$images = glob($imageFolder . $imageTypes, GLOB_BRACE);

# Sort images
if ($sortByImageName) 
{
    $sortedImages = $images;
    natsort($sortedImages);
} 
else 
{
    # Sort the images based on its 'last modified' time stamp
    $sortedImages = array();

    $count = count($images);

    for ($i = 0; $i < $count; $i++) 
    {
        $sortedImages[date('YmdHis', filemtime($images[$i])) . $i] = $images[$i];
    }

    # Sort images in array
    if ($newestImagesFirst) 
    {
        krsort($sortedImages);
    } 
    else 
    {
        ksort($sortedImages);
    }
}

$count = count($images);

for($i=1;$i<=$count; $i++)
{
    $
}

# Generate the HTML output
writeHtml('<ul class="ins-imgs">');

$count=1;

foreach ($sortedImages as $image) {

    # Get the name of the image, stripped from image folder path and file type extension

    # Get the 'last modified' time stamp, make it human readable

    # Begin adding
    if ($count==1 || $count%3==0)
    {
        writeHtml('<tr>');
    }

    writeHtml('<td>');
    writeHtml('<a  href="' . $image .'" data-lightbox="all" ><img src="' . $image .'" alt=""/></a>');
    writeHtml('</td>');

    if ($count==1 || $count%3==0)
    {
        writeHtml('</tr>');
    }

    $count++;
}

function writeHtml($html) 
{
    echo "document.write('" . $html . "');\n";
}
#防止浏览器错误输出
#图像文件夹的路径
$imageFolder=$_POST[“位置”];
#仅显示图像文件夹中的这些文件类型
$imageTypes='{*.jpg、*.jpg、*.jpeg、*.jpeg、*.jpeg、*.png、*.png、*.gif、*.gif}';
#如果希望按名称对图像进行排序,请设置为true
#如果设置为false,图像将按日期排序
$sortByImageName=false;
#如果要首先显示最旧的图像,请设置为false
#仅当图像按日期排序时才使用此选项(见上文)
$newestImagesFirst=true;
#代码的其余部分是技术性的
#将图像添加到阵列
$images=glob($imageFolder.$imageTypes,glob_括号);
#对图像排序
if($sortByImageName)
{
$sortedImages=$images;
natsort($dimages);
} 
其他的
{
#根据“上次修改”的时间戳对图像进行排序
$sortedImages=array();
$count=计数($images);
对于($i=0;$i<$count;$i++)
{
$sortedImages[date('YmdHis',filemtime($images[$i])。$i]=$images[$i];
}
#按数组对图像排序
如果($newestImagesFirst)
{
krsort($Dimages);
} 
其他的
{
ksort($15分钟);
}
}
$count=计数($images);

对于($i=1;$i您需要发送
键/值
对,而不仅仅是

尝试:

然后在php中使用
$\u POST['location']
接收


在浏览器控制台的“网络”选项卡中,检查实际请求,您将准确地看到发送的内容。这应该始终是解决ajax问题的第一点…检查完整请求传递位置的最简单方法:

function getImages(location)
{
    $.ajax({
        type: 'POST',
        url: 'loadImages.php',
        data: location, 
        success: function(data) {
            $('#gallery').html(data);
        }
    });
}
function getImages(location)
{
  $.ajax({
    type: "GET",
    url: 'loadImages.php?location='+location, // simply add as query string
    success: function(data) {
        $('#gallery').html(data);
    }
  });
}
然后在PHP文件中:

# Path to image folder
$imageFolder = $_GET["location"];

你的PHP希望你的数据是怎样的?如果你的PHP文件需要POST数据,那么你为什么要使用GET?我不知道,我认为它可能会工作,因为GET工作得不太好,我同意@amura.cxg。他发布的链接应该会给你答案。
类型
是“GET”。因此他会以$\u GET['location]的形式接收它@OmarRelawady..是的,最初尝试的post和现在设置为Get的type之间存在冲突可能我传递位置的方式不对?每个教程都使用表单,但我尝试传递一个href=argument..@TomaszSklepowicz您也不能使用
文档。加载页面后编写
。它将清除整个页面中的所有内容。哟您需要返回完整的html作为响应…在您的浏览器控制台中检查请求!!当我单击链接
  • 时,我在此行中得到意外的令牌}错误。是否有不同的方法传递位置?当我单击链接
  • 时,我在此行中得到意外的令牌}错误。是否有不同的方法传递位置?–
    function getImages(location)
    {
      $.ajax({
        type: "GET",
        url: 'loadImages.php?location='+location, // simply add as query string
        success: function(data) {
            $('#gallery').html(data);
        }
      });
    }
    
    # Path to image folder
    $imageFolder = $_GET["location"];