簡體   English   中英

從另一個PHP文件中調用一個php文件,該文件在echo中具有html格式?

[英]Call a php file from an other php file which has a html form in an echo?

我想通過單擊表單中的按鈕來刪除mysql行。 問題是我在php中執行了cicyle,而html action屬性未導航到另一個php頁面中,該代碼應在該頁面中刪除該行。 這是我的代碼:

 if ($result->num_rows > 0) {
 echo "<table><tr><th>Vezetéknév</th><th>Keresztnév</th><th>Nemzetiség</th>
 <th>Szülőváros</th><th>Találmányok száma</th></tr>";

 while($row = $result->fetch_assoc()) {
     echo "<tr><td>" . $row["vezeteknev"]. "</td><td>" . $row["keresztnev"]. 
  "</td><td>" . $row["nemzetiseg"] ."</td><td>" 
     . $row["szulovaros"]. "</td><td>". $row['talalmanyok']. "
     </td> <td><form action='deleterow.php' method='post'>
     <input type='submit' name='delete' value='".$row['id']."'/>
     </form></td></tr>";       
 }

另一頁(deleterow.php):

    header('location:index.php');
    include('DBConnection');

    if(isset($_POST['delete'])){

    $user  = $_POST['delete'];
    $delet_query = mysqli_query($conn,"DELETE FROM feltalalo WHERE id = '$user' ") or die(mysql_error());

 }

如下更改代碼:

while($row = $result->fetch_assoc()) {
     echo "<tr><td>" . $row["vezeteknev"]. "</td><td>" . $row["keresztnev"]. 
  "</td><td>" . $row["nemzetiseg"] ."</td><td>" 
     . $row["szulovaros"]. "</td><td>". $row['talalmanyok']. "
     </td> <td><a href='deleterow.php?delete=".$row['id']."'>Delete</a></td></tr>";       
 }

deleterow.php

if(isset($_GET['delete'])){    
    $user  = $_GET['delete'];
    $delet_query = mysqli_query($conn,"DELETE FROM feltalalo WHERE id = '$user' ") or die(mysql_error());    
}

我相信您想要實現的是能夠使用鏈接刪除條目,但是將其注冊為發布請求。

為此,您需要像在鏈接之前那樣生成一個表單

<form name="item_deleter_{YOUR_ID}" style="display:none;" method="post" action="{URL_FOR_DELETING}">
 <input type="hidden" name="delete" value="{YOUR_ID}">
</form>

然后,在此之后,您將創建刪除鏈接以具有onclick,該onclick使用javascript這樣提交上述表單

<a href="#" onclick="document.item_deleter_{YOUR_ID}.submit(); event.returnValue = false; return false;">Delete</a>

干杯

暫無
暫無

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

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