首页 > 解决方案 > 通过单击带有 js 的按钮添加 div 不能与 PHP 一起使用

问题描述

一旦我添加 PHP,Javascript 代码就不起作用

我有一个 javascript 函数,它通过单击按钮添加 div,代码在没有 PHP 的情况下完美运行,但只要放入以下 ​​PHP 代码,它就不再工作了。请帮忙,谢谢!

<script>$(document).ready(function(){
var i=1;
$('#add').click(function(){
    i++;
$('#dynamic_field').append('<tr id="row'+i+'"><td>Kategorie<br><select>"<?php while($row = mysqli_fetch_array($result)){echo '<option name="Kategoriename">' .$row['Kategorienname']. "</option>"; }?>"</select></td></select></td><td>Name<input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td>Inhalt<input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td>Preis<input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td>normal<input type="text" name="name[]" placeholder="Ø" class="form-control name_list" /></td><td>Preis<input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td>mittel<input type="text" name="name[]" placeholder="Ø" class="form-control name_list" /></td><td>Preis<input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td>groß<input type="text" name="name[]" placeholder="Ø" class="form-control name_list" /></td><td>Preis<input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td>Allergene<br><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/a.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/b.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/c.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/d.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/e.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/f.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/g.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/h.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/l.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/m.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/n.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/o.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/p.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/r.png"></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
});

$(document).on('click', '.btn_remove', function(){
    var button_id = $(this).attr("id"); 
    $('#row'+button_id+'').remove();
});

$('#submit').click(function(){      
    $.ajax({
        url:"name2.php",
        method:"POST",
        data:$('#add_name').serialize(),
        success:function(data)
        {
            alert(data);
            $('#add_name')[0].reset();
        }
    });
});
});

标签: javascriptphparraysdatabase

解决方案


这看起来不错,只是您在双引号内运行 PHP。以我的经验,这是行不通的。扩展您的 PHP 标签。

从:

$('#dynamic_field').append('<tr id="row'+i+'"><td>Kategorie<br><select>"<?php while

类似于:

$('#dynamic_field').append(<?php echo "<tr id=\"row'+i+'\"><td>Kategorie<br><select>"; while...

推荐阅读