Passing Form Data to separate PHP Page - php

I have a table with multiple columns (index.php). One column is a checkbox. Whenever the checkbox is checked, it displays another row where you can select a quantity. You can then hit a button called "Add to Order" and it will take you to a confirmation page (index-order.php) where I want it to display each row along with all of the data in that specified row that has the checkbox checked. Currently, I am getting no errors in my console, but no data is being displayed at all.
What do I need to change to make this happen? Here is what I have so far.
Index.php code:
<form name="form1" method="POST" action="index-order.php">
<section id="addToOrder">
<button type="submit" class="order" id="order" name="order" value="AddToOrder">Add to Order</button>
</section>
<br>
<div id="my-div2" class="ui-widget">
<div class="ui-widget">
<table id="merchTable" cellspacing="5" class="sortable">
<thead>
<tr class="ui-widget-header">
<th class="sorttable_nosort"></th>
<th class="sorttable_nosort">Loc</th>
<th class="merchRow">Report Code</th>
<th class="merchRow">SKU</th>
<th class="merchRow">Special ID</th>
<th class="merchRow">Description</th>
<th class="merchRow">Quantity</th>
<th class="sorttable_nosort">Unit</th>
<th style="display: none;" class="num">Quantity #</th>
</tr>
</thead>
<tbody>
<?php foreach ($dbh->query($query) as $row) {?>
<tr>
<td class="ui-widget-content"><input type="checkbox" class="check" name="check"></td>
<td name="rows[0][0][loc]" class="loc ui-widget-content" id="loc-<?php echo intval ($row['Loc'])?>"><?php echo $row['Loc'];?></td>
<td name="rows[0][0][rp-code]" class="rp-code ui-widget-content" align="center" id="rp-code-<?php echo intval ($row['Rp-Code'])?>"><?php echo $row['Rp-Code'];?></td>
<td name="rows[0][0][sku]" class="sku ui-widget-content" id="sku-<?php echo intval ($row['SKU'])?>"><?php echo $row['SKU'];?></td>
<td name="rows[0][0][special-id]" class="special-id ui-widget-content" align="center" id="special-id-<?php echo intval ($row['Special-ID'])?>"><?php echo $row['Special-ID'];?></td>
<td name="rows[0][0][description]" class="description ui-widget-content" id="description-<?php echo intval ($row['Description'])?>"><?php echo $row['Description'];?></td>
<td name="rows[0][0][quantity]" class="quantity ui-widget-content" data-quantity="<?php echo $row['Quantity'] ?>" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>
<td name="rows[0][0][unit]" class="unit ui-widget-content" id="unit-<?php echo intval ($row['Unit'])?>"><?php echo $row['Unit'];?></td>
<td name="rows[0][0][quant]" style="display: none;" class="quantity_num ui-widget-content"><input type="textbox" style="width: 100px;" class="spinner" name="value" id="test"></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</form>
Index-order.php:
<?php if(isset($_POST['rows'])): ?>
<table cellspacing="20">
<tr align="center">
<th>Loc</th>
<th>Report Code</th>
<th>SKU</th>
<th>Special ID</th>
<th>Description</th>
<th>Quantity</th>
<th>Unit</th>
<th>Quantity #</th>
</tr>
<?php
foreach($_POST['rows'][0] as $row):
?>
<tr align="center">
<td><?php echo $row['loc']; ?></td>
<td><?php echo $row['rp-code']; ?></td>
<td><?php echo $row['sku']; ?></td>
<td><?php echo $row['special-id']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['unit']; ?></td>
<td><?php echo $row['quant']; ?></td>
</tr>
<?php
endforeach;
?>
</table>

I am okay with the precedent answer, I never heard about this kind of method with PHP and it doesn't seems to be the right solution. Anyway, the following post would maybe help you : How to get value from td's via $_POST.
You cannot transfer datas through POST by using td ; but an alternative would be to use the "hidden" type of forms element :
<form action="script.php" method="post">
<td class=".."><input type="hidden" name="td1" value="...">value</td>
...
</form>
In PHP, you'll grab the data with the $_POST array and the td1 name :
<?php var_dump($_POST); ?>
Itwould in my opinion be the easier way to get what you want in a proper way ; the link I gave upper is also talking about DOMDocument, but it looks more complex to manage with.

Related

Image is not displaying PHP MYSQL

I want to display all of my images from the database but it seems like it's not properly displayed. I've tried many times but still not well displayed. Everything is displayed except the image.
This is the code.
<?php
include("include/connection.php");
$query = "SELECT * FROM cat";
$result = mysqli_query($conn, $query);
?>
This is the table
<form action="add_cat.php"><div align="right"><button class="btn btn-success mb-2">Add New Cat</button></div></form>
<table class="table border border-dark" width="50%" cellpadding="5" cellspacing="5">
<thead class="thead-secondary">
<tr>
<th scope="col">Num</th>
<th scope="col">Cat ID</th>
<th scope="col">Picture</th>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Gender</th>
<th scope="col">Description</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<!-- fetched data from cat table -->
<?php
$num =1;
while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<th scope="row"></th>
<td><?php echo $num; ?></td>
<td><?php echo $row['id_cat']; ?></td>
<td> <?php echo '<img src="data:img/cat;base64,' .base64_encode($row['picture']).'" style="width:100px; height:100px;">'; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['age']; ?></td>
<td><?php echo $row['gender']; ?></td>
<td><?php echo $row['description']; ?></td>
<td>
<button class="btn btn-primary">Edit</button>
<button class="btn btn-danger">Delete</button>
</td>
</tr>
<?php $num++;
} ?>
</tbody>
</table>
It might be because the mime type for the img src attribute isn't valid.
It should be something like image/jpeg, image/png, image/webp etc, depending on the format of the image stored in your database.
<tr>
...
<td><?= $row['id_cat'] ?></td>
<td><img src="data:image/png;base64, <?= base64_encode($row['picture']) ?>" style="width:100px; height:100px;"></td>
<td><?= $row['name'] ?></td>
...
</tr>
Note: The <?= $content ?> syntax is shorthand for <?php echo $content; ?> and should be supported by default if you're using PHP 5.4 or newer (You should be at least using 7.4).

