I am passing a table data value within a for each loop to the php script. The problem here is that when I try to echo $exchange;. I don't get any values of the specific product name. The product name being displayed in the form is item1.
<form action="exchangeItem" method="post">
<tbody>
<?php
$query = "SELECT * FROM product";
$data = $MySQLi_CON->query($query);
foreach ($data as $key ) {
?>
<tr>
<td class="id"><h3><input type="hidden" name="productName" ><?php echo $key['product_Name'];?></h3>
</td>
<input name = "id" type = "hidden" value="<?php echo $id; ?>">
<td id="exchange_button">
<input value="Exchange" name="exchange_submit" type="submit" class="btn btn-primary btn-md raised exchange_submit">
</td>
</tr>
<?php
}
?>
</tbody>
</form>
exchangeItem.php
<?php
$exchange = $_POST['productName'];
echo $exchange;
exit;
?>
This
<td class="id"><h3><input type="hidden" name="productName" ><?php echo $key['product_Name'];?></h3>
Should be:
<td class="id"><h3><input type="hidden" name="productName" value="<?php echo $key['product_Name'];?>" ></h3>
first, edit your td
<td class="id"><h3><input type="hidden" name="productName[]" value="<?php echo $key['product_Name'];?>" ></h3>
your name => productName should be in array thats why you need bracket [ ], because you use foreach. then your post will be in array too.
exchangeItem.php
<?php
print_r($_POST['productName']);
exit;
?>
I have added & changed your 2 input name productName[] , id[] and your form action exchangeItem.php if you get product name must be used input name with array like [] because you are used foreach loop.
<form action="exchangeItem.php" method="post">
<tbody>
<?php
$query = "SELECT * FROM product";
$data = $MySQLi_CON->query($query);
foreach ($data as $key ) {
?>
<tr>
<td class="id">
<h3><input type="hidden" value="<?php echo $key['product_Name'];?>" name="productName[]" ><?php echo $key['product_Name'];?></h3>
<input name="id[]" type="hidden" value="<?php echo $id; ?>">
</td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
<tr>
<td id="exchange_button">
<input value="Exchange" name="exchange_submit" type="submit" class="btn btn-primary btn-md raised exchange_submit">
</td>
</tr>
</tfoot>
</form>
exchangeItem.php
<?php
if(isset($_POST['exchange_submit'])) {
$exchange = $_POST['productName'];
foreach($exchange as $product_name) {
echo $product_name;
}
}
?>
The problem you facing here is with this code
<td class="id">
<h3><input type="hidden" name="productName" ><?php echo $key['product_Name'];?></h3>
</td>
here in the input field you didn't have added the value, that's why when you trying to print the posted value in your action file it's showing none. So just add add value to the input field
<td class="id">
<h3><input type="hidden" name="productName" value="<?php echo $key['product_Name'];?>"></h3>
</td>
Set value for you hidden element... No need to place the hidden element in the header tag. Use productName[] for posting array of values.
<td class="id">
<h3><?php echo $key['product_Name'];?></h3>
<input type="hidden" name="productName[]" value="<?php echo $key['product_Name'];?>">
</td>
foreach all posted values to print.
<?php
$exchange = $_POST['productName'];
foreach($exchange as $key) {
echo $key;
}
exit;
?>
Update for select
Change yout html for select values..
<td class="id">
<h3><input type="checkbox" name="productName[]" value="<?php echo $key['product_Name'];?>"><?php echo $key['product_Name'];?></h3>
</td>
Related
I want to send an Id by the form in foreach loop but when I click on each button it sends me the first form.
It means when I click on the second button it sends me the value of the first form the value of input hidden is right but the first value every time sent.
<?PHP
if ($transactions) {
foreach ($transactions as $transaction) {
?>
<td class="text-left">
<?php echo $transaction['invoice_id']; ?>
</td>
<td class="text-left">
<?php echo $transaction['invoice_type']; ?>
</td>
<td class="text-left">
<?php echo $transaction['ref_id']; ?>
</td>
<td class="text-left">
<?php echo $transaction['gateway']; ?>
</td>
<td class="text-left">
<?php echo $transaction['payment_date']; ?>
</td>
<td class="text-right">
<?php
if($transaction['payment_status'] == 'not paid') {
?>
<form
method="POST"
action="<?php echo $gate_url; ?>"
>
<input type="hidden" name="invoiceId" value="<?php echo $transaction['invoice_id']; ?>">
<button class="btn btn-md btn-primary" type="submit" >
<?php echo $pay; ?>
</button>
</form>
<?php
}
?>
</td>
<?php
}
You have an array of the values, so this:
<input type="hidden" name="invoiceId" value="<?php echo $transaction['invoice_id']; ?>">
replace with:
<input type="hidden" name="invoiceId[]" value="<?php echo $transaction['invoice_id']; ?>">
I am submitting this from and in the same page getting the values of the $_POST['id] and displaying them but its getting the wrong id not the ones I am selecting from the row in the table:
<tbody>
<?php if (!empty($found_devices)) {
foreach ($found_devices as $devs) : ?>
<tr>
<td>
<input type="checkbox" id="myCheck" onclick="display_hide_selectClient(this)" name="devices[<?php echo $devs["id"] ?>]" value="<?php echo $devs["id"] ?>">
</td>
<td>
<?php echo $devs["serial_imei"] ?>
</td>
<td>
<?php echo $devs["serial_no"] ?>
</td>
<td>
<?php echo $devs["created_date"] ?>
</td>
<td>
<input hidden name="edit_device_imei" value="<?php echo $devs["serial_imei"] ?>">
<input hidden name="edit_device_serial" value="<?php echo $devs["serial_no"] ?>">
<input hidden name="edit_device_id" value="<?php echo $devs["id"] ?>">
<button type="submit" name="edit_device">Edit</button>
</td>
</tr>
<?php endforeach;
} ?>
</tbody>
So, when submitting this form with the button edit_device I am receiving the wrong values in PHP.
PHP Ccode:
if (isset($_POST['edit_device']) && !empty($_POST['edit_device_id'])) {
$get_selected_Devices = e($_POST['edit_device_id']);
$get_selected_Devices_s = e($_POST['edit_device_serial']);
$get_selected_Devices_i = e($_POST['edit_device_imei']);
}
I'm building a test shopping site using PHP / mySQL
here is my code:
the show_product.php:
<form method ="get" action="addtocart.php">
<input type="hidden" name="id_product" value='.$row['id_product'].'>
<tr>
<th colspan="2">'.$row['name'].'</th>
</tr>
<td>
<img align="center" src='.$row['photo'].' alt="">
</td>
<td>'.mb_substr($row['description'],0,200).'....</td>
<tr></tr>
<td>
'.$row['price'].'€<input type="number" name="quantity" value="1" min="1" max="20">
</td>
<td> <button type="submit" class="btn">add to cart</button></form>
the addtocart.php
session_start()
if(isset($_GET) & !empty($_GET)){
$id_product = $_GET['id_product'];
if(isset($_GET['quantity']) & !empty($_GET['quantity'])){ $quant = $_GET['quantity']; }else{ $quant = 1;}
$_SESSION['cart'][$id_product] = array("quantity" => $quant);
header('location: cart.php');
}else{
header('location: cart.php');
}
echo "<pre>";
print_r($_SESSION['cart']);
echo "</pre>";
?>
and some code of the cart.php
foreach ($cart as $key => $value) {
$cartsql = "SELECT * FROM product WHERE id_product=$key";
$cartres = mysqli_query($con, $cartsql);
$row = mysqli_fetch_assoc($cartres);
?>
<tr><form>
<td colspan="2"><?php echo $row['name'] ?></td>
<td><?php echo $row['price'].' €'; ?></td>
**<td><input type="number" class="quantity" name="quantity" value="<?php echo $value['quantity']; ?>"></td>**
<td><?php echo ($row['price']*$value['quantity']).' €'; ?> </td>
<td><img width="30" height="30" src="icons/delete.png" alt=""></td> </tr>
If i send to addcart an item of some quantity it's working.
the problem is I cannot change the quantity inside on cart.php, I know very few about Javascript.
Please any help how to do this ??
i did it with a submit form inside the cart :)
<form class="userform" method='get' name="change" action="addtocart.php">
<input type='hidden' name='id_product' value="<?php echo $row["id_product"]; ?>" />
<td><input size="5" type="number" name="quantity" value="<?php echo $value['quantity'] ?>" min="1" max="20" onchange="this.form.submit()"></td></form>
So, I have the following problem:
I have a table which I fill with the data from my MySQL database and then display on a site. I want to make it so you can change e.g. the description afterwards. Instead what I get right now is that I empty the value.
I can't see where my code goes wrong, so I'd appreciate some help. :)
HTML
<tbody>
<?php
foreach($records as $r) {
?>
<tr><?php
echo "<td>" . "<div class=table-image>" . "<img src=Assets/Images/" . escape($r->name) . " </td>". "</div>";
?>
<td><div class="data"><?php echo escape($r->name); ?></div></td>
<td><div class="data"><?php echo escape($r->location); ?></div></td>
<td><div class="data"><?php echo escape($r->partners); ?></div></td>
<td>◈ Google Maps</td>
<td class="tDesc">
<div class="desc">
<input class="form-control" value="<? echo escape($r->description); ?>" name="description" type="text">
</div>
</td>
<td>
<?php echo escape($r->date); ?>
</td>
<td>
<form method="post" action="" enctype="multipart/form-data">
<input type="submit" value="<? echo escape($r->id); ?>" name="delete">
</form>
</td>
<td>
<form method="post" action="" enctype="multipart/form-data">
<input type="submit" value="<? echo escape($r->id); ?>" name="update">
</form>
</td>
</tr>
<?php
}
?>
</tbody>
PHP
if(isset($_POST['update']) ){
$des = $_POST['description'];
$UpdateQuery = "UPDATE repo SET description = '$des', date = NOW() WHERE id ='$_POST[update]' ";
mysql_query($UpdateQuery);
};
Your fields are outside the form, only fields inside the form will be send.
<input name="i_will_not_be_send" />
<form>
<input name="i_will_be_send" />
</form>
Always escape values you put into your query string, see mysql_real_escape_string
Also read the comments about using the correct mysql library
I am trying to validate a "modify" form. The data is received from database so of course i have to put it in a while.
<?php
while ($row = mysql_fetch_array($categories))
{
?>
<form name="adminEditCategory" action="../db/adminModifyCategory.php" method="POST" enctype="multipart/form-data" >
<tr>
<td>
<input type="text" name="id" value="<?php print $row['id']; ?>" readonly="readonly" style="width: 20px;" />
</td>
<td> <input type="text" name="name<?php print $row['id']; ?>" value="<?php print $row['name']; ?>" /><span></span>
</td>
<td> <img src="../utils/getLogoCategory.php?id=<?php print $row['id']; ?>" style="max-width:40px; max-height:40px; float: left; margin-bottom: 10px;" align="left" /> </td>
<td>
<input type="file" name="attachment<?php print $row['id']; ?>" value="" style="float: right; "><span></span>
</td>
<td> <?php print date('d-m-Y', strtotime($row['added_date'])); ?>
</td>
<td>
<select width="294" name="is_deleted<?php print $row['id']; ?>" style="width: 50px;">
<?php
foreach($is_deleted as $id=>$name)
{
?>
<option value="<?php print $id; ?>" <?php if($id == $row['is_deleted']) print "selected= 'selected'"; ?> ><?php print $name; ?></option>
<?php
}
?>
</select><span></span>
</td>
<td> <input type="submit" value="Modifica" name="Edit" class="button" onClick="return validateEditCategory(<?php print $row['id']; ?>)" />
<input type="submit" value="Sterge" class="button" name="Delete" onClick="return validateDeleteCategory()">
</td>
</tr></form>
<?php
}
?>
This is the validation code:
function validateEditCategory(Id) {
var valid;
valid = true;
// alert (document.forms["adminEditCategory"]["name"+Id].name);
valid *= checkNume(document.forms["adminEditCategory"]["name"+Id]);
valid *= checkIsFileGol(document.forms["adminEditCategory"]["attachment"+Id]);
return valid > 0;
}
The validation works, but only for the first row. I can't understand why it doesn't validate the rest of the rows.
Try changing this <form name="adminEditCategory" action=
to this <form name="adminEditCategory<?php print $row['id']; ?>" action=
and
this document.forms["adminEditCategory"]
to this document.forms["adminEditCategory" + Id].
In replace, you can reduce all the ID appending to names of inputs. The point is, give all the forms different names, but inside each form you can call all the fields the same, because you already pass the ID in the <input name='id'>. But that is not necessary, just shorter and more logical.