2017-07-08 2 views
2

Ich möchte Onclick von Bootstrap-Option Toggle für ausgewählte Spalte Statusfeld bearbeiten ..... Bitte Hilfe Beispiel: . Wenn der Status aktiv ist und wenn ein Benutzer klickt, sollte er in der Datenbank als inaktiv aktualisiert werden und die Seite neu laden. . Wenn der Status inaktiv ist und wenn der Benutzer klickt, sollte er in der Datenbank als aktiv aktualisiert werden und die Seite neu laden.aktualisieren Mysql Aufzeichnungen Onclick Bootrstrap Kästchen zum Ankreuzen Toggle

enter image description here

Fetch Datenbank

<?php 

require_once 'db_config.php'; 

$output = array('data' => array()); 


// do not fetch status 3 because it is deleted 
$sql = "SELECT *,emailnotificationstable.id as sid FROM notificationslist"; 
$query = $connect->query($sql); 

$num_rows = mysqli_num_rows($query); 

$x = $num_rows; 
while ($row = $query->fetch_assoc()) { 
    // activate button 
    $activateButton = ''; 
    if ($row['status'] == 1) { 
      $activateButton = 
      '<input type="checkbox" id="toggleBtn" name="toggleBtn" checked data-toggle="toggle" data-on="Active" data-off="Inactive" data-onstyle="success" data-offstyle="danger" data-size="mini" value="'.$row['sid'].'" onclick="editMember()">'; 
    } elseif ($row['status'] == 2) { 
      $activateButton = 
      '<input type="checkbox" id="toggleBtn" name="toggleBtn" data-toggle="toggle" data-on="Active" data-off="Inactive" data-onstyle="success" data-offstyle="danger" data-size="mini" value="'.$row['sid'].'" onclick="editMember()">'; 
    } 

    // extra code here 

    $output['data'][] = array(
     $x, 
     $activateButton, 
     $row['date'], 
     $row['notificationname'], 
     $row['employeename'], 
     $createdby, 
     $editedby, 
     $editeddate, 
     $deleteButton, 

    ); 

    $x--; 
} 

// database connection close 
$connect->close(); 

echo json_encode($output); 

JQuery

// global the manage memeber table 
var mytable; 

$(document).ready(function() { 
    mytable = $("#mytable").DataTable({ 
     "ajax": "../pages/php_action/salesexe/retriveemailnotifications.php", 
     "order": [], 
"fnDrawCallback": function() { 
    jQuery('#mytable #adBtn').bootstrapToggle(); 
} 
     }); 



}); 



function editMember(sid = null) { 
    if(sid) { 

    // remove the error 
    $(".form-group").removeClass('has-error').removeClass('has-success'); 
    $(".text-danger").remove(); 
    // empty the message div 
    $(".edit-messages").html(""); 

    // remove the id 
    $("#membersid").remove(); 
    // click on toggle button 
    $("#adBtn").click(function() { 

     $.ajax({ 
      url: 'notifstatus.php', 
      type: 'post', 
      data: {membersid : sid}, 
      dataType: 'json', 
      success:function(response) { 

       if(response.success == true) {      
        $(".removeMessages").html('<div class="alert alert-success alert-dismissible" role="alert">'+ 
          '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+ 
          '<strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong>'+response.messages+ 
         '</div>'); 
        // refresh the table 
        mytable.ajax.reload(null, false); 

       } else { 
        $(".removeMessages").html('<div class="alert alert-warning alert-dismissible" role="alert">'+ 
          '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+ 
          '<strong> <span class="glyphicon glyphicon-exclamation-sign"></span> </strong>'+response.messages+ 
         '</div>'); 
       } 
      } 
     }); 
    }); // click toggle btn 
} else { 
    alert('Error: Refresh the page again'); 
} 
} 

aktualisieren

<?php 

require_once 'db_config.php'; 

$output = array('success' => false, 'messages' => array()); 

$membersId = $_POST['membersid']; 

// update record to inactive 
$sql = "UPDATE notificationslist SET notfistatus = '2' WHERE id = $membersId"; 

$query = $connect->query($sql); 

if($query === TRUE) { 
    $output['success'] = true; 
    $output['messages'] = 'Notification trigger successfullt activated for selected user'; 
} else { 
    $output['success'] = false; 
    $output['messages'] = 'Error while activating notification trigger for selected user,'; 
} 
// close database connection 
$connect->close(); 

echo json_encode($output); 
+1

Sie haben eine SQL-Injektion bei "UPDATE notificationslist SET notfistatus = '2' WHERE id = $ membersId"; '. – Xorifelse

Antwort

1

Sie denken viel zu komplex beim erneuten Laden einer Seite, nicht mit der API wie Sie sollten. Sie möchten nur den Status abrufen und die benötigten Teile aktualisieren. Dies bedeutet, dass Sie eine geeignete Methode zum Identifizieren einer Zeile in Ihrer Tabelle benötigen. Auch eine HTML-ID muss auf einer Seite eindeutig sein.

Sie haben ein paar Optionen:

  1. Verwendung Datentabellen Ajax fordert so kann es mehrere Bearbeitungen an verschiedenen cols zu behandeln.
  2. Schalten Sie die Schaltfläche mit jquery und bootstrap ein und aus.

unten Dargestellt ist dieser:

$(function() { 
    // hooking event only on buttons, can do tr's as well. 
    $('.toggleBtn').click(function(){ 
    $.ajax({ 
     url: 'notifstatus.php', 
     type: 'post', 
     data: { 
     id : $(this).val(), 
     status: $(this).prop('checked') 
     }, 
     dataType: 'json', 
     success: function(response){ 
     if(response.success){ 
      $(this).bootstrapToggle('enable'); 
      console.log(response.message); 
     } else { 
      $(this).bootstrapToggle('disable'); 
      BootstrapDialog.show({ 
      title: 'failed to update status', 
      message: response.status + response.messages 
      }); 
     } 
     }, 
     error: function(xhr, textStatus, errorThrown) { 
     BootstrapDialog.show({ 
      title: textStatus, 
      message: errorThrown 
     }); 
     } 
    }); 
    }); 
}); 

Nun ist die einzige Antwort zu bauen, ist die folgende:

<?php 

    require_once 'db_config.php'; 

    if(isset($_POST['status']) && $_POST['status']){ 
    // user requests to turn off 
    $sql = "UPDATE notificationslist SET notfistatus = '2' WHERE id = ?"; // SQL injection, use prepared statements. fix it. 
    } else { 
    // user requests to turn on, other query. 
    } 

    $success; 
    $status = 0; 
    $message = 'Error while activating notification trigger for selected user,'; 

    if($query = $connect->query($sql)){ 
    if($row = $query->fetch_assoc()){ 
     $success = true; 
     $message = 'Notification trigger successfully activated for selected user'; 
     $status = $row['status']; 
    } 
    } 

    die(json_encode([ 
    'success' => $success, 
    'messages' => $message, 
    'status' => $status 
    ])); 
?> 

sollte es tun, keine Notwendigkeit, alle Daten zu verlangen eine aktualisieren Einzelwert.

+0

Danke @Xorifelse –

Verwandte Themen