I am trying to process form fields via POST method. I will post a long code for you to inspect, however the structure of the code is simply like this
<form> <table> <th> </th> <td> </td> </table> </form>
so it is a table inside a form. When the form is submitted, I am taken to the "cart.php" page which is expected, but that page shows the echo statement and shows the variables correctly for a split second, then shows the rest of the page but the variables in the echo statement become "undefined".
P.S: Please don't mark as duplicate with this question, thanks.
This is the first page index.php
<div class="table-responsive" id="order_table">
<form method="POST" action="cart.php" class="form-group">
<table class="table table-bordered">
<tr>
<th width="40%">Product Name</th>
<th width="10%">Quantity</th>
<th width="20%">Price</th>
<th width="15%">Sub Total</th>
<th width="5%">Action</th>
</tr>
<?php
if(!empty($_SESSION["shopping_cart"]))
{
$sub_total = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
?>
<tr>
<td><?php echo $values["product_name"]; ?></td>
<td><input type="text" name="quantity[]" id="quantity<?php echo $values["product_id"]; ?>" value="<?php echo $values["product_quantity"]; ?>" data-product_id="<?php echo $values["product_id"]; ?>" class="form-control quantity" /></td>
<td align="right">$ <?php echo $values["product_price"]; ?></td>
<td align="right">$ <?php echo number_format($values["product_quantity"] * $values["product_price"], 2); ?></td>
<td><button name="delete" class="btn btn-danger btn-xs delete" id="<?php echo $values["product_id"]; ?>">Remove</button></td>
</tr>
<?php
$sub_total = $sub_total + ($values["product_quantity"] * $values["product_price"]);
$tax = $sub_total * 0.075;
$total = $sub_total + $tax;
}
?>
<tr>
<td colspan="3" align="right"><span style="font-size:1.3em;">Tax</span></td>
<td align="right">$ <?php echo number_format($tax,2); ?></td>
</tr>
<tr>
<td colspan="3" align="right"><span style="font-size:1.3em;">Total</span></td>
<td align="right">$ <?php echo number_format($total, 2); ?></td>
<td></td>
</tr>
<tr>
<td colspan="5" align="center">
<textarea name="comments" id="comment" class="form-control" placeholder="Please enter any special instructions for the order"></textarea> <br>
<input type="submit" name="place_order" id="place_order" class="btn btn-warning" value="Place Order" />
</td>
</tr>
<?php
}
?>
</table>
</form>
And this is the beginning of cart.php:
$comment = $_POST['comments'];
echo $comment. $_POST['place_order'];
print_r($_POST);
How I know that the variables do get passed is cause I noticed some result appearing for a split second so I took a video and paused it there and it had the expected POST array, before the page suddenly appears as expected but with
"Undefined index: comments "
and
"Undefined index: place_order"
and "Array()" at the top. cart.php does not have any commands that make it refresh. Thank you.
Related
I am buildingd a library management system in PHP and HTMl.
I have some problem with the borrowing system where user can borrow books.
Here is my emitere-carti.php:
<form action="imprumutare.php" method="POST">
<table class="table table-dark">
<thead>
<tr>
<th>Imprumutare</th>
<th scope="col">Titlu </th>
<th scope="col">Autor</th>
<th scope="col">Categorie</th>
<th scope="col">Stoc</th>
</tr>
</thead>
<tbody>
<?php
$selectare="SELECT * FROM carti";
$rezultat=mysqli_query($conn,$selectare);
if(mysqli_num_rows($rezultat)>0){
while($row=mysqli_fetch_assoc($rezultat)){
$carte_nume=$row['titlu'];
$disabled=$row['stoc']>0 ? "" :"disabled";
?>
<tr>
<td><input type="submit" name="test" class="btn btn-secondary"
value="Imprumutare" <?php echo $disabled;?>></input>
<td><input type="text" name="nume" value="<?php echo $row['titlu'];?
>"></input></td>
<td><?php echo $row['autor'];?></td>
<td><?php echo $row['categorie'];?></td>
<td><?php echo $row['stoc'];?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</form>
Here is my imprumutare.php(that means borrowing):
include('conexiune.php');
//sfarsit if
//Imprumutare
if(isset($_POST['test'])){
$id=$_POST['identificator'];
$nume_carte=$_POST['nume'];
$sql_rezervare="UPDATE carti SET stoc=stoc-1 WHERE
titlu='$nume_carte' ";
if( mysqli_query($conn,$sql_rezervare)){
header('location:emitere_carti.php');
}
else{
die(mysqli_error($conn));
}
}
Here is a screenshot about what I am talking:
So my problem is both buttons are decrementeing the same value(the value of stoc).
Can someone help me?
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.
There's a table that is listed out from SQL and I have added a button on every row which I want it to link to another page.
The problem is that I would like that button to open up the correct row of data.
<div class="container">
<div class="jumpbotron">
<table class="table table-striped">
<thead>
<tr class="success">
<td>Date</td>
<td>Subject</td>
<td>Details</td>
<td>Status</td>
<td>ticket id</td>
<td>Actions</td>
</tr>
</thead>
<?php
while($row=mysqli_fetch_array($result))//while look to fetch the result and store in a array $row.
{
$date=$row[2];
$subject=$row[3];
$detail=$row[4];
$status=$row[5];
$tickid=$row[0];
?>
<form method="post">
<tbody method="post">
<td class="col-md-1"><?php print_r($date); ?></td>
<td class="col-md-1"><?php print_r($subject); ?></td>
<td class="col-sm-2"><?php print_r($detail); ?> </td>
<td class="col-md-1"><?php print_r($status); ?></td>
<td class="col-md-1"><?php echo $tickid ; ?></td>
<td class="col-md-1"><a href="detail.php" name="submit" id="submit"class="btn btn-sm btn-success">Details</td>
</tbody>
</form>
<?php } ?>
</table>
</div>
</div>
You can get the $tickid on your detail.php using $_GET['id'], in addition the user can copy the URI and share your current row's details.
<a href="detail.php?id=<?php echo $tickid; ?>" name="submit" id="submit" class="btn btn-sm btn-success">
I have the problem with update function in CodeIgniter. The cart is not updating and don't understand why. Can you help me to find a solution for this issue?
This is my Update function
public function update($in_cart = null) {
$data = $_POST;
$this->cart->update($data);
//show cart page
redirect('cart','refresh');
}
This is my form inside sidebar.php
<form action="cart/update" method="POST">
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th>QTY</th>
<th>Item Description</th>
<th style="text-align:right">Item Price</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items) : ?>
<input type="hidden" name="<?php echo $i.'[rowid]'; ?>" value="<?php echo $items['rowid']; ?>" />
<tr>
<td><input type="text" style="color:black; text-align:center;margin-bottom:5px;" name="<?php $i.'[qty]'; ?>" value="<?php echo $items['qty']; ?>" maxlength="3" size="3"></td>
<td><?php echo $items['name']; ?></td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td></td>
<td class="right"><strong>Total</strong></td>
<td class="right" style="text-align:right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<br>
<p><button class="btn btn-default" type="submit">Update Cart</button>
<a class="btn btn-default" href="cart">Go to Cart</a></p>
</form>
Try using $this->input->post() to get all form posted data.
https://ellislab.com/codeigniter/user-guide/libraries/input.html
So hi guys. I have a problem regarding a variable passing. I can't explain it clearly but here's the code. I know it'll be clearer with this.
<tbody>
<tr>
<td><?php echo $row['nameSchool'] ?></td>
<td><?php echo $row['address'] ?></td>
<td><?php echo $row['honor'] ?></td>
<td><?php echo $row['year'] ?></td>
<td width = "5%"><div id='basic-modal-2'><img src="../img/edit.png" height="22" width="22"></div></td>
<td><img src="../img/Delete.png" height="22" width="22"></td>
</tr>
<?php
}
?>
</tbody>
//some codes here.
<!-- Update Employee -->
<div id="basic-modal-content-2">
<h3>Update Information</h3>
<p></p>
</br>
<form method="post" action="add3.php">
<table id = "box-table-c2" class = "basic-modal-content-2">
<thead></thead>
<tbody>
<tr>
<td><strong><?php echo $_GET['idnum']; ?></strong></td>
<td>:</td>
<td><input type="text" class="input-xlarge" name = "NS"></td>
</tr>
<tr>
<td><strong>Address</strong></td>
<td>:</td>
<td><input type="text" class="input-xlarge" name = "ad"></td>
</tr>
<tr>
<td><strong>Honors Received</strong></td>
<td>:</td>
<td><input type="text" class="input-xlarge" name = "HR"></td>
</tr>
<tr>
<td><strong>Year Graduated</strong></td>
<td>:</td>
<td><input type="text" class="input-xlarge" name = "YG"></td>
</tr>
<tr>
<td align= "left" width= "3"><button class="btn-primary" type="submit" style = "cursor: pointer";>Submit</button></td>
</tr>
</tbody>
</table>
</form>
</div>
They are both on the the same php file (educinfo.php)
My error is that i can't pass the value of the "EDIT" link to the simplemodal.
Thank You guys.
You can pass value in PHP using many ways. one way is passing value in Query String. That is you can pass value after file name with question mark. just like key value passing
phpfilename.php?key1=value1&key2=value2
You can pass any number of key and value. After that you can access in next page using key value.
In HTML Part:
<a href="filename.php?QueryString=valueOne" title = "Edit" class = "basic">
<img src="../img/edit.png" height="22" width="22">
</a>
In php you can get like this
$Idvalue = isset($_GET['QueryString']) ? $_GET['QueryString'] : "";
Please let me know if you have any issue for passing value like this