PHP function suggestion need - php

I am little confused in making one function in my PHP page. Actually I want make one function which can transfer value from Filed A to filed B.
My current code is like below
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>S.No.</th>
<th>Profile</th>
<th>Username</th>
<th>Email</th>
<th>Name</th>
<th>Mobile</th>
<th>Requested Credit</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php if(mysqli_num_rows($result) > 0) { ?>
<?php $s=1; while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $s;?></td>
<td><img class="img-thumbnail" style="height:50px;width:50px;" src="<?php echo $row['user_image']; ?>" alt="userimg" ></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td><?php echo $row['request_redeem']; ?></td>
<td> <button onClick="reply(<?php echo $row['user_id'];?>);" class="btn btn-xs " type="button" ><i aria-hidden="true" class="fa fa-thumbs-down"> Approve</i></button> </td>
</tr>
<?php $s++; } ?>
<?php } ?>
</tbody>
in this request_redeem is value which I want transfer in filed called Credit. Both filed is in same database table. Can anyone please suggest me what should I do for it ? I am newbie and learning PHP yet...so Please let go my foolish questions if you think. Thanks

You should call $.ajax function on click on button.Like this:
$.ajax({ url: '/my/site',
data: {action: 'test'},
type: 'post',
success: function(output) {
alert(output);
}
});
Through ajax function on given url, you can create function there and update value in that function and return response that would update value in above view.

Manish has given good example for sending the ajax request . You write a SQL query on the request url to update those values.

Related

Table Rows are not shown inside the tbody tag

