【问题标题】:How to pass Span id value through href to another page如何通过href将Span id值传递到另一个页面
【发布时间】:2020-09-07 14:35:19
【问题描述】:

我正在尝试将 song_id 传递给模态,然后通过 href 将从模态中的 song_id 获得的值传递到另一个页面。 song_id 值存储在 span id 中,然后我想通过存储在 php 变量中的 href 传递 span id 值并显示在下一页中,但是 span 值没有使用 href 传递到下一页,请我需要有关如何使用 href 将存储在变量中的 span id 值传递到下一页的指南。

这是我的代码:

     // This is where am getting song_id from

                <?php
                require '../db.php';
                $sql = "select * from songs order by song_id desc Limit 20";
               $sql_query = mysqli_query($con,$sql);

                 while ($row = mysqli_fetch_array($sql_query)) {
                     $song_id = $row['song_id'];

                  ?>
                   
    // This is the code triggering the modal and passing the song_id to the modal

         <li><a data-id="<?php echo $row['song_id']; ?>"  onclick="$('$playlist_id').text($(this).data('id')); $('#myModal3').modal('show');"><span class='icon icon_playlst'></span>Add To Playlist</a></li>


// This is the modal which would receive song_id and pass it to my_playlist.php through href.



<!-- Select Playlist Start-->
    <div id="myModal3" class="modal  centered-modal" role="dialog">
        <div class="modal-dialog login_dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <button type="button" class="close" data-dismiss="modal">
                    <i class="fa_icon form_close"></i>
                </button>
                <div class="modal-body">
                    
                    <form action="create_playlist.php" method="post">
                            
                    <div class="ms_register_form">
                        <h2>Add to Playlist</h2> 
                          
                             <div class="ms_weekly_box">
                            <div class="weekly_left">
                                <div class="w_top_song">
                                  
                                    <div class="w_tp_song_name">

      //This is where i store the song_id in a php variable and pass it in the href of my_playlist.php.

<h2><?php $playlist_id = '<span id="playlist_id"/>'; echo $playlist_id; ?></h2>

                                                                         
 <h3><a href="my_playlist.php?new_id=<?php echo $playlist_id; ?>">Hip-Hop</a></h3>
                                    </div>
                                </div>
                            </div>                      
                        </div>
                    </div>
                      </form>
                </div>
            </div>
        </div>
    </div>

【问题讨论】:

    标签: php


    【解决方案1】:

    要通过 URL 传递数据,您必须使用 "get" parameters

    【讨论】:

      【解决方案2】:

      我之前看到过这个问题,但它已关闭。

      您的标题问题可以使用 javascript 解决。您可以将跨度标记和按钮包装在表单中,并在提交表单时访问跨度标记的 id。您可以创建任意数量的表单。

      <form onsubmit="playlistHandler(event)">
          <h2><span id="playlist_id"/><?php echo $playlist_id ?></span></h2>                                         
          <button type="submit">Hip-Hop</button>
      </form>
      
      
      <script>
          function playlistHandler(event)
          {
              event.preventDefault();
              let link_id = encodeURI(event.target.children[0].children[0].getAttribute('id'));
              window.location = "my_playlist.php?new_id=" + link_id;
          }
      </script>
      

      【讨论】:

        猜你喜欢
        • 2014-06-12
        • 1970-01-01
        • 2022-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-21
        • 1970-01-01
        相关资源
        最近更新 更多