IsharaRA
IsharaRA

Reputation: 45

Run php function in button OnClick (not submit)

I wanna run following query on function DeleteCus by clicking on a button.I tried following code for that.bt it is not working.

getting http://localhost/AdminLTE-2.1.1/CusRegReport.php?id=134 url after running notification.php getting error Undefined index: id in lin 67

<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); 
}

?>

Upvotes: 1

Views: 510

Answers (5)

Duong Xuong Dat
Duong Xuong Dat

Reputation: 11

You have $id = $_GET['id']; in method DeleteCus, but you didn't pass that parameter in your link in the button PHP/notification.php?hello=true. So, it should be <button type="button" id = "button" class="btn btn-danger" data-dismiss="modal" onclick ="window.location.href ='PHP/notification.php?hello=true&id=XXX'">. That's why it returns undefined index id in calling PHP method

Upvotes: 1

srinath madusanka
srinath madusanka

Reputation: 592

try

<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;" >

also in your function

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); 
}
}

when you are calling your function change it

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


}

Upvotes: 1

Alfiza malek
Alfiza malek

Reputation: 1004

remove extra href from button onclick event window.location.href ='PHP/notification.php?hello=true

try this:-

Upvotes: 1

Mahendra Salve
Mahendra Salve

Reputation: 96

TRY this code for button

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

Upvotes: 1

Tristup
Tristup

Reputation: 3663

Please replace the button code with the following code :

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

Upvotes: 2

Related Questions