I am looking to insert rows to the table but
The rows are echoed but not displayed inside the table.
The HTML code of the project is:
<div class="container">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>SN</th>
<th>Category</th>
<th>Parent</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody id="displayCategory">
</tbody>
</table>
</div>
The main.js file of the project is:
It fetches data from the PHP file below:
$(document).ready(function () {
manageCategory();
function manageCategory(){
$.ajax({
url: DOMAIN + "/includes/process.php",
method: "POST",
data: { manageCategory: 1 },
success: function (data) {
$("#displayCategory").html(data);
alert(data);
console.log(data);
}
})
};
}
PHP code of the project is:
<?PHP
if (isset($_POST["manageCategory"])) {
$m = new Manage();
$result = $m->manageTableWithPagination('pdcts_categories', 1);
$rows = $result['rows'];
$pagination = $result['pagination'];
if (count($rows) > 0) {
foreach ($rows as $row) {
$n = 0;
?>
<tr>
<td><?php echo ++$n; ?></td>
<td><?php echo $row["Category"]; ?></td>
<td><?php echo $row["Parent"]; ?></td>
<td>
Active
</td>
<td>
Edit
Delete
</td>
</tr>
<?php
}?>
<!-- <tr><td colspan="5"><?php echo $pagination; ?></td></tr> -->
<?php exit(); }
}?>
Try appending data (insted of html) like so:
$("#displayCategory").append(data);
note: check in the console that your php code is returning valid html (<tr> data)

Hiding table row based on incremental php variable attached to the row

How can I hide a particular <tr> based on php variable associated with that <tr>(incremental variable $i) with jQuery when the table is being populated dynamically through Mysql. I have looked around but not getting any clue.
<table id="example">
<thead>
<tr>
<th>S.No.</th>
<th>Item Number</th>
<th>Question</th>
<th>Option-A</th>
<th>Option-B</th>
<th>Option-C</th>
<th>Option-D</th>
<th style="text-align: center;">Correct Ans.</th>
<th style="text-align: center;">Marks</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<tbody>
<?php
if ($query != '') {
$res = mysql_query($query) ;
$i = 1;
while($row = mysql_fetch_array($res))
{
extract($row);
?>
<tr class="record">
<td><?php echo $i ; ?></td>
<td><?php echo $item_no ; ?></td>
<td><?php echo $level_id ; ?></td>
<td><textarea disabled name="question" rows="4" cols="35"><?php echo $question ; ?></textarea></td>
<td><?php echo $option_A ; ?></td>
<td><?php echo $option_B ; ?></td>
<td><?php echo $option_C ; ?></td>
<td><?php echo $option_D ; ?></td>
<td><?php echo $correct_ans ; ?></td>
<td><?php echo $marks ; ?></td>
<td> </td>
<script type="text/javascript" >
$(function() {
$(".delbutton").click(function() {
var del_id = $(this).attr("id");
var info = 'id=' + del_id;
var $tr = $(this).closest('tr');
if (confirm("Sure you want to delete this post? This cannot be undone later.")) {
$.ajax({
type : "POST",
url : "delete_entry.php", //URL to the delete php script
data: info,
success : function(response) {
if(response=='deletion success'){
$tr.find('td').fadeOut(1000,function(){ $tr.remove(); });
}
}
});
}
return false;
});
});
</script>
</tr>
<?php $i++; } }
</tbody>
</table>
And my ajax page,
<?php
header('Content-Type: application/json');
session_start();
require("../config.php");
require("../Database.class.php");
require("../site.php");
$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$fnc=new site_functions($db);
$id = $_POST['id'];
$deleted_date = date("Y-m-d h:i:s");
$deleted_by = $_SESSION['session_admin_id'] ;
$nots = $db->idToField("tbl_ques","notes",$id);
if ($nots == "")
{
$date_string = "last deleted on|".$deleted_date."|" ;
}
else {
$date_string = $nots."last deleted on|".$deleted_date."|" ;
}
$fnc->update_is_not_deleted_for_Pearsonvue("tbl_ques",$id, "$deleted_date", $deleted_by);
$notes_data = array("notes"=>$date_string);
if($db->query_update("tbl_ques", $notes_data, "id=$id")){
http_response_code();
echo json_encode('deletion success');
}else{
http_response_code(204);
}
?>
You are already close - in fact it looks like your code should work on the first click at least. Anyway,
Change line
$tr.find('td').fadeOut(1000,function(){ $tr.remove(); });
to
$('#' + del_id).closest('tr').remove(); });
What this does is find the delete button element in the DOM, then look 'up' the DOM structure to the first TR element, and remove that from the DOM.
It is generally better to rely on simple variables within async callbacks because relying on objects, such as $this or in your case $tr can cause issues where the object pointed to by the $tr variable is not the one you expected.
EDIT: Added the working snippet below to illustrate the technique. If you still have a problem please create a minimal verifiable version as a snippet from your code so that we can pinpoint the issue.
$('.del').on('click', function() {
$(this).closest('tr').remove();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="example">
<thead>
<tr>
<th>First name</th>
<th>Second name</th>
<th>Age</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>Joe</td>
<td>Bloggs</td>
<td>22</td>
<td style="text-align: center;">
<button id='A1' class='del'>Delete</button>
</td>
</tr>
<tr>
<td>Jane</td>
<td>Bloggs</td>
<td>19</td>
<td style="text-align: center;">
<button id='A2' class='del'>Delete</button>
</td>
</tr>
<tr>
<td>Joanne</td>
<td>Bloggs</td>
<td>28</td>
<td style="text-align: center;">
<button id='A3' class='del'>Delete</button>
</td>
</tr>
</tbody>
</table>

Fetch and dispaly through a modal database contents of selected in a datatable row when button is click

I have a datatable which retrieves data from db, i want to be to implement a modal pop up under the row improvement actions so instead displaying the actual improvement actions i want like a symbol under that row so when i click it a modal appears with the improvement actions while all the other data remains the same , i want to implement this using ajax but am quite a novice in ajax please assist.
<table class="table table-hover table-striped table-bordered datatable-basic" >
<thead>
<tr>
<tr class="bg-violet-400" >
<th>ID</th>
<th>Site</th>
<th>Date</th>
<th>SN</th>
<th>Gap identified</th>
<th>Improvement Actions</th>
<th>Timeline</th>
<th>Person responsible</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
$connect = mysqli_connect("localhost", "root", "", "dqa");
$query=mysqli_query($connect,"SELECT * FROM action_plan");
while($row=mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['site'] ?></td>
<td><?php echo $row['date'] ?></td>
<td><?php echo $row['sn'] ?></td>
<td><?php echo $row['gap_identified'] ?></td>
<td><?php echo $row['Improvement_Actions'] ?></td>
<td><?php echo $row['timeline'] ?></td>
<td><?php echo $row['person_responsible'] ?></td>
<td><?php if( $row['status'] == 1 )
{
echo ' <button type="button" class="btn bg-grey">Approved</button></span>';
}
elseif( $row['status'] == 0 ) {
echo ' <button type="button" class="btn btn-danger">Active Action</button></span>';
}
elseif( $row['status'] == 2 ) {
echo ' <button type="button" class="btn bg-brown"> Resubmission </button></span>';
}
elseif( $row['status'] == 3 ) {
echo ' <button type="button" class="btn bg-orange">Action Due</button></span>';
}
?></td></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
Without any sample data or example data, I can only speculate at what you are trying to accomplish. Here is an example that I think you may find helpful.
$(function() {
$(".table").DataTable();
$(".table tbody .actions").button({
showLabel: false,
icon: "ui-icon-extlink"
}).click(function(e) {
var self = $(this);
var row = $(this).attr("href").substring(1);
var siteurl = "<?php echo siteurl('improvement');?>";
var form = $("<form>", {
id: "form-" + row
});
form.append($("<input>", {
type: "hidden",
name: "rowid",
value: row
}));
var dialog = $("<div>", {
title: "Improvement Actions - Row " + row
}).append(form).dialog({
modal: true,
create: function(e, ui) {
$.getJSON(siteurl + "/" + row, function(data) {
$('[name="rowid"]', this).val(data.id);
});
},
close: function(e, ui) {
self.data("results", form.serialize());
dialog.dialog("destroy").remove();
}
});
});
});
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.js"></script>
<table class="table table-hover table-striped table-bordered datatable-basic">
<thead>
<tr>
<tr class="bg-violet-400">
<th>ID</th>
<th>Site</th>
<th>Date</th>
<th>SN</th>
<th>Gap identified</th>
<th>Improvement Actions</th>
<th>Timeline</th>
<th>Person responsible</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>
1
</td>
<td>
LOC-1
</td>
<td>
04/24/2018
</td>
<td>
1001
</td>
<td>
0.1
</td>
<td>
Improvement Actions
</td>
<td>
</td>
<td>
John Smith
</td>
<td>
<button type="button" class="btn bg-grey">Approved</button>
</td>
</tr>
</tbody>
</table>
Assuming you are using DataTable, The table is populated with various data from PHP, resulting in HTML like shown above. You can then use jQuery UI to program the button to build a dialog. One of the callback events for .dialog() is create, see more: http://api.jqueryui.com/dialog/#event-create
You can perform your AJAX call at this time to get specific data for this dialog. In my example, all of it is being created dynamically, so there is no need to reset forms etc.
I advise using $.getJSON(), but there is nothing wrong with the $.ajax() code you implemented. The $.getJSON() simply assumes you are performing a GET call to a resource that will return JSON data. Resulting in simplified code.
Testing of this example is not complete as the PHP Script URL is fake or will be null.
Hope that helps.

JQuery multiple variables sent in url

I have searched for hours now to find out how I could do this, but unfortunately all to no avail.
I am trying to send entered information so it can run through MySQL and obtain the information, then echo it in table on screen.
The issue (as far as I can tell) must be with my JQuery code:
$("#btnCheckNoteIDs").click(function(){
var noteUser = $("#noteUser").val();
var noteDate = $("#noteDate").val();
$("#LoadNoteIDs").load('check_noteIDs.php?noteDate='+noteDate + "&noteUser="+noteUser);
});
My php code is as follows:
$result = mysqli_query($con,"
SELECT ID, ClientID, Note, ToDoDate, CaseID
FROM ToDoNotes
WHERE ToDoStatus='0' and Deleted='0' and `ToDoDate`='".$_GET['noteIDsDate']."' and User='".$_GET['noteIDsDate']."'");
while($row = mysqli_fetch_array($result))
{
$ID = $row['ID'];
$ClientID = $row['ClientID'];
$Note = $row['Note'];
$ToDoDate = $row['ToDoDate'];
$CaseID = $row['CaseID'];
}
?>
<table class="table table-striped">
<thead>
<tr>
<th>Note ID</th>
<th>Client ID</th>
<th>Case ID</th>
<th>Note</th>
<th>To Do Date</th>
</tr>
</thead>
<tbody>
<tr>
<td><? echo $noteID; ?></td>
<td><? echo $ClientID; ?></td>
<td><? echo $CaseID; ?></td>
<td><? echo $Note; ?></td>
<td><? echo $ToDoDate; ?></td>
</tr>
</tbody>
</table>
Can anyone here offer any assistance please? Any help will be greatly appreciated!
Thanks!
Suppose you have form.php like below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form method="post" id="btnCheckNoteIDs">
<label> User</label>
<input type="text" name="user" id="noteUser"><br>
<label>Date</label>
<input type="date" name="date" id="noteDate"><br>
<input type="submit" value="submit">
</form>
<br>
<div id="LoadNoteIDs"></div>
</body>
</html>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous"></script>
<script>
$(document ).ready(function() { //make sure document is ready
$('#btnCheckNoteIDs').submit(function(e){
e.preventDefault();//to prevent default behaviour
var noteUser = $("#noteUser").val();
var noteDate = $("#noteDate").val();
if(noteUser==undefined){
alert('No noteuser');
return false;
}else if(noteDate==undefined){
alert('no note date');
return false;//stop sending
}
//to convert %2F, use decodeURIComponent
var param= 'noteDate='+decodeURIComponent(noteDate) + '&noteUser='+noteUser;
$.ajax({
url: "check_noteIDs.php",
data: param,
type: "post",
success: function(data){
$('#LoadNoteIDs').html(data);
}
});
});
});
</script>
in check_noteIDs.php which must be in the same folder where you put the form.php
require_once "dbconfig.php";
$user= isset($_REQUEST['noteUser'])? $_REQUEST['noteUser']:'';
$date = isset($_REQUEST['noteDate'])? date('Y-m-d', strototime($_REQUEST['noteDate'])):'';
//You can validate here.
$result = mysqli_query($con," SELECT ID, ClientID, Note,
ToDoDate, CaseID
FROM `ToDoNotes`
WHERE `ToDoStatus`='0' AND `Deleted`='0'
AND `ToDoDate`='$date'
AND `User`='$user'");
$row = mysqli_fetch_assoc($result);
<table class="table table-striped">
<thead>
<tr>
<th>Note ID</th>
<th>Client ID</th>
<th>Case ID</th>
<th>Note</th>
<th>To Do Date</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['ClientID']; ?></td>
<td><?php echo $row['CaseID']; ?></td>
<td><?php echo $row['Note']; ?></td>
<td><?php echo $row['ToDoDate']; ?></td>
</tr>
</tbody>
</table>
<?php
mysqli_free_result($result);
mysqli_close($con);
?>
I think your issue is the submit button in your form, submits the form before the script executes. If that's the case, you need to update your script as below:
$("#btnCheckNoteIDs").click(function(event){
event.preventDefault();
var noteUser = $("#noteUser").val();
var noteDate = $("#noteDate").val();
$("#LoadNoteIDs").load('check_noteIDs.php?noteDate='+noteDate + "&noteUser="+noteUser);
});

How can I implement show() hide() function using jQuery in Codeigniter based on onclick of button in a table?

I need to have it so extra information pops up onclick and is otherwise hidden. I have tried using show() and hide() but I am unsure of what I am doing. Thanks guys, Kieran
View:
<table class="table table-striped">
<thead>
<tr><th>Last name</th><th>First name</th><th>Practice</th><th>County</th><th>Email</th><th></th></tr>
</thead>
<tbody>
<?php foreach ($health_professional as $hp): ?>
<tr>
<td><?php echo $hp['last_name'] ?></td>
<td><?php echo $hp['first_name'] ?></td>
<td><?php echo $hp['practice'] ?></td>
<td><?php echo $hp['county'] ?></td>
<td><?php echo $hp['email'] ?></td>
<td><a class="btn btn-info" href="hp/view/<?php echo $hp['hpid'];?>">Expand</a></td>
</tr>
<thead>
<tr><th>Permission to send info?</th><th>Registered</th><th>Attendance</th><th>Date of last update</th></tr>
</thead>
<tr id ="trid">
<td><?php echo $hp['send_info']; ?></td>
<td><?php echo $registration_count; ?></td>
<td><?php echo $attendance_count; ?></td>
<td><?php echo $hp['send_info_last_update']; ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
One problem that you are going to run into is that:
<tr id="trid">
is not going to be a unique id. Since you have an id associated with each $health_professional you can change it to:
<tr id="tr-<?php echo $hp['hpid'];?>" class="hidden">
Now that you have unique id's and a generic class, set up your link like so:
<td><a class="expand" data="<?php echo $hp['hpid'];?>">Expand</a></td>
Now in javascript you can call
$(document).ready(function(){
$('.expand').click(function () {
var id = $(this).attr('data');
$('#tr-' + id).show();
});
});
and in your css:
.hidden{display:none;}
Here is a hardcoded example (terrible styling aside):
http://jsfiddle.net/4qbFQ/
I can't see where you have provided the code where your button is, but you could simply put:
onclick="$('#extra_info_div_id').toggle();"
In the code for the button.
Here's how I would do it.
<script type="text/javascript">
$(document).ready(function(){
$('#trid').hide();
$('#expand').click(function(){
$('#trid').show();
});
});
</script>
...
<td><a class="btn btn-info" id="expand" href="javascript:;">Expand</a></td>

Categories