HTML Modals没有弹出

问题描述

我正在使用具有逻辑的PHP构建应用程序,

我只是用while循环进行了迭代。 问题是... 来自第一个文件菜单正在回显。但是,该模式仅适用于最后一个菜单

当我检查时,将创建菜单和模态。 https://postimg.cc/crQ8JCr5-检查过的屏幕截图

$myfile1 = fopen("fisrt-file.dat","r") or die("Unable to open file!");
$myfile2 = fopen("Second-file.dat","r") or die("Unable to open file!");

while(!feof($myfile1)) {
    $fistFile = fgets($myfile1);
    $seconfFile = fgets($myfile2);
                      
    echo "<div class='one-by-three1'><div class='course-card1' data-toggle='modal' data-target='#".$seconfFile."'><p class='e-c-head'>".$fistFile."</p></div></div>";
                        
    echo "<div class='modal fade' id='".$seconfFile."' tabindex='-1' role='dialog' aria-labelledby='exampleModalCenterTitle' aria-hidden='true'>
        <div class='modal-dialog modal-dialog-centered' role='document'>
            <div class='modal-content'>
                <div class='modal-header'>
                    <h5 class='modal-title' id='exampleModalLongTitle'>".$fistFile."</h5>
                    <button type='button' class='close' data-dismiss='modal' aria-label='Close'>
                        <span aria-hidden='true'>&times;</span>
                    </button>
                </div>
                <div class='modal-body'>
                              <p>Second file element</p>
                </div>
                <div class='modal-footer'>
                    <button type='button' class='btn btn-secondary' data-dismiss='modal'>Close</button>
                </div>
            </div>
        </div>
    </div>";
    }
fclose($myfile1);
fclose($myfile2);

我使用了Bootstrap Modal。 菜单将数据目标作为第二个文件值传递。

解决方法

我看到您已更改为使用手风琴,但是正要告诉您它不起作用的原因,因此我会继续进行,以防它对其他人有所帮助。

原因是因为fgets读取行包括新行字符。因此,分配给模式的ID包含换行符,但最后一行在文件中没有换行,因此可以正常工作。

要使其正常工作,只需像下面这样限制行:

$fistFile = rtrim(fgets($myfile1));
$seconfFile = rtrim(fgets($myfile2));