I have this table into a modal form:
<div class="form-group">
<!-- Record list -->
<table id="tabscarichi" class="table table-bordered table-striped" style='border-collapse: collapse;' >
<tr style='background: whitesmoke;'>
<th>Check</th>
<th>Id</th>
<th>Formulario</th>
<th>EER</th>
<th>Data FR</th>
<th>Q.tà</th>
</tr>
<?php
$query = "SELECT * FROM registro_c_s where stato_carico='1'";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_array($result) ){
$id = $row['id'];
$fr = $row['formulario'];
$data = $row['data_fr'];
$eer = $row['codice_eer'];
$qta = $row['quantità'];
$stato = $row['stato_carico'];
?>
<tr>
<!-- Checkbox -->
<td><input type='checkbox' class="box" name='update[]' value='<?= $id ?>' ></td>
<td><?= $id ?></td>
<td><?= $fr ?></td>
<td><?= $eer ?></td>
<td><?= $data ?></td>
<td class="qta_scarichi"><?= $qta ?></td>
</tr>
<?php
}
?>
</table>
</div>
I'm trying to write a script to sum values of class .qta_scarichi and reporting it in another input number with .qtas class.
I tried this:
$('.box').change(function(){
var total = 0;
$('.box:checked').each(function(){
total+=parseFloat($(this).parent().next('td').find('.qta_scarichi').text());
});
$('#qtà').val(total);
});
But when i check some rows..nothing happen.. #qtà is the id of the input number that i want to fill..
Problem found! in table with id="tabscarichi" i have the following script:
$(function() {
$('#tabscarichi').hide();
$('#tipo').change(function(){
if($('#tipo').val() == 'scarico') {
$('#tabscarichi').show();
console.log('0'); }
else { $('#tabscarichi').hide();
} }); }); </script> ```
i choose to show the table with a select... if i disable the script.. the css selector works! But i don't understand why..
$(this).parent().next('td') just selects <td><?= $id ?></td>, which doesn't contain .qta_scarichi.
Use $(this).closest('tr').find('.qta_scarichi').
I suspect you may have intended ".siblings('td'). .next()` only selects the immediately following element.
Related
I have inserted a file with an UPDATE command because I want my files (pdf, jpg) to be in one table together with name, course, etc. and and I just want to know if it it is possible that instead of showing the name of the filename in the table, its filename would be an href on when the person have click it, it would go in the another tab to show is content (pdf, or jpeg).
Here is my code of my addrequirements.php
<?php
include_once 'connection.php';
$result = mysqli_query($conn,"SELECT * FROM collegestud");
$result1 = mysqli_query($conn,"SELECT * FROM shsstud");
session_start();
if(!$_SESSION['login']){
header("location:index.php");
die;
}
?>
<div id="ccc">
<?php
if (mysqli_num_rows($result) > 0) {
?>
<table id="example" class="display" style="width:100%;">
<thead>
<tr>
<th>Student Id</th>
<th>Course</th>
<th>Last Name</th>
<th>First Name</th>
<th>Psa</th>
<th>Form137</th>
<th>2x2 Picture</th>
<th>Actions</th>
</tr>
</thead>
<tbody id='cccc'>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["Stud_ID"]; ?></td>
<td><?php echo $row["Course"]; ?></td>
<td><?php echo $row["Lastname"]; ?></td>
<td><?php echo $row["Firstname"]; ?></td>
<td><?php echo $row["PSA"]; ?></td>
<td><?php echo $row["Form137"]; ?></td>
<td><?php echo $row["Picture"]; ?></td>
<td><a style="color:green;" href="updatecollege1.php?id=<?php echo $row["ID"]; ?>"><i class="fas fa-folder-plus fa-2x "></i></a>
<span><a style="color:red" href="deletecollege.php?ID=<?php echo $row["ID"]; ?>"onclick="return confirm('Do you really want to delete this student?')"><i class="fas fa-user-minus fa-2x"></i></a></span>
</td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
<?php
}
else
{
echo "<p><center><strong>0 RESULTS IN THE TABLE</center></p></strong>";
}
?>
Code of my updatecollege1.php
<?php
include_once 'connection.php';
session_start();
if(!$_SESSION['login']){
header("location:index.php");
die;
}
$result = mysqli_query($conn,"SELECT * FROM collegestud WHERE id='" . $_GET['id'] . "'");
$row= mysqli_fetch_array($result);
?>
<div class = "container3">
<strong><left>PSA </left></strong>
<span> <input id="ra" type="text" value="" readonly>ADD or UPDATE</span><br>
Code of my psaadd.php
<?php
include_once 'connection.php';
session_start();
if(!$_SESSION['login']){
header("location:index.php");
die;
}
if(count($_POST)>0) {
mysqli_query($conn,"UPDATE collegestud
set PSA = '" . $_POST['PSA'] . "'
WHERE ID='" . $_POST['ID'] . "'");
$id = $_POST['ID'];
header('Location: updatecollege1.php?id='.$id);
}
$result = mysqli_query($conn,"SELECT * FROM collegestud WHERE id='" . $_GET['id'] . "'");
$row= mysqli_fetch_array($result);
?>
<form action="" method="POST" onsubmit="return confirm('Are you sure you want to save changes?');">
<div class= "container2">
<p style="font-weight: bold; font-size: 35px; text-align: center;">PSA</p>
<input type="hidden" name="ID" class="txtField" value="<?php echo $row['ID']; ?>">
<div class = "container3">
<center>
<input name="PSA" type = "file" accept = '.pdf'>
</center>
PROBLEM:
I just want to have a solution that instead it displays the filename of that file (text), it will show its filename in the table that is clickable to open its file in the another tab.
Yes, of course it possible. You should save also relative path to that PDF in database and then show link in UI part:
<!-- Start of table -->
<td>
Click to view
</td>
And your database table will have 1 additional field named path VARCHAR(256) NOT NULL with value e.g. students/some_name_of_file.pdf
Never store file content to database, it's not build to hold file contents
/*I have two table , 1st table name is bazar and 2nd table name is bazarduepayment having same columne name : sl,date,item,paid,due,remark. 'sl' is auto increment . Delete function is working perfectly . Someone please help me how to insert deleted row data in 2nd table 'bazarduepayment' Here below is code detail i wrote */
<?php
session_start();
include_once("rwdbconnection.php");
error_reporting(0);
if(isset($_POST['save']))
{
$checkbox = $_POST['check'];
for($i=0;$i<count($checkbox);$i++)
{
$del_id = $checkbox[$i];
mysqli_query($conn,"DELETE FROM bazar WHERE sl='".$del_id."'");
$message = "Data deleted successfully !";
}
}
$result = mysqli_query($conn,"SELECT * FROM bazar");
?>
<!DOCTYPE html>
<html>
<head>
<linkrel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>Delete data</title>
</head>
<body>
<div>
<?php if(isset($message)) { echo $message; } ?>
</div>
<form method="post" action="">
<table class="table table-bordered">
<thead>
<tr>
<th><input type="checkbox" id="checkAl"> Select All</th>
<th>Sl</th>
<th>Date</th>
<th>Item</th>
<th>Paid</th>
<th>Due</th>
<th>Remark</th>
</tr>
</thead>
<?php
$i=0;
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><input type="checkbox" id="checkItem" name="check[]" value="<?php echo $row["sl"]; ?>"></td>
<td><?php echo $row["sl"]; ?></td>
<td><?php echo $row["date"]; ?></td>
<td><?php echo $row["item"]; ?></td>
<td><?php echo $row["paid"]; ?></td>
<td><?php echo $row["due"]; ?></td>
<td><?php echo $row["remark"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<p align="center"><button type="submit" class="btn btn-success" name="save">DELETE</button></p>
</form>
<script>
$("#checkAl").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
</body>
</html>
First you need to copy the data from one table to another using INSERT ... SELECT syntax and only then you can delete.
You should be using prepared statements for this.
if (isset($_POST['save'])) {
// Prepared INSERT query
$stmt_insert = $conn->prepare('INSERT INTO bazarduepayment(date,item,paid,due,remark)
SELECT date,item,paid,due,remark FROM bazar WHERE sl=?');
// Prepare DELETE query
$stmt_delete = $conn->prepare('DELETE FROM bazar WHERE sl=?');
// Loop on all checkboxes selected
foreach ($_POST['check'] as $del_id) {
$stmt_insert->bind_param('s', $del_id);
$stmt_insert->execute();
$stmt_delete->bind_param('s', $del_id);
$stmt_delete->execute();
}
}
You could even simplify this to get rid of the foreach loop entirely.
I have a form where I'm submitting some values (from checkboxes) to another page and on that page I'm using an explode function to break the string inside the array. But I'm getting an additional (+1) value when I put the count() function on the explode.
HTML
<form name = "view" method = "POST" action ="cart.php">
<table align = 'center' width = '100%' border = '4'>
<tr>
<td colspan = '20' align = 'center'><h2> Viewing all the Products </h2></td>
</tr>
<tr align = 'center'>
<th>Item ID</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
</tr>
<tr align = 'center' class = "odd">
<?php
while($row = mysql_fetch_array($run))
{
$i_id = $row['item_id'];
$i_name = $row['item_name'];
$i_price = $row['item_price'];
?>
<td><?php echo $i_id; ?></td>
<td><?php echo $i_name; ?></td>
<td><?php echo $i_price; ?></td>
<?php
$item = $i_name ." ". $i_price;
?>
<td><input type="checkbox" name="addcart[]" value="<?php echo $item; ?>" onClick="return KeepCount()" />Tick</td>
</tr>
<?php }?><input type = "hidden" name = "check">
<button type= "submit" onclick = "location.href = 'cart.php';" id = "cart">Add to Cart</button> <?php } ?>
</table>
</form>
PHP (on page 2)
$prd = implode(",",$_POST['addcart']);
$final = explode(",", $prd);
for($i=0; $i<=count($final); $i++)
{
echo count($final); //this is where I'm getting the +1 to original count and hence everything falls apart.
echo $final[$i];
}
Note: I have include all the essential files like config.php and everything in the PHP file already.
Why that much extra code, directly do like below:-
foreach($_POST['addcart'] as $val){
echo $val;
}
Since count() starts counting from 1 and for loop starting from 0.
Replace this line:
for($i=0; $i<=count($final); $i++)
with
for($i=0; $i<=count($final)-1; $i++)
I am using this code to create an infinite table for my mysql queries:
<table cellspacing='0'> <!-- cellspacing='0' is important, must stay -->
<!-- Table Header -->
<thead>
<tr>
<th>User</th>
<th>SteamID</th>
<th>Banned by</th>
<th>Admin SteamID</th>
<th>Time Banned (min)</th>
<th>Reason</th>
</tr>
</thead>
<!-- Table Header -->
<!-- Table Body -->
<tbody>
<?php
echo '<tr>';
for($i = 0; $bans = mysqli_fetch_array($query2); $i = ($i+1)%3){
echo '<td>'.$bans['name'].'</td>';
echo '<td>'.$bans['steamid'].'</td>';
echo '<td>'.$bans['nameAdmin'].'</td>';
echo '<td>'.$bans['steamidAdmin'].'</td>';
echo '<td>'.$bans['time'].'</td>';
echo '<td>'.$bans['reason'].'</td>';
if($i == 2)
echo '</tr><tr>';
}
echo '</tr>';
?>
</tbody>
I got that code from Mysql fetch array, table results
It works fine, except it doesn't CORRECTLY go further down than 6 rows. The other rows for whatever reason are placed to the right of my last column as shown in this screenshot:
http://puu.sh/h0qZF/a12de1dd87.png
How can I fix this? Is there something wrong with my code? Why is it happening?
Well, your looping makes no sense. Using $i to inject new rows, like is done here, is not necessary; you can just loop over each row and then output it as a row:
<table>
<!-- <thead>...</thead> -->
<tbody>
<?php while ($bans = mysqli_fetch_array($query2)): ?>
<tr>
<td><?php echo $bans['name'] ?></td>
<td><?php echo $bans['steamid'] ?></td>
<td><?php echo $bans['nameAdmin'] ?></td>
<td><?php echo $bans['steamidAdmin'] ?></td>
<td><?php echo $bans['time'] ?></td>
<td><?php echo $bans['reason'] ?></td>
</tr>
<?php endwhile ?>
</tbody>
</table>
You are making two columns.
You have code that will print out the end of the table row every two sets of data:
if($i == 2)
echo '</tr><tr>';
It should just be echo '</tr><tr>';
Use a while loop as instructed here . So something like this:
$result = $conn->query($sql);
while($bans = $result->fetch_assoc()) {
echo '<td>'.$bans['name'].'</td>';
echo '<td>'.$bans['steamid'].'</td>';
echo '<td>'.$bans['nameAdmin'].'</td>';
}
I create a table using PHP from a database, meaning the size of the table and values vary.
The table creation:
<table>
<thead>
<tr>
<th>Ticket</th>
<th>Empresa</th>
<th>Requerente</th>
<th>Motivo</th>
<th>Data</th>
<th>Hora</th>
</tr>
</thead>
<tbody>
<form name="goTicket" action="resolver.php" method="POST" >
<?php
$sql = "QUERY";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<tr onmouseover=\"this.style.background='#EFF5FB';this.style.cursor='pointer'\"
onmouseout=\"this.style.background='white';\" onclick=\"javascript: submitform()\">";
echo "<td>$row[Ticket]</td>";
echo "<input type='hidden' name='_ticket' value='$row[Ticket]' />";
echo "<td>$row[Empresa]</td>";
echo "<td>$row[Requerente]</td>";
echo "<td>$row[Motivo]</td>";
echo "<td>$row[Data]</td>";
echo "<td>$row[Hora]</td>";
echo "</tr>";
}
echo "</form>";
echo "<tr><td colspan='6' style='text-align: center;'><form action='adminmenu.php' ><br /><input type='submit' name='woot' value='Main Menu' /></form></td></tr>";
echo "<tbody>";
echo "</table>";
?>
The Javacript function used to submit is this one:
<script type="text/javascript">
function submitform()
{
document.forms["goTicket"].submit();
}
</script>
Now whenever I click on a row it takes to the appropriate page "resolver.php" but always sends the most recent data, from last time the while was executed.
I need the value from the first cell, or the hidden input tag, that contains what I need, from the row I click.
Thank you.
Full suggestion
<script type="text/javascript">
function submitform(ticket) {
document.goTicket.elements["_ticket"].value=ticket;
document.goTicket.submit();
}
</script>
<table>
<thead>
<tr>
<th>Ticket</th>
<th>Empresa</th>
<th>Requerente</th>
<th>Motivo</th>
<th>Data</th>
<th>Hora</th>
</tr>
</thead>
<tbody>
<form name="goTicket" action="resolver.php" method="POST" >
<input type="hidden" name="_ticket" value="" />
<?php
$sql = "QUERY";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
?>
<tr onmouseover="this.style.background='#EFF5FB';this.style.cursor='pointer'"
onmouseout="this.style.background='white';"
onclick="submitform('<?php echo $row[Ticket]; ?>')">
<td><?php echo $row[Ticket]; ?></td>
<td><?php echo $row[Empresa]; ?></td>
<td><?php echo $row[Requerente]; ?></td>
<td><?php echo $row[Motivo]; ?></td>
<td><?php echo $row[Data]; ?></td>
<td><?php echo $row[Hora]; ?></td>
</tr>
<?php } ?>
</form><!-- not really nice -->
<tr><td colspan="6" style="text-align: center;"><form action="adminmenu.php" >
<br /><input type="submit" name="woot" value="Main Menu" /></form></td></tr>
</tbody>
</table>
Your problem is that you're just submitting the form without any reference to what was clicked. All the hidden inputs have the same name so the form just sends the last one.
Try the following:
echo "<tr onmouseover=\"this.style.background='#EFF5FB';this.style.cursor='pointer'\" onmouseout=\"this.style.background='white';\" onclick=\"javascript: submitform('$row[Ticket]')\">";
// delete this from while:
// echo "<input type='hidden' name='_ticket' value='$row[Ticket]'/>";
// and instead add before </form> with value=""
Then change your javascript to change the value before submitting:
function submitform(val) {
document.forms["goTicket"].elements["_ticket"].value = val;
document.forms["goTicket"].submit();
}
Kudos to mplungjan also got this as I was typing!
If I see good ... Yo set form with many of hidden inputs with same name ... what you want with this... of course it return last value ...
you have to changing names in loop and then call exact name of input