How to do formatting using CSS to table

I am trying to design the table but it seems coming into proper way but for some cases. In this table, that $cal_amount and $cal_amount_key part shouldbe print whole for any numbers of cars.
Code:
PHP
<?php
$TotV=array("car","bus","jeep");
$Nopkg=array(759,159,459);
$Avg=array(20,50,15);
$Qty=array(15000,58000,58922);
$rate=array(31.73,17.15,17.25);
$gro_tot=789456;
$Cex=781254;
$S_Tot=159254;
$Vat=12500;
$frieght=0;
$G_Tot=985251;
?>
CSS
table,td,th,tr {
border:1px solid black;
border-collapse: collapse;
text-align:left;
}
HTML
<table>
<tr>
<th>S.No</th><th align="center">Variety of Goods</th><th style="width:70px">No.and Description of<br/>Packages</th><th style="width:70px">Avg. Contents per Package<br/>(Pcs)</th><th style="width:70px">Total Quantity<br/>(Pcs)</th><th style="width:70px">Rate per pc.</th><th colspan="2" style="text-align:center;">Amount<br/>(Rs.)</th>
</tr>
<?php
for($i=0;$i<count($TotV);$i++)
{?>
<tr>
<td><?php echo $i+1;?></td>
<td><?php echo strtoupper($TotV[$i]);?></td>
<td><?php echo $Nopkg[$i];?></td>
<td><?php echo $Avg[$i];?></td>
<td><?php echo $Qty[$i];?></td>
<td><?php echo $rate[$i];?></td>
<?php
}?>
<td colspan="1">Total Value</td><td style="width:100px;text-align:right;"> <?php echo $gro_tot;?></td>
<tr><td></td><td></td>
<td></td><td></td><td></td><td></td><td style="width:150px;">C.Ex.Duty 12.5%</td><td style="width:150px;text-align:right;"> <?php echo $Cex;?></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td></td><td style="width:150px;">SubTotal</td><td id="num" style="width:150px;text-align:right;"> <?php echo $S_Tot;?></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td style="width:150px;">VAT/CST 2%</td><td style="width:150px;text-align:right;"> <?php echo $Vat;?></td></tr>
<tr><td style="border:none;" colspan="3"><b>qwert</b></td><td style="border:none;"></td><td style="border:none;" colspan="2"><td><b>Freight</b></td><td style="width:150px;text-align:right;"> <STRONG><?php echo $frieght;?></STRONG></td></tr>
<tr><td style="border:none;" colspan="3"><b>asdf</b></td><td style="border:none;"></td><td style="border:none;" colspan="2"><td><b>Grand Total</b></td><td style="width:150px;text-align:right;"> <STRONG><?php echo $G_Tot;?></STRONG></td></tr>
</tr>
</table>
Please suggest some changes to be done in code so that It will start from first row instead of third row. Thanks in advance.
Try this:
First make PHP array for total calculation and label.
$TotV=array("car","bus","jeep");
$Nopkg=array(759,159,459);
$Avg=array(20,50,15);
$Qty=array(15000,58000,58922);
$rate=array(31.73,17.15,17.25);
$gro_tot=789456;
$Cex=781254;
$S_Tot=159254;
$Vat=12500;
$frieght=0;
$G_Tot=985251;
$cal_amount_key = array('Total Value',
'C.Ex.Duty 12.5%',
'SubTotal',
'VAT/CST 2%',
'Freight',
'Grand Total');
$cal_amount = array($gro_tot,
$Cex,
$S_Tot,
$Vat,
$frieght,
$G_Tot);
Put the code inside loop:
<table>
<tr>
<th>S.No</th>
<th align="center">Variety of Goods</th>
<th style="width:70px">No.and Description of<br/>Packages</th>
<th style="width:70px">Avg. Contents per Package<br/>(Pcs)</th>
<th style="width:70px">Total Quantity<br/>(Pcs)</th>
<th style="width:70px">Rate per pc.</th>
<th colspan="2" style="text-align:center;">Amount<br/>(Rs.)</th>
</tr>
<?php
for($i=0;$i<count($TotV);$i++) {?>
<tr>
<td><?php echo $i+1;?></td>
<td><?php echo strtoupper($TotV[$i]);?></td>
<td><?php echo $Nopkg[$i];?></td>
<td><?php echo $Avg[$i];?></td>
<td><?php echo $Qty[$i];?></td>
<td><?php echo $rate[$i];?></td>
<td><?php echo $cal_amount_key[$i];?></td>
<td style="width:100px;text-align:right;"><?php echo $cal_amount[$i];?></td>
</tr>
<?php } ?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td style="width:150px;">VAT/CST 2%</td>
<td style="width:150px;text-align:right;"> <?php echo $Vat;?></td>
</tr>
<tr>
<td style="border:none;" colspan="3"><b>qwert</b></td>
<td style="border:none;"></td>
<td style="border:none;" colspan="2">
<td><b>Freight</b></td>
<td style="width:150px;text-align:right;"> <STRONG><?php echo $frieght;?></STRONG></td>
</tr>
<tr>
<td style="border:none;" colspan="3"><b>asdf</b></td>
<td style="border:none;"></td>
<td style="border:none;" colspan="2">
<td><b>Grand Total</b></td>
<td style="width:150px;text-align:right;"> <STRONG><?php echo $G_Tot;?></STRONG></td>
</tr>
</table>

How to select id from the datatables using checkbox

Heres my code: HTML with Server side to get data from the database. How do I get id from the datatables using checkbox option?
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<h4>
<th></th> <!-- For checkbox column-->
<th>ID Number</th>
<th>Lastname</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Gender</th>
<th>Course</th>
<th>Department</th>
<th>Address</th>
<th>Contact</th>
<th>Birthdate</th>
<th>View</th>
<th>Events</th>
</h4>
</tr>
</thead>
<tbody>
<?php
include 'pgconnect.php';
$mydata = mysqli_query($conn,"SELECT * FROM student") or die(mysql_error());
while($record = mysqli_fetch_assoc($mydata))
{
$id=$record['id'];
?>
<tr>
<td><input type="checkbox"></td>
<td class="id"><?php echo $record['id'] ?></td>
<td class="lname"><?php echo $record['last_name'] ?></td>
<td class="fname"><?php echo $record['first_name'] ?></td>
<td class="mname"><?php echo $record['mid_name'] ?></td>
<td class="sex"><?php echo $record['gender'] ?></td>
<td class="course"><?php echo $record['course'] ?></td>
<td class="dept"><?php echo $record['department'] ?></td>
<td class="add"><?php echo $record['home_address'] ?></td>
<td class="contact"><?php echo $record['contact'] ?></td>
<td class="bdate"><?php echo $record['birthdate'] ?></td>
<td> View Records </td>
<td> <a title='Click here to update record.'href='#edit' data-toggle='modal'><i class="fa fa-pencil fa-fw"></i></a>
<a title='Click here to add record.' href='#'><i class="fa fa-plus fa-fw"></i></a>
<a title='Click here to delete record.' href='delete.php?del=<?php echo $record['id'] ?>'><i class="fa fa-trash-o fa-fw"></i></a></td>
</tr>
<?php
}
?>
</tbody>
</table>
Here's my script:
$(document).ready(function(){
$('#dataTables-example').dataTable();
});
I tried to get the ID but still it didn't works. Hope you can help me.
In your work, you get the ID in table. your should grab it from the checkbox if you use jquery, as in;
<script>
$(document).ready(function(){
document.getElementByID("ckmyid").innerHTML;
});
</script>
Note:add Selector ID in your checkbox, first, say, "ckmyid" so the code will be like this:
<input id="ckmyid" type="checkbox" value="<?php echo $record['id'] ?>">
Based on your structure, you can grab it directly from the link in your view records link if you want to place the ID there,as in;
<td> View Records </td>

post value of checkbox is empty

I am trying to get checked values in unos.php file. I get empty post but could not figure it out where is mistake.
I want to check just one checkbox and according to post value received I will perform results.
<form method="post" action="unos.php">
<button type="submit" class="btn btn-primary" name="izmjena">Izmjena</button>
<table>
<thead>
<tr>
<th data-field="artikal" data-checkbox="true" > </th>
<th data-field="id" data-sortable="true">Id</th>
<th data-field="sifra" data-sortable="true">Sifra</th>
<th data-field="name" data-sortable="true">Naziv</th>
<th data-field="mjera" data-sortable="true">Jed. mjere</th>
<th data-field="stanjesa" data-sortable="true">Stanje Sa</th>
<th data-field="stanjebl" data-sortable="true">Stanje BL</th>
<th data-field="minkolicina" data-sortable="true">Min kolicina</th>
<th data-field="kategorija" data-sortable="true">Kategorija</th>
<th data-field="status" data-sortable="true">Status</th>
<th data-field="print" data-sortable="true">P</th>
</tr>
</thead>
<tbody>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><input type="checkbox" name="artikli" value="<?.$r['ArtId'].?>"></td>
<td><?=$r['ArtId'] ?></td>
<td><?=$r['ArtSifra'] ?></td>
<td><?=$r['ArtNaziv'] ?></td>
<td><?=$r['JmNaziv'] ?></td>
<td><?=$r['ArtStanje']?></td>
<td><?=$r['ArtStanjeMo'] ?></td>
<td><?=$r['ArtMinKolic'] ?></td>
<td><?=$r['KatNaziv'] ?></td>
<td><?=$r['ArtAktivan'] ?></td>
<td><a onClick="window.open('kartica-artikla.php?id= <?=$r['ArtKategorija']?>','Websoft','width=800,height=500,left=0,top=100,screenX=0,screenY=100,menubar=yes,scrollbars=yes')" href "">
<img src="../../img/kartica.png" width="20"></td>
</tr>
<?php endwhile; ?>
</tbody>
<tfoot>
</tfoot>
</table>
file unos.php.. In this file I want to post value of checkbox
<?php
if (isset($_POST["izmjena"])) {
$id=$_POST["artikli"];
echo $id;
var_dump($id);
}
else{
}
Change:
<td><input type="checkbox" name="artikli" value="<?.$r['ArtId'].?>"></td>
To:
<td><input type="checkbox" name="artikli" value="<?php echo $r['ArtId']; ?>"></td>
Dont forget to close your form either :)
If you're using several checkboxes, you should be using an input checkbox with the following "name": "artikli[]".
The "[]" will send an array of values instead of sending a string.
Code:
<input type="checkbox" name="artikli[]" value="<?.$r['ArtId'].?>" />
checkbox's value will be present if it is checked. Do this -
$id=(!empty($_POST["artikli"])) ? $_POST["artikli"] : false;
and you are not echoing the whole html so no need to concate. Change -
<td><input type="checkbox" name="artikli" value="<? echo $r['ArtId'];?>"></td>

get the value of a checkbox then pass it to a link to edit a page

I have been working on this script that should get the value of an id field in a database through a checkbox that will be used by a link to edit a page. I know there are topics relating to this but non deals with using the value of the checkbox in a link. The checkbox is actually getting the value of the id from the database i know this because i used inspect element in firefox to view the value of the checkbox. The issue I'm having is using this value in a link to edit a page. Any help will be appreciated. My script is displayed below.
<td width="341" height="73"><h1><strong><span class="style2">EVENTS </span></strong></h1></td>
<td width="59" align="right"><div align="right"><strong>New</strong></div></td>
<td width="66" align="right"><div align="right"><strong><a href="edit_event.php?id=<?php
if (isset ($chek)) {
echo urlencode($chek);
}
?>"/>Edit</a></strong></div></td>
<td width="82" align="right"><div align="right"><strong>Archive</strong></div></td>
<td width="79" align="right"><div align="right"><strong>Unarchive</strong></div></td>
<td width="70" align="right"><div align="right"><strong>Delete</strong></div></td>
<td width="71" align="right"><div align="right"><strong>Exit</strong></div></td>
</tr>
</table>
<p> </p>
<table width="786" border="1">
<tr valign="top">
<th width="46">Event Title</th>
<th width="27"><input type="checkbox" name="checkbox1" value="checkbox" /></th>
<th width="37">Start Date</th>
<th width="36">End Date</th>
<th width="43">Start Time</th>
<th width="38">End Time</th>
<th width="43">Venue</th>
<th width="45">Event Type</th>
<th width="94">Event Description</th>
<th width="152">Event Program</th>
<th width="79">Reminder Date</th>
<th width="70">Reminder Time</th>
</tr>
<?php foreach($events as $event): ?>
<tr valign="top">
<td><?php echo $event->event_title; ?></td>
<td> <form id="form1" name="form1" method="post" action="">
<?php echo "<input type=\"checkbox\" name=\"chek[]\" value= $event->event_id id=\"chek\"/>"; ?>
</form>
</td>
<td><?php echo $event->start_date; ?></td>
<td><?php echo $event->end_date; ?></td>
<td><?php echo $event->start_time; ?></td>
<td><?php echo $event->end_time; ?></td>
<td><?php echo $event->venue; ?></td>
<td><?php echo $event->event_type; ?></td>
<td><?php echo $event->event_description; ?></td>
<td><?php echo $event->event_program; ?></td>
<td><?php echo $event->reminder_date; ?></td>
<td><?php echo $event->reminder_time; ?></td>
</tr>
<?php endforeach; ?>
<?php
$check = $_POST['chek'];
if(empty($check)){
$$check="";
}else{
$N = count($check);
for($i=0; $i < $N; $i++){
$chek =($check[$i]);
}
}
?>
I don't know if my question is understood, what i meant is that I want the value of my check box to display after (http://localhost/emgt/admin/edit_event.php?id=) id so i can edit that particular row.
You can check with the below link. It may be helpful for you.
As far I see you need to add class, and id to your checkbox and use a jquery/javascript function to call the value in your href.
Pass checkbox value to Edit (using href)
Hope it would be of some help to you.
I'm sorry, i found your code to be a little unclear. If you mean calling a url with the checkbox value as a parameter you can do so by adding an "id" property to the checkbox element, and then calling the page like:
var chk = document.getElementById("checkbox_id");
var url = "http://localhost/emgt/admin/edit_event.php?id=" + chk.value;
location.href = url;

Categories