tabela.php
<?php
include('required/header.php');
include('core/functions.php');
?>
<div id="wrapper">
<?php
$host="localhost";
$username="root@localhost";
$password="pwd";
$databasename="odswiezenie";
$conn=mysqli_connect($host,$username,$password);
$db=mysqli_select_db($conn, $databasename);
$ReturnSortValues = @$_GET['SortName'];
$ReturnSortType = @$_GET['SortType'];
$ReturnSortType = htmlspecialchars(addslashes($ReturnSortType));
$ReturnSortValues = htmlspecialchars(addslashes($ReturnSortValues));
(empty($ReturnSortValues)) ? $ReturnSortValues = 'bookingdate' : $ReturnSortValues = $ReturnSortValues;
(empty($ReturnSortType)) ? $ReturnSortType = 'ASC' : $ReturnSortType = $ReturnSortType;
$sql = "SELECT id, destination, ticketnumber, name, surename, phone, origin, tripday, bookingdate, bookedby FROM wycieczki ORDER BY " . $ReturnSortValues . " " . $ReturnSortType;
$result = mysqli_query($conn,$sql);
?>
<div class="container-fluid">
<?php
print $_POST['SortType'];
?>
<div class="jumbotron">
<h1>Orders control panel.</h1>
<p>Edit and delete database records details live option. That option include an extra form fields where you can additional new database row with new tickets details. Option located end of page.</p>
</div>
<?php
print ShowSortingForm( $argObj = true, $TabSortValues = array('destination', 'ticketnumber', 'name', 'surename', 'phone', 'origin', 'tripday', 'bookingdate', 'bookedby'), $TabSortType = array('ASC', 'DESC') );
?>
<table align="center" cellpadding="5" border="1" id="user_table" class="table table-hover" width=100%>
<tr>
<th>No.:</th>
<th>Destination</th>
<th>Ticket number</th>
<th>Name</th>
<th>Surename</th>
<th>Phone</th>
<th>Origin</th>
<th>Trip day</th>
<th>Booking date</th>
<th>Booked by</th>
<th>Action</th>
</tr>
<?php
$count = 1;
while ($row=mysqli_fetch_array($result))
{
?>
<tr id="row<?php echo $row['id'];?>">
<td width=10 class="table_orders"><?php echo $count;?></td>
<td class="table_orders" id="destination_val<?php echo $row['id'];?>"><?php echo $row['destination'];?></td>
<td class="table_orders" id="ticketnumber_val<?php echo $row['id'];?>"><?php echo $row['ticketnumber'];?></td>
<td class="table_orders" id="name_val<?php echo $row['id'];?>"><?php echo $row['name'];?></td>
<td class="table_orders" id="surename_val<?php echo $row['id'];?>"><?php echo $row['surename'];?></td>
<td class="table_orders" id="phone_val<?php echo $row['id'];?>"><?php echo $row['phone'];?></td>
<td class="table_orders" id="origin_val<?php echo $row['id'];?>"><?php echo $row['origin'];?></td>
<td class="table_orders" id="tripday_val<?php echo $row['id'];?>"><?php echo $row['tripday'];?></td>
<td class="table_orders" id="bookingdate_val<?php echo $row['id'];?>"><?php echo $row['bookingdate'];?></td>
<td class="table_orders" id="bookedby_val<?php echo $row['id'];?>"><?php echo $row['bookedby'];?></td>
<td style="text-align:center">
<input type='button' class="btn btn-primary" id="edit_button<?php echo $row['id'];?>" value="edit" onclick="edit_row('<?php echo $row['id'];?>');">
<input type='button' class="btn btn-success" id="save_button<?php echo $row['id'];?>" value="save" onclick="save_row('<?php echo $row['id'];?>');">
<input type='button' class="btn btn-danger" id="delete_button<?php echo $row['id'];?>" value="delete" onclick="if(confirm('Are you sure?')) delete_row('<?php echo $row['id'];?>');" >
</td>
</tr>
<?php
$count++;
}
?>
<tr id="new_row">
<td></td>
<td><input type="text" class="form-control" id="new_destination" value="Type destination value"></td>
<td><input type="text" class="form-control" id="new_ticketnumber" value="Type ticket number value"></td>
<td><input type="text" class="form-control" id="new_name" value="Type name value"></td>
<td><input type="text" class="form-control" id="new_surename" value="Type surename value"></td>
<td><input type="text" class="form-control" id="new_phone" value="Type phone value"></td>
<td><input type="text" class="form-control" id="new_origin" value="Type origin value"></td>
<td><input type="text" class="form-control" id="new_tripday" value="Type trip day value"></td>
<td><input type="text" class="form-control" id="new_bookingdate" value="Type booking date value"></td>
<td><input type="text" class="form-control" id="new_bookedby" value="Type booked by value"></td>
<td><input type="button" value="Insert new ticket details" class="btn btn-success" onclick="confirmR();"></td>
</tr>
</table>
</div>
<div class="ajax"></div>
<br><br><br><br>
</body>
</html>
<?php
mysqli_close($conn);
?>
plik: functions.php
<?php
function ShowSortingForm($argObj, $TabSortValues, $TabSortMethod): string
{
$argObj = "<div class=\"row\"><div class=\"col-md-2 col-sm-2 col-xs-12\"><form method=\"post\" id=form>";
$argObj .= "<select name=\"SortName\" class=\"form-control input-sm\">";
foreach($TabSortValues as $key1)
{
$argObj .= "<option value=" . $key1 . ">" . $key1 . "</option>";
}
$argObj .= "</select></div>";
$argObj .= "<div class=\"col-md-2 col-sm-2 col-xs-12\"><select name=\"SortType\" class=\"form-control input-sm\" style=\"width=100px;\">";
foreach($TabSortMethod as $key2)
{
$argObj .= "<option value=" . $key2 . ">" . $key2 . "</option>";
}
$argObj .= "</select></div>";
$argObj .= "<div class=\"col-md-2 col-sm-2 col-xs-12\"><input type=\"submit\" id=submit class=\"btn btn-primary\">Sort data</button></div></div></form><br><br>";
return $argObj;
}
?>
W pliku tabela.php jest odwolanie do tej funkcji z formularzem
<?php
print ShowSortingForm( $argObj = true, $TabSortValues = array('destination', 'ticketnumber', 'name', 'surename', 'phone', 'origin', 'tripday', 'bookingdate', 'bookedby'), $TabSortType = array('ASC', 'DESC') );
?>
modify_records.js
function edit_row(id)
{
var destination=document.getElementById("destination_val"+id).innerHTML;
var ticketnumber=document.getElementById("ticketnumber_val"+id).innerHTML;
var name=document.getElementById("name_val"+id).innerHTML;
var surename=document.getElementById("surename_val"+id).innerHTML;
var phone=document.getElementById("phone_val"+id).innerHTML;
var origin=document.getElementById("origin_val"+id).innerHTML;
var tripday=document.getElementById("tripday_val"+id).innerHTML;
var bookingdate=document.getElementById("bookingdate_val"+id).innerHTML;
var bookedby=document.getElementById("bookedby_val"+id).innerHTML;
document.getElementById("destination_val"+id).innerHTML="<input type='text' class='form-control' id='destination_text"+id+"' value='"+destination+"'>";
document.getElementById("ticketnumber_val"+id).innerHTML="<input type='text' class='form-control' id='ticketnumber_text"+id+"' value='"+ticketnumber+"'>";
document.getElementById("name_val"+id).innerHTML="<input type='text' class='form-control' id='name_text"+id+"' value='"+name+"'>";
document.getElementById("surename_val"+id).innerHTML="<input type='text' class='form-control' id='surename_text"+id+"' value='"+surename+"'>";
document.getElementById("phone_val"+id).innerHTML="<input type='text' class='form-control' id='phone_text"+id+"' value='"+phone+"'>";
document.getElementById("origin_val"+id).innerHTML="<input type='text' class='form-control' id='origin_text"+id+"' value='"+origin+"'>";
document.getElementById("tripday_val"+id).innerHTML="<input type='text' class='form-control' id='tripday_text"+id+"' value='"+tripday+"'>";
document.getElementById("bookingdate_val"+id).innerHTML="<input type='text' class='form-control' id='bookingdate_text"+id+"' value='"+bookingdate+"'>";
document.getElementById("bookedby_val"+id).innerHTML="<input type='text' class='form-control' id='bookedby_text"+id+"' value='"+bookedby+"'>";
document.getElementById("edit_button"+id).style.display="none";
document.getElementById("save_button"+id).style.display="inline";
}
function save_row(id)
{
var destination=document.getElementById("destination_text"+id).value;
var ticketnumber=document.getElementById("ticketnumber_text"+id).value;
var name=document.getElementById("name_text"+id).value;
var surename=document.getElementById("surename_text"+id).value;
var phone=document.getElementById("phone_text"+id).value;
var origin=document.getElementById("origin_text"+id).value;
var tripday=document.getElementById("tripday_text"+id).value;
var bookingdate=document.getElementById("bookingdate_text"+id).value;
var bookedby=document.getElementById("bookedby_text"+id).value;
$.ajax
({
type:'post',
url:'modify_records.php',
data:{
edit_row:'edit_row',
row_id:id,
destination_val:destination,
ticketnumber_val:ticketnumber,
name_val:name,
surename_val:surename,
phone_val:phone,
origin_val:origin,
tripday_val:tripday,
bookingdate_val:bookingdate,
bookedby_val:bookedby
},
success:function(response) {
if(response=="success")
{
document.getElementById("destination_val"+id).innerHTML=destination;
document.getElementById("ticketnumber_val"+id).innerHTML=ticketnumber;
document.getElementById("name_val"+id).innerHTML=name;
document.getElementById("surename_val"+id).innerHTML=surename;
document.getElementById("phone_val"+id).innerHTML=phone;
document.getElementById("origin_val"+id).innerHTML=origin;
document.getElementById("tripday_val"+id).innerHTML=tripday;
document.getElementById("bookingdate_val"+id).innerHTML=bookingdate;
document.getElementById("bookedby_val"+id).innerHTML=bookedby;
document.getElementById("edit_button"+id).style.display="inline";
document.getElementById("save_button"+id).style.display="none";
}
}
});
}
function delete_row(id)
{
$.ajax
({
type:'post',
url:'modify_records.php',
data:{
delete_row:'delete_row',
row_id:id,
},
success:function(response) {
if(response=="success")
{
var row=document.getElementById("row"+id);
row.parentNode.removeChild(row);
document.location.reload();
}
}
});
}
function insert_row()
{
var destination=document.getElementById("new_destination").value;
var ticketnumber=document.getElementById("new_ticketnumber").value;
var name=document.getElementById("new_name").value;
var surename=document.getElementById("new_surename").value;
var phone=document.getElementById("new_phone").value;
var origin=document.getElementById("new_origin").value;
var tripday=document.getElementById("new_tripday").value;
var bookingdate=document.getElementById("new_bookingdate").value;
var bookedby=document.getElementById("new_bookedby").value;
$.ajax
({
type:'post',
url:'modify_records.php',
data:{
insert_row:'insert_row',
destination_val:destination,
ticketnumber_val:ticketnumber,
name_val:name,
surename_val:surename,
phone_val:phone,
origin_val:origin,
tripday_val:tripday,
bookingdate_val:bookingdate,
bookedby_val:bookedby
},
success:function(response) {
if(response!="")
{
var id=response;
var table=document.getElementById("user_table");
var table_len=(table.rows.length)-1;
var row = table.insertRow(table_len).outerHTML="<tr id='row"+id+"'><td id='destination_val"+id+"'>"+destination+"</td><td id='ticketnumber_val"+id+"'>"+ticketnumber+"</td><td id='name_val"+id+"'>"+name+"</td><td id='surename_val"+id+"'>"+surename+"</td><td id='phone_val"+id+"'>"+phone+"</td><td id='origin_val"+id+"'>"+origin+"</td><td id='tripday_val"+id+"'>"+tripday+"</td><td id='bookingdate_val"+id+"'>"+bookingdate+"</td><td id='bookedby_val"+id+"'>"+bookedby+"</td><td><input type='button' class='edit_button' id='edit_button"+id+"' value='edit' onclick='edit_row("+id+");'/><input type='button' class='save_button' id='save_button"+id+"' value='save' onclick='save_row("+id+");'/><input type='button' class='delete_button' id='delete_button"+id+"' value='delete' onclick='delete_row("+id+");'/></td></tr>";
document.getElementById("new_destination").value="";
document.getElementById("new_ticketnumber").value="";
document.getElementById("new_name").value="";
document.getElementById("new_surename").value="";
document.getElementById("new_phone").value="";
document.getElementById("new_origin").value="";
document.getElementById("new_tripday").value="";
document.getElementById("new_bookingdate").value="";
document.getElementById("new_bookedby").value="";
}
}
});
}
function confirmR()
{
if(confirm('Would you like additional table row ? \n If you can\'t see changes or can\'t edit or delete just generated record after clicking OK, please refresh the page.'))
{
insert_row();
setTimeout(function(){
window.location.reload();
}, 10);
}
}
Ten plik .js dolanczam w pliku header.php
<script type="text/javascript" src="modify_records.js"></script>
jak rowniez url do biblioteki jquery.
Plik "modify_records.php" okreslony w tym pliku .js sluzy do edycji, dodania i usuniecia rekordu z bazy danych. Ogolnie chodzi mi o to sortowanie.
Nie wiem jak przekazac tw dwie zmienne z formularza