I have a table in my form which gets its data from a MYSQL database. I am trying to make it dynamic by allow the user to add more information, however I'm having difficulties in adding an extra row directly under the last row of the query.This is the code i have so far and my attempt at adding a new row using JQuery:
<h4>Manage Courses</h4>
<div class="container">
<div class = "container-fluid">
<div id = "table_container" style="margin-top:50px;" class="mainbox col-md-6">
<div class="row clearfix">
<div class="col-md-12">
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr >
<!-- <th class="text-center">
#
</th> -->
<th class="text-center">
Course Code
</th>
<th class="text-center">
Course Title
</th>
<!-- <th class="text-center">
Edit
</th>
<th class="text-center">
Delete
</th> -->
</tr>
</thead>
<?php foreach ($courses as $row) {
echo "<tr><td>";
echo $row['course_code'];
echo "</td><td>";
echo $row['course_title'];
echo "</td>";
echo "</tr>"; }
?>
<tbody>
<tr id='addr0'>
<td>
1
</td>
<td>
<input type="text" name='code' placeholder='Code' class="form-control"/>
</td>
<td>
<input type="text" name='title' placeholder='Title' class="form-control"/>
</td>
<td>
<p
data-placement="top"
title="Edit"></a>
<button class="btn btn-primary btn-xs"
data-title="Edit"
data-toggle="modal"
data-target="#edit" >
<span class="glyphicon glyphicon-pencil"></span></button></p>
</td>
<td>
<p
data-placement="top"
data-toggle="tooltip"
style="margin-left:5px"
itle="Delete">
<button class="btn btn-danger btn-xs"
data-title="Delete"
data-toggle="modal"
data-target="#delete" >
<span class="glyphicon glyphicon-trash"></span></button></p>
</td>
</tr>
<tr id='addr1'></tr>
</tbody>
</table>
</div>
</div>
<a id="add_row" class="btn btn-default pull-left">Add Row
<span class="glyphicon glyphicon-plus"></span>
</a>
<a id='delete_row' class="pull-left btn btn-default">Delete Row
<span class="glyphicon glyphicon-trash"></span>
</a>
</div>
</div>
</div>
and this is the Jquery to support the add and delete buttons:
$(document).ready(function(){
var i=1;
$("#add_row").click(function(){
$('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='code"+i+"' type='text' placeholder='Code' class='form-control input-md' /> </td><td><input name='title"+i+"' type='text' placeholder='Title' class='form-control input-md'></td>");
$('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
i++;
});
$("#delete_row").click(function(){
if(i>1){
$("#addr"+(i-1)).html('');
i--;
}
});
});
The code adds one row underneath the last row but them adds the second row above it for some reason.
Another issue i am encountering is the ability to replicate the glyphicons for the delete and edit for each row. I would really like these to be added for each row and for the new rows being added.
Any help would be much appreciated! :)
In the html you provided there is no "#tab_logic". You are also creating rows outside of the <tbody> and <thead> options. You also are not creating html for the glyphicons in your jQuery function.
Edit: sorry there is a tab_logic. But, you need to make sure you include the html for the glypicon. A better way to do this might be to clone an existing element and insert it.
I'm not seeing the additional row being inserted as you describe:
http://jsfiddle.net/pekv2tx0/
Also: fix your HTML, this is what is causing the problems, note the closing anchor tag with no body, inside of a p tag, and you have an empty row, so it's not inserting an extra, it's already in the html:
<p
data-placement="top"
title="Edit"></a>
<button class="btn btn-primary btn-xs"
data-title="Edit"
data-toggle="modal"
data-target="#edit" >
<span class="glyphicon glyphicon-pencil"></span></button></p>
</td>
<tr id='addr1'></tr>
inside your click function, try appending an entire row to the tbody:
$("#add_row").click(function(){
$('#tab_logic tbody').append("<tr><td>"+ (i+1) +"</td><td><input name='code"+i+"' type='text' placeholder='Code' class='form-control input-md' /> </td><td><input name='title"+i+"' type='text' placeholder='Title' class='form-control input-md'></td></tr>");
i++;
});
Related
I'm new to php and I'm trying to make a theatrical school management app.
I have a section that I create a table with all students of a project and I want every row in the table to have a button that opens a modal with all project weeks and a checkbox to check those that payed the weekly cost.
I tried to make it work with the code bellow but only the last record opens the modal. The previews records doesn't do anything. If I add another record in the table only the new record opens the modal. Please help!
My code:
<tbody id="firstClass">
<?php
if( mysqli_num_rows($result) > 0){
// we have data
// output the data
while( $row = mysqli_fetch_assoc($result) ) {
$id = $row["id"];
/* print_r($id); */
echo "<tr>";
echo "<td>" . $row['id'] . "</td><td>" . $row['firstname'] . "</td><td>" . $row['lastname'] . "</td><td>" . $row['Age'] . "</td><td>" . $row['email'] . "</td><td>" . $row['phone'] . "</td><td>" . $row['sign_date'] ."</td>";
echo '<td><button type="button" class="btn btn-warning btn-sm" data-bs-toggle="modal" data-bs-target="#payments-' . $id . '">
<i class="fas fa-euro-sign"></i>
</button></td>';
echo '<td><a href="edit_student.php?id=' . $row['id'] .'" type="button" class="btn btn-primary btn-sm">
<i class="far fa-edit"></i>
</a></td>';
echo '<td><a href="delete_student.php?id=' . $row['id'] .'" type="button" class="btn btn-danger btn-sm delete">
<i class="fas fa-trash-alt"></i>
</a></td>';
echo "</tr>";
}
} else {// if no entries
echo "<div class='alert alert-warning'>Δεν έχετε εγγραφές!</div>";
}
// mysqli_close($conn);
?>
<tr>
<td colspan="11"><div class="text-center"><i class="fas fa-plus"></i> Προσθήκη εγγραφής</div></td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="payments-<?php echo $id; ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Πληρωμή</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<!-- <?php echo $id ;?> -->
<div class="table-responsive">
<table class="table" id='myTable'>
<thead>
<tr>
<th class="table-cell text-center" scope="col">Week 1</th>
<th class="table-cell text-center" scope="col">Week 2</th>
<th class="table-cell text-center" scope="col">Week 3</th>
<th class="table-cell text-center" scope="col">Week 4</th>
<th class="table-cell text-center" scope="col">Week 5</th>
<th class="table-cell text-center" scope="col">Week 6</th>
<th class="table-cell text-center" scope="col">Week 7</th>
<th class="table-cell text-center" scope="col">Week 8</th>
</tr>
</thead>
<tbody>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
</tbody>
</table>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<?php
include('includes/footer.php');
?>
You've to write your modal into the WHILE loop too that you are using for fetching data to add an individual modal for each row. Otherwise only the last id will find the modal. Format your code like this:
<?php
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
?>
<tr>
<td><?= $id ?></td> //<?= ?> is the short form of <?php echo '' ?>
.................
</tr>
<!-- Modal -->
<div class="modal fade" id="payments-<?php echo $id; ?>" tabindex="-1"
aria-labelledby="exampleModalLabel" aria-hidden="true">
..................
</div>
<?php }
} else { ?>
.............
<?php } ?>
I just went through your question and your code. As far as I can understand you want your modal to display the details of a particular student's payment records on which a user clicks.
Unfortunately, you have used a loop inside which you have defined a variable $id and the value of $id gets changed every time the loop iterates. So, in the end, $id holds the value of the very last record from the database.
As per your code, the generated HTML structure would be something like this(considering the record of five students):
<tbody>
<tr>
<td><button type="button" data-bs-toggle="modal" data-bs-target="#payments-1">xyz</button></td>
</tr>
<tr>
<td><button type="button" data-bs-toggle="modal" data-bs-target="#payments-2">xyz</button></td>
</tr>
<tr>
<td><button type="button" data-bs-toggle="modal" data-bs-target="#payments-3">xyz</button></td>
</tr>
<tr>
<td><button type="button" data-bs-toggle="modal" data-bs-target="#payments-4">xyz</button></td>
</tr>
<tr>
<td><button type="button" data-bs-toggle="modal" data-bs-target="#payments-5">xyz</button></td>
</tr>
</tbody>
<div class="modal fade" id="payments-5">
.....
</div>
Here modal will get the id as 5 just because $id will hold the value of the last record. As the id from the last row of the table matches with the id of the modal, the modal only opens up from the last row.
So, this was the problem with the code. Hope you have understood what was wrong with your code. Now let's come to the solution part of this.
For this, I would recommend you to open the modal using jquery. Where you'll open the modal using a class selector and pass student_id in the modal using the data attributes.
Sharing a snippet for better help.
$('.open-modal').on('click', function() {
var student_id = $(this).data('student_id');
$('#student_id').val(student_id);
$('.modal').modal('toggle');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<table>
<tbody>
<tr>
<td><button type="button" class="open-modal" data-student_id='1'>Student 1</button></td>
</tr>
<tr>
<td><button type="button" class="open-modal" data-student_id='2'>Student 2</button></td>
</tr>
<tr>
<td><button type="button" class="open-modal" data-student_id='3'>Student 3</button></td>
</tr>
</tbody>
</table>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<form>
<!–– form to be used for saving the data with student id as hidden input -->
Student ID: <input type="text" id="student_id">
</form>
</div>
</div>
</div>
</div>
This will let you create only one modal for n number of students.
i am new in both php and jquery. i want to use span to hide and show content in html and php. can i use jquery? if yes, what should i do? i already put the jquery script to the bottom of page. this is my code :
<!-- reply -->
<span title="fas fa-reply" name="reply" data-id="1"
</span>
<!-- if span clicked show all these data... -->
<table class="table table-borderless" style="margin-top:50px;">
<thead class="bg-secondary pb-0">
<tr>
<th colspan="3" class="pb-0">
<p class="test text-light">Creating Reply <?php echo $rowAllPost["id"]; ?>
</p></th>
<th class="pb-0 text-right">
</th>
</tr>
</thead>
<th scope="row" style="width:15rem!important;">
<img src="<?php echo $rowViewUserNow["picture"];?>" class="rounded-circle" style=height:93px;width:81px;><br>
<p class="ml-3"> Online</p>
<p class="ml-2"> <?php echo $rowViewUserNow["username"];?>
</p>
<i class="fas fa-info-circle pr-2 pt-2"></i>Active
<td colspan="3" class="text-justify">
<form action="controller/doCheckPost.php" method="post">
<textarea name="description" class="form-control"></textarea>
</td>
</tr>
<tr class="bg-secondary pb-0" >
<th colspan="3" class="pb-0">
<button title="Cancel" class="btn" type="submit" name="cancel"><i class="fas fa-times"></i></th></button>
<th class= "pb-0 text-right">
<button title="Submit" class="btn" name="submit"><i class="fas fa-check"></i></button>
</form>
</th>
</th>
</tr>
</div>
</tbody>
</table>
The best way would be to first give an id to both the span and table elements. Then you can use an event handler in jQuery like this:
$('#spanId').on('click', function() {
$('#tableId').toggle();
});
Toggle will show the element if it's hidden and hide it if it's visible.
I want to know how should we use div tags inside a table in PHP code. This is my code. Please tell me how to use it properly.
<tbody>
<?php
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
echo "<tr>
<td>{$row['id']}</td>
<td><img width='90px' height='90px' src='imageView.php?id=".$row["id"]."' /> </td>
<td>{$row['item_name']}</td>
<td>{$row['description']}</td>
<td>{$row['quantity']}</td>
<td>echo '<div class="col-md-1 col-sm-3 col-xs-6 c-cart-qty">
<div class="c-input-group c-spinner">
<input type="text" class="form-control c-item-1" value="1">
<div class="c-input-group-btn-vertical">
<button class="btn btn-default" type="button" data_input="c-item-1">
<i class="fa fa-caret-up"></i>
</button>
<button class="btn btn-default" type="button" data_input="c-item-1">
<i class="fa fa-caret-down"></i>
</button>
</div>
</div>
</div>
</td>';
<td> <button>submit</button> </td>
</tr>";
}?>
</tbody>
I have tried using inverted comma's and braces but still, it shows error. What needs to be done here?
You can write the div inside table. Don't put all table inside php tag. remove html outside the <?php ?> php code. after that whenever you want to echo any value just used the php.
Customize your code like below,
<table>
<tbody>
<?php
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><img width='90px' height='90px' src='imageView.php?id=<?php echo $row["id"]; ?>' /> </td>
<td><?php echo $row['item_name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td>
<div class="col-md-1 col-sm-3 col-xs-6 c-cart-qty">
<div class="c-input-group c-spinner">
<input type="text" class="form-control c-item-1" value="1">
<div class="c-input-group-btn-vertical">
<button class="btn btn-default" type="button" data_input="c-item-1">
<i class="fa fa-caret-up"></i>
</button>
<button class="btn btn-default" type="button" data_input="c-item-1">
<i class="fa fa-caret-down"></i>
</button>
</div>
</div>
</div>
</td>
<td> <button>submit</button> </td>
</tr>
<?php } ?>
</tbody>
</table>
Within a double quote you can use a single quote
<tbody>
<?php
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
echo "<tr>
<td>{$row['id']}</td>
<td><img width='90px' height='90px' src='imageView.php?id=".$row["id"]."' /> </td>
<td>{$row['item_name']}</td>
<td>{$row['description']}</td>
<td>{$row['quantity']}</td>
<td><div class='col-md-1 col-sm-3 col-xs-6 c-cart-qty'>
<div class='c-input-group c-spinner'>
<input type='text' class='form-control c-item-1' value='1'>
<div class='c-input-group-btn-vertical'>
<button class='btn btn-default' type='button' data_input='c-item-1'>
<i class='fa fa-caret-up'></i>
</button>
<button class='btn btn-default' type='button' data_input='c-item-1'>
<i class='fa fa-caret-down'></i>
</button>
</div>
</div>
</div>
</td>;
<td> <button>submit</button> </td>
</tr>";
}?>
</tbody>
I posted another question for issue and it was partially solved. I have a table with a single link on each row. The link calls AJAX and outputs in another Bootstrap column. This works fine. When I click the link, the class "highlight" is applied which changes the background color of the row. When I click another link I want the previously clicked clink to have the "highlight" class removed and the new row to have the class applied.
Currently, the add class works but any other link click get the class added but the class is never removed.
<table class="table table-hover unpadded" id="mashed_tab">
<thead>
<tr>
<th class="artcl_hdr text-center" colspan="2">
<p class="big_date top_pad"><i class="fa fa-rss fa-lg concrete"></i> Articles/Keywords for
2016-07-09 - 2016-07-10</p>
</th>
<th><span class="badge bkgcol-wet-asphalt">55</span></th>
</tr>
</thead>
<tbody class="mashed_body">
<tr class="mashed_row">
<td class="bkgcol-sunflower wht-border">
<div class="date_cell">
<span class="asphalt long_date">Sat</span><span class="white day">Jul 09, 2016</span>
</div>
</td>
<td class="linked-title dark unpadded">
<div class="cell_link">
link 1
</div>
</td>
<td class="small-cell">
<i class="fa fa-arrow-right"></i>
</td></tr><tr class="mashed_row">
<td class="bkgcol-sunflower wht-border">
<div class="date_cell">
<span class="asphalt long_date">Sat</span>
<span class="white day">Jul 09, 2016</span>
</div>
</td>
<td class="linked-title dark unpadded">
<div class="cell_link">
Link 2
</div>
</td>
<td class="small-cell">
<i class="fa fa-arrow-right"></i>
</td>
</tr>
<tr class="mashed_row">
<td class="bkgcol-sunflower wht-border">
<div class="date_cell">
<span class="asphalt long_date">Sat</span>
<span class="white day">Jul 09, 2016</span>
</div>
</td>
<td class="linked-title dark unpadded">
<div class="cell_link">
Link 3
</div>
</td>
<td class="small-cell">
<i class="fa fa-arrow-right"></i>
</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
attachEvents();
});
function attachEvents() {
var selClass = 'highlight';
$('#mashed_tab tr a').click(function() {
$(this).parent().parent().removeClass(selClass);
$(this).parent().parent().addClass(selClass);
});
};
</script>
Try something like this in your on click function and let me know.
$('tr a').click(function() {
$("#mashed_tab tr").find("." + selClass).removeClass(selClass);
$(this).parent().parent().addClass(selClass); });
I have a form inside a bootstrap modal which is called from class and triggered by a button inside a table. The problem is that the form is not submitting.
Here is my class (localhost/project/includes/func/function.php)
class product{
function selectProducts($conn){
$select_prod_query= "SELECT * FROM `tbl_products` ORDER BY `prod_id` DESC";
$select_prod_query_result = $conn->query($select_prod_query) or die("Unable to view products. Press F5 to retry!");
while($row=$select_prod_query_result->fetch_assoc()){
if($row['prod_img']!="no image available"){
$row['prod_img'] = "<a href='#' data-featherlight='".$row['prod_img']."'><img src='".$row['prod_img']."' height='50px' width='auto'></a>";
}
else{
$row['prod_img']="<img src='includes/img/no_image.png' height='50px' width='auto'>";
}
if($row['prod_description']==NULL){
$row['prod_description']="No description";
}
$option_html = "
<button type='button' data-toggle='modal' data-target='#editproduct".$row['prod_id']."' class='btn btn-primary btn-xs'><span class='glyphicon glyphicon-edit'></span> Edit</button>
<div class='modal fade' id='editproduct".$row['prod_id']."' role='modal' aria-labeledby='editproduct".$row['prod_id']."label'>
<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>Edit Product #".$row['prod_id']."</h4>
</div>
<div class='modal-body'>
<form action='http://localhost/xampp/wci-jpms_version_4/index.php' method='POST'>
<label class='label label-primary' for='prod_name'>Product Name</label>
<input type='text' name='prod_name' value='".$row['prod_name']."' class='form-control' required><br>
<label class='label label-primary' for='prod_category'>Product Category</label>
<input type='text' name='prod_category' value='".$row['prod_category']."' class='form-control' required><br>
<label class='label label-primary' for='prod_description'>Product Description</label>
<input type='text' name='prod_description' value='".$row['prod_description']."' class='form-control'><br>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-danger' data-dismiss='modal' aria-label='Close'>Close</button>
<button type='submit' class='btn btn-success' name='btn_edit_product'>Save</button></form>
</div>
</div>
</div>
</div>";
$prod_data = array('prod_id'=>$row['prod_id'], 'prod_name'=>$row['prod_name'], 'prod_category'=>$row['prod_category'], 'prod_img'=>$row['prod_img'], 'prod_description'=>$row['prod_description'], 'prod_date_added'=>$row['prod_date_added'], 'option'=>$option_html);
$prod_data_arr[] = $prod_data;
}
return json_encode($prod_data_arr);
}
since I've used the Bootstrap-table library, I have to retrieve the data as JSON.
localhost/project/includes/json/product_json.php
<?php
namespace wci_jpms;
require("../func/function.php");
$obj_wci_jpms_db = new wci_jpms_db();
$objProduct = new product();
$conn = $obj_wci_jpms_db->connect();
echo $objProduct->selectProducts($conn);
$obj_wci_jpms_db->close($conn);
?>
and here is my index.php
<table data-toggle="table" id="table"
data-url="includes/json/product_json.php"
data-side-pagination="client"
data-show-refresh="true"
data-page-list = "[5, 10, 20, 50, 100, 200, 500, ALL]"
data-pagination="true"
data-search="true"
data-search-time-out="600"
data-height="500"
data-striped="true"
data-pagination-first-text="<< First"
data-pagination-pre-text="< Prev"
data-pagination-next-text="Next >"
data-pagination-last-text="Last >>">
<thead>
<th data-field="prod_id" data-sortable="true" data-valign="middle">Product ID</th>
<th data-field="prod_name" data-sortable="true" data-valign="middle">Product Name</th>
<th data-field="prod_category" data-sortable="true" data-valign="middle">Product Category</th>
<th data-field="prod_img" data-searchable="false" data-valign="middle">Product Image</th>
<th data-field="prod_description" data-searchable="false" data-valign="middle">Product Description</th>
<th data-field="prod_date_added" data-searchable="false" data-valign="middle">Date Added</th>
<th data-field="option" data-searchable="false" data-valign="middle">Option</th>
</thead>
</table>
any wrong with my code? thanks! :D
The form Tag can't be placed like that, change it to this:
Also use "index.php" instead of "http://localhost/xampp/wci-jpms_version_4/index.php"
<form action='index.php' method='POST'>
<div class='modal-body'>
...
</div>
<div class="modal-footer">
...
</div>
</form>
I think this should do the trick.