Synetrix
Synetrix

Reputation: 59

Passing input values using post

I am trying to pass the value of <input id="ids" type="hidden" name="ids" value="<?php echo $key['product_ID'];?>"> to the exchange.php page. Lets say i have apple | 1 , orange | 2, pineapple | 3. However, whenever i try to submit the values to another page, I am getting the 3 as the echo value of $product. When i try to remove the type="hidden", I get the correct value but when I try to submit, it turns out to be a different value.

<form action="exchange.php" method="post">
    <div class="row">
        <?php $query="SELECT * FROM Product" ; $data=$ MySQLi_CON->query($query); 
        foreach ($data as $key ) { ?>
        <strong>Name: </strong>
        <?php echo $productname=$ key[ 'product_Name'];?>
        <input id="ids" type="hidden" name="ids" value="<?php echo $key['product_ID'];?>">
        <strong>Status: </strong>
        <strong>Action: </strong>
        <input type="submit" value="Exchange" name="exchange_submit" class="btn btn-info btn-xs">
        <input id="id" name="id" type="hidden" value="<?php echo $id; ?>">
        <?php } ?>
    </div>
</form>

exchange.php

<?php
$id = $_POST['id']; 
$product = $_POST['ids']; 
echo $id; 
echo $product; 
exit;
?>

Upvotes: 0

Views: 1064

Answers (3)

Touheed Khan
Touheed Khan

Reputation: 2151

You are using foreach for products so I consider you've more than one products.

but when you doing this you are overwriting the $_POST['ids'].

<input id="ids" type="hidden" name="ids" value="<?php echo $key['product_ID'];?>">

thats why its showing last product_ID present in table.

Upvotes: 0

Brijesh_yadav
Brijesh_yadav

Reputation: 75

Whenever you execute a foreach loop, it will store the last output of the Database table in the hidden input of yours, and that's why no matter any input you give, it will take 3 as the id of the product.Try using a tag also.

Upvotes: 0

Gopi Chand
Gopi Chand

Reputation: 174

 <?php echo $productname=$ key[ 'product_Name'];?>
//is it working not showing error

Upvotes: 1

Related Questions