知道点击了什么链接添加到数据库

时间:2013-12-03 15:17:01

标签: javascript php sql html5

当点击一个链接时,我打开一个模态窗口,有一个下拉菜单,一个项目被选中,然后在提交时我需要知道哪个链接被点击打开了模态,所以我可以将信息插入数据库。我唯一的问题是知道哪个链接打开了模态。以下是我的代码的一些snipets:

collections.php:

<?php
$query = 'SELECT Song.Song_OID As Song_OID, Song.Title As Song,
    Album.Album_OID As Album_OID, Album.Title As Album,
    Artist.Artist_OID As Artist_OID, Artist.Name As Artist
    From Song
    Left JOIN Artist_has_Song ON Artist_has_Song.Song_Song_OID = Song.Song_OID
    Left JOIN Artist ON Artist.Artist_OID = Artist_has_Song.Artist_Artist_OID
    Left JOIN Album_has_Song ON Album_has_Song.Song_Song_OID = Song.Song_OID
    Left JOIN Album ON Album.Album_OID = Album_has_Song.Album_Album_OID
    ORDER BY Song, Artist IS NULL, Artist, Album IS NULL, Album';
$statement = $pdo->prepare($query);
$statement->execute();
while ($row = $statement->fetch(PDO:: FETCH_ASSOC)) { ?>
<tr>
    <td><a href="#addToPlaylist">+</a></td>
</tr>
<?php } ?>

链接<a href="addToPlaylists">+</a>是点击打开此模态窗口的内容:

<div id="addToPlaylist">
    <div class="hidden">
        <a href="#close" title="Close" class="close">X</a>
        <form action="addTo.php" method="post">
            <p>Add To:</p>
            <select name="userPlaylists">
            <?php
            $query = 'SELECT Name, Playlist_OID FROM Playlist WHERE User_User_OID=?';
            $statement = $pdo->prepare($query);
            $statement->execute(array($_SESSION['User_OID']));
            while ($row = $statement->fetch(PDO:: FETCH_ASSOC)) {
            ?>
                <option value="<?php echo $row['Playlist_OID']; ?>">
                    <?php echo $row['Name']; ?></option>
            <?php } ?>
            </select>
            <input type="submit" value="Add"/>
        </form>
    </div><!-- end hidden -->   
</div><!-- end addToPlaylist -->

然后提交此表单,这是addTo.php:

<?php
require ('../private_html/config.php');
$playlist = $_POST['userPlaylists'];
$song = $_POST['< the part i dont know >'];

$query = 'INSERT INTO Playlist_has_Song (Song_Song_OID, 
            Playlist_Playlist_OID)
            VALUES (?, ?)';
$statement = $pdo->prepare($query);
$statement->execute(array($playlist, $song));   
header('Refresh: 0; URL=collections.php');
?>

所需的config.php文件具有会话启动以及pdo。因此,在这个设置中,我需要从具有#addToPlaylist链接的循环中获取Song_OID到addTo.php中,以便可以将其插入到播放列表中。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

由于您还将此问题标记为HTML5,因此您可能有兴趣使用data attributes

这将允许您使用以下HTML:

<tr>
    <td>
        <!-- class song-picker is used to allow for easier element selection -->
        <a href="#addToPlaylist" class="song-picker" data-song-id="<?php echo $row['Song_OID']; ?>">+</a>
    </td>
</tr>

你可以使用jQuery来获取歌曲的id

var songID;
$('.song-picker').on('click', function (e) {
    songID = $(this).data('song-id');
});

这可能需要一些改进,但可能会为您提供良好的服务。