簡體   English   中英

在按鈕OnClick中運行php函數(不提交)

[英]Run php function in button OnClick (not submit)

我想通過單擊按鈕在功能DeleteCus上運行以下查詢。我嘗試了針對那段代碼的代碼。

運行notify.php后獲取http://localhost/AdminLTE-2.1.1/CusRegReport.php?id = 134 url出現錯誤未定義索引:lin 67中的id

<button type="button"  id = "button" class="btn btn-danger" data-dismiss="modal" onclick ="window.location.href = href='PHP/notification.php?hello=true'">

<?php


 if (isset($_GET['hello'])) {
DeleteCus();


}

session_start();
$dbc = mysqli_connect('localhost','root','','dashboard') or die(mysqli_connect_error(''));

function notification(){
    global $dbc;
    $query = "SELECT `customer_id`, `customer_name` FROM `customer` WHERE `confirmation`IS NULL LIMIT 0, 30 ";
    $result = mysqli_query($dbc,$query);        
    while($row=mysqli_fetch_array($result))
    {
        echo "<a href='CusRegReport.php?id=" . $row['customer_id'] . "'><i class='fa fa-users text-red'>&nbsp;&nbsp;" . $row['customer_name'] . "&nbsp;&nbsp; needs to register</i></a>";        
     $_SESSION["count"] =mysqli_num_rows($result);  

    }

}

function RegReport(){
    global $dbc; 



    $id = $_GET['id'];  

   // $id = $mysqli->real_escape_string ($id);   

    $sql = "SELECT * FROM `customer` WHERE `customer_id` = '$id'";
    $r = mysqli_query($dbc,$sql);
    while($line=mysqli_fetch_array($r))
    {
        $a= "<br/>" . $line['customer_name'] . "<br/>" . $line['ad_line_one'] .$line['ad_line_two'] .$line['ad_line_three'] .$line['web'] .$line['pub_pvt'] .$line['reg_no'] .$line['tax_no'] .$line['description'] .$line['attachments'] .$line['contact_name'] .$line['contact_salutation'] .$line['contact_designation'] .$line['contact_email'] .$line['contact_tp1'] .$line['contact_tp2'] .$line['md_fname'] .$line['md_lname'] .$line['md_email'] .$line['date']. "</a>";  

    }
}
function DeleteCus(){
global $dbc;   
$id =  $_GET['id'];
$sql = "DELETE FROM `customer` WHERE `customer`.`customer_id` = '$id'";
  $r = mysqli_query($dbc,$sql); 
}

?>

請用以下代碼替換按鈕代碼:

<button type="button"  id = "button" class="btn btn-danger" data-dismiss="modal" onclick ="window.location.href='PHP/notification.php?hello=true'" />

嘗試此代碼的按鈕

<button type="button"  id = "button" class="btn btn-danger" data-dismiss="modal" onclick ="window.location.href ='PHP/notification.php?hello=true'">

嘗試

<button type="button"  id = "button" class="btn btn-danger" data-dismiss="modal" 
onclick="javascript:window.location.href='www.exampe.com/PHP/notification.php?hello=true&id=<?php echo $_GET['id'];?>'; return false;" >

也在你的職能上

function DeleteCus($dbc,$id){
 $dbc=$dbc; 
if(isset['$_GET['id'])){  
$id =  $_GET['id'];
$sql = "DELETE FROM `customer` WHERE `customer`.`customer_id` = '$id'";
  $r = mysqli_query($dbc,$sql); 
}
}

當您調用函數時進行更改

if (isset($_GET['hello'])) {
DeleteCus($dbc,$id);


}

從按鈕onclick事件window.location.href ='PHP / notification.php?hello = true中刪除多余的href

嘗試這個:-

您有$id = $_GET['id']; 在DeleteCus方法中,但是您沒有在按鈕PHP / notification.php?hello = true中的鏈接中傳遞該參數。 因此,應為<button type="button" id = "button" class="btn btn-danger" data-dismiss="modal" onclick ="window.location.href ='PHP/notification.php?hello=true&id=XXX'"> 這就是為什么它在調用PHP方法時返回未定義的索引ID

暫無
暫無

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

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