doujuanxun7167 2013-12-03 15:17
浏览 32
已采纳

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

When clicking on a link I open a modal window, there is a drop down menu, an item is selected and then when submitted I need to know which link was clicked that opened the modal so I can insert the information into the database. The only problem I am having is knowing which link opened the modal. Here is some snipets of my code:

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 } ?>

The link <a href="addToPlaylists">+</a> is what is clicked to open this modal window:

<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 -->

This form then submits and this is the 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');
?>

The config.php file that is required has the session start as well as the pdo. So given this set up i need to get the Song_OID from the loop that has the #addToPlaylist link into the addTo.php so it can be inserted into the playlist. Any help will be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • drlma06060 2013-12-03 16:17
    关注

    Due to the fact that you also tagged this question as HTML5, you might be interested in using data attributes.

    This would allow you use the following 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>
    

    and you could use jQuery to grab the song's id

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

    This might need some improving, but might server you as a good staring point.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?