简体   繁体   English

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

[英]how to pass parameter to php from html file using ajax?

I have protfolio.html file and i have a table #gallery with categories in it. 我有protfolio.html文件,并且有一个带有类别的表#gallery。 I want to update content of the #gallery, depending on chosen category using ajax. 我想根据使用ajax选择的类别来更新#gallery的内容。 To achieve this I have php file which scans specific folder with the images relevant to category and returns html, however I don't know how to pass the location string to php script. 为此,我有一个php文件,该文件会扫描与类别相关的图像的特定文件夹并返回html,但是我不知道如何将位置字符串传递给php脚本。 So far, i have this: 到目前为止,我有这个:

index.html file: 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>

Script 脚本

function getImages(location)
{
    $.ajax({
        type: "GET",
        url: 'loadImages.php',
        data: location, 
        success: function(data) {
            $('#gallery').html(data);
        }
    });
}

php file: 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";
}

You need to send key/value pairs, not just value as you are doing: 您需要发送key/value对,而不仅仅是value ,你正在做的:

Try: 尝试:

$.ajax({
    .....
         data: {location : location},
    .....

});

then in php receive with $_POST['location'] . 然后在php中接收$_POST['location']

In browser console network tab, inspect the actual request and you will see exactly what is sent. 在浏览器控制台的“网络”选项卡中,检查实际的请求,您将确切看到发送的内容。 This should always be your first point of troubleshooting ajax... inspecting the full request 这应该始终是对ajax进行故障排除的第一步...检查完整的请求

function getImages(location)
{
    $.ajax({
        type: 'POST',
        url: 'loadImages.php',
        data: location, 
        success: function(data) {
            $('#gallery').html(data);
        }
    });
}

Simplest way to pass location: 通过位置的最简单方法:

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

Then in your PHP file: 然后在您的PHP文件中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM