0

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;
?>
4
  • id should be unique. Currently you are setting id="ids" inside foreach, please change it. Commented Apr 15, 2017 at 4:53
  • same thing goes for id="id". Commented Apr 15, 2017 at 4:55
  • id and ids are two different thing. id is the current user ID, ids is the product ID Commented Apr 15, 2017 at 4:56
  • if you are selecting all the products then running foreach to iterate over them. That means you are going to multiple create input by the id="id" or id="ids" which is wrong. id attribute should be unique inside whole page. we cannot assign same id to any other html element. Commented Apr 15, 2017 at 5:02

3 Answers 3

1
 <?php echo $productname=$ key[ 'product_Name'];?>
//is it working not showing error
Sign up to request clarification or add additional context in comments.

4 Comments

yes, it is working. Not showing error. Everything is working except for the $product = $_POST['ids']; Doesn't seem to display the correct output
You want multiple ids or single product id??
single product id, apple | 1 , orange | 2, pineapple | 3
but whenever for example i click on apple. It gives me id of 3
0

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.

1 Comment

Thanks for the suggestion, but the answer above solved my problem :)
0

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.

2 Comments

Thanks for the suggestion, but the answer above solved my problem :)
@user3676202 no worries :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.