簡體   English   中英

HTML onclick似乎在echo PhP中不起作用

[英]Html onclick does not seem to work in echo PhP

我是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']?>)'>

1.像這樣更改刪除按鈕

<input  type='submit' name='delete' id='delete_row' onclick ='Delete(<?php echo $row_msg['id']?>)'>
  1. 假設你有任何唯一的ID,所以我在onclick函數中提到過,如果您有其他名字,請更改它

     function Delete(id) { var x = confirm("Are you sure you want to delete?"); if(x) { $.ajax({ type: "POST", url: "delete.php", data: {id:id}, success: function() { }, error:function(err){ //handle your error alert('did not work'); } }); }); } 

從SQL結果中獲取唯一ID作為ID,並使用jQuery代碼刪除行為:

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

我希望這可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM