jquery load data break modal - javascript

I have more and more problems :(
I try get a php table witch i use datatable.js for it to reload data without refresh page.
Data was load true jq with load:
$(document).ready(function () {
data_th1();
//data tablice
function data_th1(){
setInterval(function () {
$('#tablica_home_1').load('ajax/data.php')
});
}
});
And data is loaded and i can refresh it with function data_th1() BUT when data was loaded i have one + button for open modal to add some coments, ehh problem was that modal after load data true jq not work?
HTML CODE WHEN DATA WAS LOAD (index.php)
<div id="tablica_home_1"></div>
SCRIPT IN INDEX.PHP FOR LOAD
<script>
$(document).ready(function () {
data_th1();
//data tablice
function data_th1(){
setInterval(function () {
$('#tablica_home_1').load('ajax/data.php')
});
}
});
</script>
DATA.PHP
<?php
require_once("../includes/inc_files.php");
//tablica poziva
$sql7 = "SELECT * FROM svi_pozivi WHERE calltype='Outbound' AND status = 'NO ANSWER' OR calltype='Inbound' AND status = 'NO ANSWER' ORDER BY datum DESC";
$result7 = $database->query($sql7);
?>
<table id="example" class="display responsive-table datatable-example">
<thead>
<tr style="text-transform: uppercase;">
<th>ID</th>
<th>Pozivatelj</th>
<th>Primatelj</th>
<th>Datum</th>
<th>Status poziva</th>
<th>Komentar</th>
<th>Obrada</th>
<th>Funkcije</th>
</tr>
</thead>
<tbody>
<?php while ($row7 = $database->fetch_array($result7)){ ?>
<tr>
<td><?php echo $row7['id']; ?></td>
<td>
<?php
if ($row7['calltype'] == 'Outbound'){
echo $row7['src'];
}
else{
echo realbroj_ul($row7['src']);
echo ' <a href="index.php?stranica=imenik-add&broj=realbroj_iz($row7["dst"])" alt="Dodaj u imenik"><i class="material-icons" style="margin-top: -4px;position: absolute;color: blue;margin-left: 5px;">add_circle</i>';
}
?></td>
<td>
<?php
if ($row7['calltype'] == 'Outbound'){
echo realbroj_iz($row7['dst']);
echo '<a href="index.php?stranica=imenik-add&broj=realbroj_iz($row7["dst"])" alt="Dodaj u imenik"><i class="material-icons" style="margin-top: -4px;position: absolute;color: blue;margin-left: 5px;">add_circle</i>';
}
else{
echo $row7['dst'];
}
?></td>
<td><?php echo realdatum($row7['datum']); ?></td>
<td><?php echo realstatus($row7['status']); ?></td>
<td>
<?php
//komentar
$sql8 = "SELECT * FROM komentari WHERE call_id = '$row7[id]'";
$result8 = $database->query($sql8);
$row8 = $database->fetch_array($result8);
if ($row8['id'] != ''){
echo $row8['komentar'];
}
else{
echo 'Nema komentara';
echo '<a data-toggle="modal" class="modal-trigger" data-id="'.$row7["id"].'" href="#komentarM" alt="Kreiraj Komentar"><i class="material-icons" style="margin-top: -4px;position: absolute;color: blue;margin-left: 5px;">add_circle</i>';
}
?></td>
<td><?php echo statuskomentara($row7['k_status']);?></td>
<td>7</td>
</tr>
<?php } ?>
</tbody>
</table>
AND MODAL IN INDEX.PHP
<div id="komentarM" class="modal bottom-sheet">
<div class="modal-content">
<h4>KOMENTAR</h4>
<p>Dodajte svoj komentar</p>
</div>
<form id="koment_post">
<input type="hidden" name="id" value="">
<input type="hidden" name="agent" value="<?php echo $ime2; ?>">
<input style="width=80%;" type="text" name="komentar" placeholder="Unesite Vaš komentar...">
<div class="modal-footer">
<button type="submit" class=" modal-action modal-close waves-effect waves-green btn-flat">SPREMI</button>
</div>
</form>
</div>
ONE AGAIN: I have in data.php table with modal call a href, but when i call data.php with jq laod function modal was not work. I need to fix that modal show,

I am not using modal with ajax load, i am searching a lot of site and this problem was not solved.

Maybe i'm too late, but, just in case for you or another person who may want to know how to fix this:
The explanation for this, it's in here
I've used the element BODY to access my element, like this:
$('body').on('click', '.Class',function () {});
$('body').on('click', '#Id',function () {});
This are a few examples, but it worked for me. I hope it'll help u.

Related

My JavaScript pop up modal only works on the first row

First question: I have a dynamic table, with edit icon on every row, when pressing the icon a popup modal should show up. But the popup modal only work on the first row. I tried to determine the problem and I think it’s because of the id "open5", that it’s duplicated and only work for one row which is the first row. How can I make it appear on every row?
Second question: how can I use the book id to determine which book going to be updated, when the user press the button on the update modal?
trackingPage.php
<!-- dynamic table -->
<div class="container">
<table class="_table">
<thead>
<tr>
<th>الغلاف</th>
<th>العنوان</th>
<th>المؤلف</th>
<th>تاريخ البدء</th>
<th>تاريخ الإنتهاء</th>
<th>التقدم</th>
<th width="50px">
</th>
</tr>
</thead>
<tbody id="table_body">
<?php
$query = "SELECT * FROM `tracking` WHERE `username_tracking`='$_SESSION[username]'";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><img style='width:80px; height: 85px; border-radius: 10px; margin: 0.1px;' src="upload/<?php echo $row['cover'] ?>"></td>
<td><?php echo $row["book_name"] ?></td>
<td><?php echo $row["author_name"] ?></td>
<td><?php echo $row["start_date"] ?></td>
<td><?php echo $row["end_date"] ?></td>
<td>
<div class='progress-container'><progress value='<?php echo $row["current_page"] ?>' max='<?php echo $row["page_number"] ?>'></progress></div>
</td>
<td>
<div class='action_container'>
<a href='delete_book.php?book_id=" <?php echo $row["book_id"] ?> "' class='danger' onclick='remove_tr(this)'><i class='fa fa-close'></i></a>
<a class='success' id='open5'>
<i class='fa fa-plus'></i>
</a>
</div>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<div class="modal-container-tracking" id="modal_container5">
<div class="modal-tracking">
<form class="book-form" method="post" action="trackingPage.php" enctype="multipart/form-data">
<!-- Modal content -->
<div class="closeIcon-tracking" id="close5">
<span class="close-tracking">×</span>
</div>
<div class="modalContent-tracking">
<h3>تحديثات الكتاب</h3>
<input type="number" name="current_page" placeholder="عدد الصفحات المقروءة" class="pages" min='1' required />
<input name="end_date" placeholder='تاريخ الانتهاء' type="date" class="start-date" required />
<textarea class="review" name="review" placeholder="مراجعة الكتاب"></textarea>
<button href="" name="update_book" class="bn60" style="text-decoration: none;">حدِّث الكتاب</button>
</div>
</form>
</div>
</div>
<script>
//Popup page
const open5 = document.getElementById('open5');
const modal_container5 = document.getElementById('modal_container5');
const close5 = document.getElementById('close5');
open5.addEventListener('click', () => {
modal_container5.classList.add('show');
});
close5.addEventListener('click', () => {
modal_container5.classList.remove('show');
});
//end of Popup page
</script>
This is my DB:
Users table
Tracking table
Your JavaScript code is looking for an element with the ID open5, HTML id's should be unique, but your PHP code renders an element with the same ID for every row, ergo the JS code is satisfied when it finds the first element with ID open5 and therefore all subsequent ones do not get event listeners attached. You have to switch to a class and then attach a listener to each of those links, e.g. something like this:
document.querySelectorAll('tr .success').forEach(function(el, i, nodeList) {
el.addEventListener('click', () => {
modal_container5.classList.add('show');
});
}

Display alert on button submission

So I have built a very basic application system. I have made one page where I can see all the applications in a table via MySQL query. I have also made so I can click a button to either approve or deny the application. Dependning on which button I press, the row with that specific user will be moved do the approved or denied table in the database. But, I want to display an alert when pressing the different buttons at the same time they send the row id to the PHP code.
Here is my code for the application table site: (register.php)
<?php
$query = "SELECT * FROM ansokningar";
$query_run = mysqli_query($link, $query);
?>
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th> ID </th>
<th> Förnamn </th>
<th>Efternamn </th>
<th>Mejladress</th>
<th>Ålder</th>
<th>Ansökt som</th>
<th>Läs Ansökning</th>
<th>Godkänn</th>
<th>Neka</th>
</tr>
</thead>
<tbody>
<?php
if(mysqli_num_rows($query_run) > 0)
{
while($row = mysqli_fetch_assoc($query_run))
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fornamn']; ?></td>
<td><?php echo $row['efternamn']; ?></td>
<td><?php echo $row['mejladress']; ?></td>
<td><?php echo $row['alder']; ?></td>
<td><?php echo $row['intresseradavyrket']; ?></td>
<td>
<form action="register.php" method="post">
<input type="hidden" id="deleteid" name="delete" value="<?php echo $row['id']; ?>">
<center> <button id="delete" type="submit" name="delete" class="btn btn-danger btn-circle btn-sm">
<i class="fas fa-trash"></i></button></center>
</form>
</td>
</tr>
<?php
}
}
else {
echo $ingaansokning;
}
mysqli_close($link);
?>
And here is my PHP code for moving the row: (neka.php)
<?PHP require '../db/dbconfig.php';
if(isset($_POST['id']))
{
$id = $_POST['id'];
$query = "
INSERT INTO nekade SELECT * FROM ansokningar WHERE id='$id';
DELETE FROM ansokningar WHERE id='$id';
";
$query_run = $link->multi_query($query);
if($query_run)
{
$_SESSION['success'] = '';
header('Location: ../../register.php');
exit();
How do I send the row ID from the first page to the second and display one alert when it's done?
I have tried this on the first page (register.php) but it doesn't work:
<script>
$(function(){
$('#delete').click(function() {
var id = <?php echo $row['id']; ?>;
$.ajax({
type: 'POST',
url: 'includes/ansokningar/neka.php',
data:{id: id},
success: function(data){
Swal.fire(
'Grattis!',
'Din ansökan är nu skickad!',
'success'
)
}
})
})
});
(Without the ajax stuff it works to move the user to the other table)
Thanks!

PHP - Notice: Undefined index: and not displaying data

this is code of search.php. i want to take search by date from the input box as below.
<form action="visitor-print.php">
<div class="col-sm-3 text-center"><h3>Date</h3>
<input type="text" class="form-control" name="dat" placeholder="Enter Date">
<p class="help-block">Month Format 1,2,3,....29,30..</p>
<button class="btn btn-default"><span>Submit</span></button>
</div>
</form>
this is my visitor-print.php code where i get undefined index error on dat (which is from the other page)
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('visitor_list');
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM list1 WHERE d1 = '". $_POST["dat"] . "' ";
$retval = mysql_query( $sql,$conn ) or die("Error: ".mysql_error($conn));
?>
<div class="container">
<h3 class="text-center">User Feed-Back Details</h3><br />
<div class="table-responsive" id="employee_table">
<table class="table table-bordered">
<tr>
<th width="10%">Visitor Name</th>
<th width="10%">Address</th>
<th width="10%">Area to Visit</th>
<th width="10%">Phone No.</th>
<th width="20%">Want to meet with </th>
<th width="50%">Purpose of meeting</th>
</tr>
<?php
while($row = mysql_fetch_array($retval,MYSQLI_BOTH))
{
?>
<tr>
<td><?php echo $row['nm']; ?></td>
<td><?php echo $row['add1']; ?></td>
<td><?php echo $row['area_vis']; ?></td>
<td><?php echo $row['y1']; ?></td>
<td><?php echo $row['app_ty']; ?></td>
<td><?php echo $row['no_per']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
<div align="center">
<button name="create_excel" id="create_excel" class="btn btn-success">Create Excel File</button>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$('#create_excel').click(function(){
var excel_data = $('#employee_table').html();
var page = "excel.php?data=" + excel_data;
window.location = page;
});
});
</script>
Problem:
Notice: Undefined index: dat in D:\wamp\www\radissun visitor\visitor-print.php on line 12
and is not showing data from mysql
The answer you are looking for: You are using $_POST - which is just for forms which have method="post". Default method is GET - so your variables are $_GET[]. You can either set the method parameter in the html to POST, or you change your PHP code to GET.
But I must point out, that your code is not usable for productive environments, as it is vulnerable to SQL Injection. See http://www.w3schools.com/sql/sql_injection.asp
MISSING METHOD ON FORM METHOD="POST"
NOTE: IF YOU USE METHOD="POST" YOU CAN GET VALUE BY $_POST['dat']; IF YOUR NOT USING METHOD ATTRIBUTE .FORM JUST SUBMITED AS GET METHOD YOU CAN GET BY $_GET['dat'];
<form action="visitor-print.php" METHOD="POST" >
MISSING FORM SUBMIT
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="SUBMIT" >
(OR)
ADD TYPE="SUBMIT" TO BUTTON
<button TYPE="SUBMIT" class="btn btn-default"><span>Submit</span></button>

Why is JQuery click function not working in succeeding pages of dataTable?

I have this code that displays a list of topics with delete and edit buttons in a datatable.
Display.php
<table data-toggle="table" class="table table-striped table-hover">
<thead>
<tr>
<th><input type="checkbox" name="all_topic"></th>
<th>Topic</th>
<th>Date Created</th>
<th>Date Update</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<?php
$topics = array();
$faq = new Faq;
$topics = $faq->all();
?>
<?php $i = 1;
foreach($topics as $topic) : ?>
<tr id="tr-id-<?php echo $i; ?>" class="tr-class-<?php echo $topic->id; ?>" data-topic-id="<?php echo $i; ?>">
<td><input type="checkbox" name="topic[]" value="<?php echo $topic->id; ?>" class="faq-checkbox"></td>
<td id="td-id-<?php echo $i; ?>" class="td-class-<?php echo $i; ?>">
<?php echo $topic->topic; ?>
</td>
<td><?php echo $topic->date_created; ?></td>
<td><?php echo $topic->date_updated; ?></td>
<td>
<a class="btn btn-info" href="faqs-edit-topic.php?id=<?php echo $topic->id; ?>">Edit</a>
<button class="btn btn-danger" data-toggle="modal" data-target="#faq-delete-modal-<?php echo $topic->id; ?>" >Delete</button>
<div id="faq-delete-modal-<?php echo $topic->id; ?>" class="modal fade" tabindex="-1" role="dialog" >
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Delete Topic</h4>
</div>
<div class="modal-body">
<p>Delete <b><i><?php echo $topic->topic; ?></i></b> ? </p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-danger faq-delete-topic" data-modal-id="faq-delete-modal-<?php echo $topic->id; ?>" data-topic-id="<?php echo $topic->id; ?>" >Delete </button>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
script.js
$(document).ready(function(){
$('.faqs table').dataTable({
searching: false,
"order": [[3, 'desc']],
"columns": [
{ orderable: false, width: "10%" },
null,
null,
null,
{ orderable: false, width: "20%" }]
});
$("button.faq-delete-topic").click(function(){
var $topic_id = $(this).attr("data-topic-id");
var modal_id = "#" + $(this).attr("data-modal-id");
$.post('faqs-delete-topic.php',{'topic_id': $topic_id}, function(success){
console.log(success);
if(success) {
// $("table tbody tr[data-topic-id="+ $topic_id +"]").remove();
$(modal_id).modal('hide');
$('.faqs table').DataTable().row(".tr-class-"+$topic_id).remove().draw(false);
display_message('Topic successfully deleted.');
}
});
});
});
Now when I have a long list of topics, datatable automatically creates a pagination to show the list of topics at a time. Whenever I try to delete a topic using the delete button, it shows the modal and then clicking the delete button on the modal deletes the topic, but only on the first page of the datatable. If I try to go to, say the next page of the datatable, the click function in script.js seems that it doesn't recognize the delete action on the modal. I can only delete on the first page on the datatable, but not on the succeeding pages.
What seems to be the problem with this one? Please help. Thanks guys.
It will only bind those on the 1st page as they are the only ones it can select at the time
Try using
$("table").on("click","button.faq-delete-topic",function(){
var $topic_id = $(this).attr("data-topic-id");
var modal_id = "#" + $(this).attr("data-modal-id");
disable_delete1_button();
$.post('faqs-delete-topic.php',{'topic_id': $topic_id}, function(success){
console.log(success);
if(success) {
// $("table tbody tr[data-topic-id="+ $topic_id +"]").remove();
$(modal_id).modal('hide');
$('.faqs table').DataTable().row(".tr-class-"+$topic_id).remove().draw(false);
display_message('Topic successfully deleted.');
remove_disable_delete1_button();
}
});
You might need to bind the click to an ancestor of table e.g. $("body").on("click","button.faq-delete-topic",...); depending on how the paging works
Your current logic only applies the click handler to the visible rows. Whenever you change the page, the UI is refreshed and new rows appear, with no click handler applied.
Use the createdRow event of datatable to initialize your buttons, just as it's presented here:
https://www.datatables.net/examples/advanced_init/row_callback.html
There are multiple other examples worth checking if you're interested in more advanced models. I would also recommend the paging using REST WebService that returns only the amount of pages needed at the moment, instead of getting all of them at once. It's much faster solution for more data.

How to get value table row value using jquery/ajax

<?php
include "config.php";
// Fetch the data
$con = mysql_query("select * from product");
?>
<div style=" height:250px; overflow:auto;">
<table class="table table-striped table-bordered dTableR" id="ajxtable">
<tr>
<th>Jobno</th>
<th>Product</th>
<th>Qty</th>
<th>Designed by</th>
<th>Action</th>
</tr>
<?php
while ($row = mysql_fetch_array($con)) {
$id = $row['id'];
$pcode = $row['pcode'];
$lproduct = $row['lproduct'];
$mrprate = $row['mrprate'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $lproduct; ?></td>
<td><?php echo $mrprate; ?></td>
<td><?php echo "admin"; ?></td>
<td><input type="button" id="addmorePOIbutton1" style="width:25px;" value="+" onClick=""/>
<input type="button" id="delPOIbutton1" style="width:25px;" value="-" onClick=""/></td>
</tr>
<?php
}
?>
</table>
</div>
This is ajax page. Below table is auto refreshed every 5 seconds.
My doubt is how to get that particular row value.
When i click table row + button, get these particular row values, and place to index page text box and this '+' button also hide.
Kindly suggest any jquery or ajax code for adding below code.
I am new to jquery ,,anybody help me with sample code..that would help me greately. Thanks in advance
$(document).ready(function () {
$('#yourtablename tr').click(function (event) {
alert($(this).attr('id')); //trying to alert id of the clicked row
});
});
Above code may be help you and another solution is:
$('td').click(function(){
var row_index = $(this).parent().index();
var col_index = $(this).index();
});

Categories