I have a bootstrap table which element are loaded from a database.
I have a js function to delete a table row from data base, so I add a button in each row of the table.
<table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>id</th>
<th>client</th>
</tr>
</thead>
<tbody>
<?php
require("config.php");
// Create connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_row()) {
$user_id=$row[0];
$user_name=$row[1];
?>
<tr>
<!--here showing results in the table -->
<td><?php echo $user_id; ?></td>
<td ><?php echo $user_name; ?></td>
<td>
<input type="button" class="btn btn-danger btn-xs delete" value="Cancella sin id" onclick="deleteFunction()"/>
</td>
</tr>
</tbody>
</table>
How can I pass to deleteFucntion the id or user_name in order to use them into Javascipt like this:
function deleteFunction() {
var username = #MY_ROW_USERNAME;
....
}
TD's should look like this
<td id="user_name_<?=$user_id?>" onClick="deleteFunction(<?=$user_id?>)"><?=$user_name?></td>
Delete function like this
function deleteFunction(user_id) {
var username = document.getElementById('user_name_'+user_id).innerHTML;
}
Related
I already tried to use href and onclick options to call my deleteAppoint. I can call it, but it won't delete any data in my database, and I don't know how to fix it properly. I've been kind of stuck in this problem for a while now.
Here is the code:
viewAppoint.php
if ($_POST) {
$appoint_id = $_POST["appoint_id"];
$result = "SELECT * FROM appointment WHERE appoint_id='$appoint_id'";
$resultConn = $connect->query($result);
$row = $resultConn->fetch_array();
}
--
<div class="row">
<div class="col-md-12">
<div class="panel panel-success">
<div class="panel-heading">Listing all your Appointments</div>
<div class="panel-body">
<table class="tables" id="manageAppointTable" style="width:100%;">
<thead>
<tr>
<th style="text-align: center;" name="appoint_id"
id="appoint_id">
Appoint ID
</th>
<th style="text-align: center;">Appoint Date</th>
<th style="text-align: center; width: 150px">Item/s</th>
<th style="text-align: center;">Quantity</th>
<th style="text-align: center; width: 100px">Due Date</th>
<th style="text-align: center;">Request Status</th>
<th style="text-align: center;">Maintenance Status</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="custom/js/appoint.js"></script>
<?php require_once 'includes/footer.php'; ?>
Here is the code to fetch my data
fetchAppoint.php
require_once 'core.php';
$sql = "SELECT appoint_id, customer_name, customer_contact, order_date,
quantity, due_date, item_type, description, appoint_active, appoint_status,
request_status FROM appointment WHERE appoint_status = 1";
$result = $connect->query($sql);
$output = array('data' => array());
if ($result->num_rows > 0) {
$activeBrand = "";
while ($row = $result->fetch_array()) {
$brandId = $row[0];
$buttons = '<a href="php_action/deleteAppoint.php?delete=true"
class="btn btn-sm btn-danger edit_cat">Delete</a>';
$output['data'][] = array(
$row[0],
$row[3],
$row[6],
$row[4],
$row[5],
$activeCategory,
$activeCategory1,
$buttons
);
}
}
$connect->close();
echo json_encode($output);
Lastly, my code for delete.
deleteAppoint.php
require_once 'core.php';
$valid['success'] = array('success' => false, 'messages' => array());
if ($_POST) {
$appoint_id = $_POST["appoint_id"];
$sql2 = "DELETE from appointment where appoint_id = '$appoint_id'";
if ($connect->query($sql2) === true) {
header('location: http://localhost/inventory-system/viewAppoint.php');
} else {
echo "Failed!";
}
}
Additionally if you decide to get the appoint_id from GET then you must apppend it to href attribute of a tag.
I have create a multiple ckeditor in php, it can display the panel of ckeditor correctly, but when I click update after adding something in it, it didn't save to the database.
<?php
if(isset($_POST["action"])&&($_POST["action"]=="update")){
$id = $_POST['id'];
$content = $_POST['content'];
$query_update = "UPDATE `correspond` SET content='$content' WHERE id='$id'";
mysql_query($query_update);
header("Location: correspond.php");
}
?>
<table>
<?php while($row_correspond_result=mysql_fetch_assoc($correspond_result)){ ?>
<tr class="table table-bordered table-striped"><form action="" method="POST" name="correspond_form" id="correspond_formJoin">
<tbody id="myTable">
<td colspan="3" align="center"><div contenteditable="true" class="myContent" id="content"><?php echo $row_correspond_result["content"];?> </div>
<script>
var elements = document.getElementsByClassName( 'myContent' );
for ( var i = 0; i < elements.length; ++i ) {
CKEDITOR.inline( elements[ i ], { /* config for this instance */ } );
}});
</script>
</td>
</tr><tr>
<td colspan="2"></td>
<td align="center">
<input name="action" type="hidden" id="action" value="update">
<input type="submit" name="Submit" value="Update"></td>
</form></tr><?php }?> </tbody></table>
To save multiple ckeditor, you have need to pass the name attribute in array format. when you click on submit save the content value using the serialize function.
PHP code.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST["action"])&&($_POST["action"]=="update")){
$content = $id = '';
$form_data = $_POST['form_data'];
for($i=0 ; $i<count($form_data['id']); $i++){
$content = serialize($form_data['content'][$i]);
$id = $form_data['id'][$i];
$query_update = "UPDATE correspond SET content='$content' WHERE id= $id";
mysqli_query($conn, $query_update);
}
//header("Location: correspond.php");
}
?>
At the time of display unserialize content value and put in textarea field.
HTML Code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdn.ckeditor.com/4.11.4/standard/ckeditor.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var elements = document.getElementsByClassName( 'myContent' );
for ( var i = 0; i < elements.length; ++i ) {
CKEDITOR.replace( elements[ i ]);
//CKEDITOR.inline( elements[ i ], { /* config for this instance */ } );
}
});
</script>
<table>
<tr class="table table-bordered table-striped">
<form action="" method="POST" name="correspond_form" id="correspond_formJoin">
<tbody id="myTable">
<td colspan="3" align="center">
<?php
$sql = "SELECT id, content FROM correspond";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//get the content form database and use unserialize method
$content = unserialize($row["content"]);
?>
<!-- Change div to textarea and use the name attr as array format -->
<textarea contenteditable="true" class="myContent" name="form_data[content][]" id="content"><?php echo $content;?> </textarea>
<!-- add the corresponding id to update the content. -->
<input type="hidden" name = "form_data[id][]" value ="<?php echo $row["id"];?> ">
<?php
}
}
?>
</td>
</tr><tr>
<td colspan="2"></td>
<td align="center">
<input name="action" type="hidden" id="action" value="update">
<input type="submit" name="Submit" value="Update"></td>
</form></tr></tbody></table>
This div Converted to TextArea
From
<div contenteditable="true" class="myContent" id="content"><?php echo $row_correspond_result["content"];?> </div>
To
<textarea contenteditable="true" class="myContent" id="content"><?php echo $row_correspond_result["content"];?> </textarea>
I am trying to create a search within my table.
I have the table populated with date already loaded, but when i type into the search box for example a name and press submit, nothing is happening the page just reloads and nothing happens.
Here is the code, (i also need to do the same with a table where the table data is all foreign keys.)
<?php // Include config file
include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/config.php");
include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/functions.php");
include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/header.php");
$sql = "SELECT * FROM patient";
if (isset($_POST['searchform'])) {
$search_term = ($_POST['searchpat']);
$sql .= " WHERE fName LIKE '{$search_term}'";
$sql .= " OR sName LIKE '{$search_term}'";
$sql .= " OR addLineOne LIKE '{$search_term}'";
}
$query = mysqli_query($db, $sql) or die(mysql_error());
?>
<body>
<div class="container">
<?php include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/logo.html"); ?>
<h2>List of Patients</h2>
<p>All Patients Registered with Freddies Medical:</p>
<form name="searchform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<label>Search for Patient</label>
<input type="text" class="form-control" name="searchpat" required><br>
<input type="submit" class="btn btn-primary" name="search" value="Submit">
<span class="help-block"></span>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>First Name</th>
<th>Surname</th>
<th>Address</th>
<th>Phone</th>
<th>Email Address</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
echo '<tr>
<td>'.$row['fName'].'</td>
<td>'.$row['sName'].'</td>
<td>'.$row['addLineOne'].", ".$row['addCity'].", ".$row['addPostCode'].'</td>
<td>'.$row['phone'].'</td>
<td>'.$row['email'].'</td>
<td>View Patient</td>
<td>Delete</td>
</tr>';
$no++;
}?>
</tbody>
</table>
New Patient
Admin Area
</div>
<div class="bottompadding"></div>
<?php include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/footer.php"); ?>
</body>
</html>
$_POST['searchform'] will not be set as far as I know since it is the name of the form and not a form element. Better check for isset($_POST['search']):
if(isset($_POST['search'])) {
$search_term = $_POST['searchpat'];
$sql .= " WHERE fName LIKE '%".$search_term."%'"; // Using % wildcard will search for fields 'containing' searched string rather then 'exact matches'
$sql .= " OR sName LIKE '%".$search_term."%'";
$sql .= " OR addLineOne LIKE '%".$search_term."%'";
}
i created an appointments table where user1 would add appointments and user2 would accept/reject the appointment. now my problem is when the accept and reject are clicked it is displayed in the table but it is not updated in the db I've did a lot of research over this but hit a road block after road block, so I really hope your help in this, many thanks!
Here is an image of my appointments table:
Here is my code:
<table cellpadding="0" cellspacing="0" border="0" class="table table-condensed" id="example">
<thead>
<tr>
<th>appoinment ID</th>
<th>Date</th>
<th>time</th>
<th>teacher</th>
<th>parent</th>
<th> accept/reject </th>
<th>state</th>
</tr>
</thead>
<tbody>
<?php
$query=mysqli_query($conn, "select * from `app` left join `par` on par.par_id=app.par_id
left join `tea` on tea.tea_id=app.tea_id
ORDER BY app_id DESC");
if($query === false)
{
throw new Exception(mysqli_error($conn));
}
while($row=mysqli_fetch_array($query))
{
$ann_id=$row['app_id'];
$date=$row['date'];
$msg=$row['time'];
$username = $row['username'];
$username = $row['p_username'];
?>
<tr>
<td><?php echo $row['app_id'] ?></td>
<td> <?php echo date('j/m/y',strtotime($row['date'])); ?></td>
<td><?php echo $row['time'] ?></td>
<td><?php echo $row['p_username'] ?></td>
<td><?php echo $row['username'] ?></td>
<td>
reject
accept
</td>
<td>
<div class="chgtext">PENDING</div>
</td>
</tr>
<?php
if (isset($_GET['state'], $_GET['app_id'])) {
$stmt = mysqli_prepare($conn, "UPDATE app SET state = ? WHERE app_id = ?");
mysqli_stmt_bind_param($stmt, "sd", $_GET['state'], $_GET['s.app_id']);
$stmt->execute();
$stmt->close();
}
}
?>
</tbody>
</table>
</div>
</form>
You have an error there. You are using $_GET request with setting it in url. Change your html tag like below
reject
accept <!-- I assumed that column names in database were app_id and state
And in php code
if (isset($_GET['state'], $_GET['app_id'])) {
$stmt = mysqli_prepare($conn, "UPDATE app SET state = ? WHERE app_id = ?");
mysqli_stmt_bind_param($stmt, "sd", $_GET['state'], $_GET['app_id']);
$stmt->execute();
$stmt->close();
}
}
I hope it will help you. If there is any problem then kindly tell me
I have a table with a checkbox at the top of the first column to allow the user to select all items in the table and perform an AJAX script. I'm using PHP to generate the html table from a database query - I'd like to set the checked value for the header checkbox if all items in the table are also checked for the same column.
In the PHP file I'm setting up the table and the header rows first, then doing a foreach loop to create the table rows from the found records. I could add a counter along the way to track how many rows are checked, but as this is happening after the table header I can't see a way to subsequently update the header row? Unless this could be done via Javascript?
Here's my PHP page that generates the table:
<table class="table table-condensed table-striped table-bordered">
<thead>
<th><input type="checkbox" class="select-all checkbox" name="select-all" /> </th>
<th class="text-center" scope="col">Product ID</th>
<th class="text-center" scope="col">Description</th>
</thead>
<tbody>
$found = $result->getFoundSetCount();
foreach($records as $record) {
$productID = $record->getField('productID');
if (!in_array($productID, $_SESSION['selectedProductIDs'])) {
$checked = '';
} else {
$checked = ' checked';
}
?>
<tr class="" id="<?php echo $recid ?>">
<td id="<?php echo $productID; ?>"><input type="checkbox" class="select-item checkbox" name="select-item" value="<?php echo $productID; ?>" <?php echo $checked; ?>></td>
<td><?php echo $productID; ?></td>
<td><?php echo $record->getField('Description'); ?></td>
</tr>
} // foreach
i did not test this code, but i think is what do you search
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function(){
var allAreChecked = true;
$("table > tbody > tr > td > input[type=checkbox]").each(function(){
if(! $(this).is(':checked'))
allAreChecked = false;
});
if(allAreChecked === true){
$("table > thead > tr > th > input[type=checkbox]").prop('checked', true);
}
});
</script>
simple do the trick with string concatenation like below
1st : First run the foreach loop and concatenate all tr as a string .
2nd : Outside the foreach loop Set the $check_all_button=true; in foreach loop check any row is not checked means set the variable to false.
3rd : After all this concatenate the table header part with another variable . based on $check_all_button variable value check the checkbox .
4th : Finally echo the both variables . to render the html table .
<?php
$found = $result->getFoundSetCount();
$table_tr='';
$check_all_button=true;
foreach($records as $record)
{
$productID = $record->getField('productID');
if (!in_array($productID, $_SESSION['selectedProductIDs'])) {
$checked = '';
$check_all_button=false;
} else {
$checked = ' checked';
}
$table_tr.="<tr class='' id='<?php echo $recid ?>'>
<td id='<?php echo $productID; ?>'><input type='checkbox' class='select-item checkbox' name='select-item' value='<?php echo $productID; ?>' <?php echo $checked; ?>></td>
<td><?php echo $productID; ?></td>
<td><?php echo $record->getField('Description'); ?></td></tr> ";
<?php
}
$check_all_button=true;
$table_and_head='';
$table_and_head.="<table class='table table-condensed table-striped table-bordered'>
<thead>
<th><input type='checkbox' class='select-all checkbox' name='select-all'";
if($check_all_button==true){ $table_and_head.=' checked'; }
$table_and_head.=" /> </th>
<th class='text-center' scope='col'>Product ID</th>
<th class='text-center' scope='col'>Description</th>
</thead>
<tbody> </tbody> </table>";
echo $table_and_head;
echo $table_tr;
?>