简体   繁体   English

使用ajax将数据从一个PHP页面传递到另一页面

[英]Pass data from one PHP page to another using ajax

Ok so I have one page, called albums.php, with the following code: 好的,所以我有一个页面,名为albums.php,其中包含以下代码:

<?php

    require_once('./config.php');

    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    $album = $mysqli->query("SELECT * FROM Albums");

    print('<div id="grid">');
    print('<ul>');
    while ($row = $album->fetch_assoc()) {
        $cover = $row['Album_Cover'];
        $name = $row['Album_Name'];
        $id = $row['Album_ID'];

        print ('<li>');
            print('<form method="POST" action="">');
                print("<input type='image' src=$cover name='image' id='image' class=$id>");
            print("</form>");
            print('<br/>');
            print ("$name");
        print ('</li>');
    }
    print('</ul>');
    print('</div>');

    print('<br/>');
    print('<br/>');
    print('<br/>');
    print('<br/>');

    $mysqli->close();

    ?>

DB_HOST, DB_USER, DB_PASSWORD, and DB_NAME come from config.php. DB_HOST,DB_USER,DB_PASSWORD和DB_NAME来自config.php。 albums.php (again, the code above) is linked to the script button.js, the code for which is below: albums.php(同样,上面的代码)链接到脚本button.js,其代码如下:

$("#image").click(function(e) {
    e.preventDefault();
    var id = $(this).attr('class');
    var dataString = 'id='+id;
    $.ajax({
        type: "POST",
        data: dataString,
        url: "./pictures.php",
        success: function(data) {
            alert(data);
        }
    });
});

My goal is to pass the id of the clicked image to pictures.php using ajax. 我的目标是使用Ajax将点击的图像的ID传递给pictures.php。 My code for pictures.php is as follows: 我的pictures.php代码如下:

<?php

require_once('./config.php');
require_once('./albums.php');

$id = $_POST['id'];

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$pictures = $mysqli->query("SELECT * FROM Pictures WHERE Album_ID = $id");

print('<div id="grid">');
print('<ul>');
while ($row = $pictures->fetch_assoc()) {
    $picture = $row['Picture'];

    print("<li><img src=$picture class='thumbnail'></li>"); 
}
print('</ul>');
print('</div>');

$mysqli->close();

?>

Do I also have to link the button.js script in pictures.php? 我还必须在pictures.php中链接button.js脚本吗? Other than that, I can't think of a possible issue with this code. 除此之外,我无法想到此代码可能存在的问题。 By the way, all three of these files are stored in the same folder on my server, so I believe I am accessing them correctly. 顺便说一句,所有这三个文件都存储在服务器上的同一文件夹中,因此我相信我可以正确访问它们。 Any help would be much appreciated! 任何帮助将非常感激!

Just to mention few things: 只说几件事:


  1. As mentioned in the comment Ids must be unique but it wont crash your code. 如评论中所述,Id必须是唯一的,但不会使您的代码崩溃。

  2. You must have put your js code before the php code that constructs image elements or you have to use jquery's $(document).ready(function(){//your code}) ; 您必须将js代码放在构造图像元素的php代码之前,否则必须使用jquery的$(document).ready(function(){//your code}) to make sure that you are registering the click event listener for the images. 以确保您正在注册图像的点击事件侦听器。

  3. On your js change: 在您的js更改:
    var dataString = 'id='+id; to var dataString = id; var dataString = id; and
    url: "./pictures.php" to url: "pictures.php" url: "./pictures.php"到网址: "pictures.php"

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

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