gpt4 book ai didi

php - Html onclick 似乎在 echo PhP 中不起作用

转载 作者:行者123 更新时间:2023-11-29 12:14:32 24 4
gpt4 key购买 nike

我是 PHP 初学者。基本上我想在表的行中添加一个名为“删除”的按钮。但是:

  1. 按钮名称,即使我将其称为“删除”,它仍然显示为“提交”
  2. onclick 函数似乎在 echo 中不起作用。(我看过其他方法,但似乎不理解或不符合我的意图)

感谢您的帮助或建议。

更新:我已经解决了问题 1。但是,当我按下按钮时,我无法获得问题 2 onClick 函数来删除任何表数据,任何帮助将不胜感激。

更新1:Admin.php代码

 <!DOCTYPE html>
<html>
<head> <!--inseart script here -->
<script type= "text/javascript" src="js/jquery.js"></script> <!-- jquery script -->
<script type= "text/javascript" src="js/func_submit_msg.js"></script>
</head>

<body>
Message: <input type="text" name="message" id= "message_form"><br> <!-- br break new line -->


<input type="submit" id= "submit_form" onclick = "submit_msg()">

<div id="result"></div>


<table border= "1" style="width: 100%">
<!--create top table -->
<tr> <!--tr defines a row --> <!-- define row 1 -->
<th> No </th> <!-- th defines a header cell-->
<th> messages </th>
<th> action </th>
</tr>
<!-- row 2 : display results onwards and creates dynamically -->
<!-- input fields at the bottom -->


<?php
include "connect.php";
$get_data2admin = $db->query("SELECT ID,message FROM test_table_1");


if( $get_data2admin->num_rows >0 ){ //if selected rows is more than zero
while($row_msg = $get_data2admin->fetch_assoc()){
//insert html tag table inside echo

echo "<tr>";
echo "<td></td>";
echo "<td>".$row_msg['message']. "</td>";
echo "<td>"."<input type='submit' name='delete' id='delete_row' value='Delete' onClick='Delete()'>"."</td>"; //new updated line


echo "</tr>";
}
}

?>
</table>
</body>
</html>

更新 1:func_submit_msg.js(可能会将此文件重命名为 global.js 以避免混淆)

//this is a function file to submit message to database, use include before calling function

$(document).ready(function(){

$('#submit_form').on('click', function(e){
e.preventDefault();
message_form = $('#message_form').val();

$.ajax({
type: "POST",
url: "submit_data.php",
data: {message: message_form},
success: function(data) {
$('#result').html(data);
},
error:function(err){
//handle your error
alert('did not work');
}
});
});

$function Delete() {
$('input[name="delete"]').on('click', function(e){
e.preventDefault();
var show_id=$(this).attr('id') //to grab id of element

if(confirm("Are you sure you wish to delete")){
$.ajax({
type: "POST",
url: "delete.php",
data: {show_id},
success: function() {

},
error:function(err){
//handle your error
alert('did not work');
}
});
});
});
});
});

原始消息:

主要按钮代码与onclick函数一起位于admin.php页面中:

admin.php

 <!DOCTYPE html>
<html>
<head> <!--inseart script here -->
<script type= "text/javascript" src="js/jquery.js"></script> <!-- jQuery script -->
<script type= "text/javascript" src="js/func_submit_msg.js"></script>
</head>

<body>
Message: <input type="text" name="message" id= "message_form"><br> <!-- br break new line -->


<input type="submit" id= "submit_form" onclick = "submit_msg()">

<div id="result"></div>


<table border= "1" style="width: 100%">
<!--create top table -->
<tr> <!--tr defines a row --> <!-- define row 1 -->
<th> No </th> <!-- th defines a header cell-->
<th> messages </th>
<th> action </th>
</tr>
<!-- row 2 : display results onwards and creates dynamically -->
<!-- input fields at the bottom -->


<?php
include "connect.php";
$get_data2admin = $db->query("SELECT ID,message FROM test_table_1");


if( $get_data2admin->num_rows >0 ){ //if selected rows is more than zero
while($row_msg = $get_data2admin->fetch_assoc()){
//insert html tag table inside echo

echo "<tr>";
echo "<td></td>";
echo "<td>".$row_msg['message']. "</td>";
echo "<td>"."<input type='submit' name='delete' id='delete_row' onclick =' '>"."</td>"; //in this scenario the indents ' and " matters

echo "</tr>";
}
}

?>
</table>

</body>

下面是名为 func_submit_msg.js 的 javascript 文件,它具有 ajax 删除功能(代码的第二个功能)

//this is a function file to submit message to database, use include     before calling function

$(document).ready(function(){

$('#submit_form').on('click', function(e){
e.preventDefault();
message_form = $('#message_form').val();

$.ajax({
type: "POST",
url: "submit_data.php",
data: {message: message_form},
success: function(data) {
$('#result').html(data);
},
error:function(err){
//handle your error
alert('did not work');
}
});
});




$('#delete_row').on('click', function(e){
e.preventDefault();
var show_id=this.id //to grab id of element

if(confirm("Are you sure you wish to delete")){
$.ajax({
type: "POST",
url: "delete.php",
data: {show_id},
success: function() {

},
error:function(err){
//handle your error
alert('did not work');
}
});
});
});

一个delete.php文件,假设通过id元素删除行

<?php
include "connect.php";

if($_POST['show_id'])
{
$delete_id=$db->query("DELETE FROM test_table_1 WHERE ID='$show_id'");
}

if ($insert_data === TRUE ){
echo "Delete successfully";
} else {
echo "Error delete: " . $insert_data . "<br>" . $db->error;
}

$db->close();
?>

最佳答案

对于第 1 点:

<input  type='submit' name='delete' id='delete_row' onclick ='Delete(<?php echo $row_msg['id']?>)' value="Delete">

对于第 2 点:

<input  type='submit' name='delete' id='delete_row' onclick ='Delete(<?php echo $row_msg['id']?>)'>

关于php - Html onclick 似乎在 echo PhP 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30053674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com