【问题标题】:html php button generation on the fly, buttons not working动态生成html php按钮,按钮不起作用
【发布时间】:2020-03-13 16:14:14
【问题描述】:

----编辑----- Anwser 发现,而不是按钮,我使用 href 链接将数据传递到另一个 php 文件以进行数据库编辑

下面的代码解决了它

echo "<tr><td>". $row['NAME'] . " " .  $row['id'] . "</td><td> <a href='edit.php?edit=$i'>Edit</a>
</td>";

从 mysql 数据库数据生成按钮时。按钮本身似乎不适用于 on click 命令

 $i=0;
 $redirecrurl = 'location.href=\"TestButtonWorks.php\";';
while($row = mysqli_fetch_array($result))
{
     $i= $i+1;
    echo "<tr><td><button class=$i onclick = '$redirecrurl'>  ". $row['NAME'] . " " .  $row['ID'] . " </button></tr></td>";

}


我也试过用一个函数替换网页链接来改变背景颜色,但也不起作用

页面链接: https://crucial-creeks.000webhostapp.com/MainPage.php

它在表格中生成的按钮不起作用,底部添加到列表的按钮工作正常

完整源代码:

<html>
<head>
<meta http-equiv="refresh" content="15" />

<STYLE TYPE="text/css">
<!--
TD{font-family: Arial; font-size: 16pt; color="white";}
--->
</STYLE>
<title>Roma</title>
</head>
<body style="background-color:black;">
<font size="3" color="white">
<h1>Pizzaroma</h1>

<?php 
    echo "____________________________________________________________</br>";
    //Connect to database
    require 'Conn.php';

    $result = mysqli_query($conn,"SELECT id, NAME FROM Chips");
    echo "</br>To Be Made</br>";
    //Setup Table format
    echo "<table border='1' style='font-family:Georgia, Garamond, Serif;color:white;font-style:italic;'>";


    $i=0;
    $redirecrurl = 'location.href=\"TestButtonWorks.php\";';
    while($row = mysqli_fetch_array($result))
    {
        //$i To give each buton a specific class
         $i= $i+1;
         //Create button per information in database
        echo "<tr><td><button class=$i onclick = '$redirecrurl'>  ". $row['NAME'] . " " .  $row['id'] . " </button></tr></td>";
    }
    //prints out total amount of orders
    echo $i;
    echo "</table>";
    //Close Database
    mysqli_close($conn);


?>;

<p>____________________________________________________________</p>

</br> 

<button class="btn btn-success" onclick="location.href='SingleChip.php';"> Single Chip</button>
<button class="btn btn-success" onclick="location.href='DoubleChip.php';">Double Chip</button>
<button class="btn btn-success" onclick="location.href='ChipCheese.php';"> Chip Cheese</button>
</br>
<button class="btn btn-success" onclick="location.href='GarlicChip.php';"> Garlic Chip</button>
<button class="btn btn-success" onclick="location.href='GarlicChipCheese.php';">Garlic Chip & Cheese</button>
</br>
<button class="btn btn-success" onclick="location.href='ClearTable.php';"> Clear Table</button>

</br>

</br>
<label for="vehicle1">Salt</label>
<input type="checkbox" id="Salt2" name="Salt2" value="Salt">
<label for="vehicle1">Vinegar</label>
<input type="checkbox" id="Vinegar2" name="Vinegar2" value="Vinegar">
<label for="Well Done">Well Done</label>
<input type="checkbox" id="Well Done2" name="Well Done2" value="Well Done">
<label for="Delivery">For Delivery</label>
<input type="checkbox" id="Delivery2" name="Delivery2" value="Delivery">
</font>



<script>

function RemoveRow($id){
    echo "FUNCTION CALLED SUCCESSFULLY" $id;
}
</script>

</body>
</html>

【问题讨论】:

  • 您的引号有语法错误。
  • 对我来说效果很好,没有语法错误,无需创建页面位置。

标签: php html mysql button


【解决方案1】:

onclick = 'location.href='TestButtonworks.php';' 检查您的语法。您应该使用onclick='location.href=\"TestButtonWorks.php\";' 来避免与单引号' 的冲突。

【讨论】:

  • 我已将其替换为您的建议,但似乎无法解决按钮不起作用的问题。待制作:它将显示数据库中的内容,我希望那些即时制作的按钮重定向到另一个页面,但这些按钮似乎不起作用。 crucial-creeks.000webhostapp.com/MainPage.php
【解决方案2】:

感谢 youtube 上的教程 https://www.youtube.com/watch?v=Hw1MwUlekeo
,我找到了问题的答案 基本上你不能在动态创建它们时使用点击按钮,而是使用链接并发送主键信息等通过php删除信息,就像这样

echo "<tr><td>". $row['NAME'] . " " .  $row['id'] . "</td><td> <a href='edit.php?edit=$i'>Edit</a>
</td>";

【讨论】:

    【解决方案3】:
    $redirecrurl = 'location.href="TestButtonworks.php"';
    echo "<tr><td><button class=$i onclick = '$redirecrurl'>  ". $row['NAME'] . " " .  $row['ID'] . " </button></tr></td>";
    

    【讨论】:

    猜你喜欢
    • 2016-01-22
    • 2014-04-05
    • 2013-11-27
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 2013-01-02
    • 2011-05-13
    • 1970-01-01
    相关资源
    最近更新 更多