-1

I am using array to display the data after user click a button

Here is my code:

$items=array("1234"=>"Iphone 5S","1235"=>"Samsung Galaxy S4");
    $proids = $_POST['product_id'];
    echo $items[$proids];

Here is the code for products:

<div>
    <image src="ip5s.jpg">
    <p><font color="blue">&nbsp&nbspIphone 5S</font></p>
    <p><font color="red">&nbsp&nbspRM1999</font></p>
    <p><form name="addcart" method="post" action="processcart.php">
    <input type="submit" name="addtocart" value="Add to cart">
    <input type="hidden" name="product_id" value="1234" />
    <input type="hidden" name="quantity" value="1" />
    </form>
    </p>
</div>
<div>
     <image src="s4.png">
     <p><font color="blue">&nbsp&nbspSamsung Galaxy S4</font></p>
     <p><font color="red">&nbsp&nbspRM1999</font></p>
     <p><form name="addcart" method="post" action="processcart.php" >
     <input type="submit" name="addtocart" value="Add to cart">
     <input type="hidden" name="product_id" value="1235" />
     <input type="hidden" name="quantity" value="1" />
     </form>
     </p>
 </div>

However this will only show one data.For example i click on the add button for Iphone5, it will redirect to page and show me Iphone5. After that i click on Samsung, it will redirect to page and overwrite the Iphone5 and show me Samsung.

AlanK
  • 19
  • 6

2 Answers2

1

You can use quantity[], product_id[]

 <input type="hidden" name="product_id[]" value="1235" />
 <input type="hidden" name="quantity[]" value="1" />

PHP:

$product_ids = $_POST['product_id'];

 foreach($product_ids as $product_id){
      echo $items[$product_id];
  }

HTML:

 <form name="addcart" method="post" action="processcart.php">
   <div>
    <image src="ip5s.jpg">
    <p><font color="blue">&nbsp&nbspIphone 5S</font></p>
    <p><font color="red">&nbsp&nbspRM1999</font></p>
    <p>
    <input type="submit" name="addtocart" value="Add to cart">
    <input type="hidden" name="product_id[]" value="1234" />
    <input type="hidden" name="quantity[]" value="1" />

    </p>
</div>
<div>
     <image src="s4.png">
     <p><font color="blue">&nbsp&nbspSamsung Galaxy S4</font></p>
     <p><font color="red">&nbsp&nbspRM1999</font></p>
     <p>
     <input type="submit" name="addtocart" value="Add to cart">
     <input type="hidden" name="product_id[]" value="1235" />
     <input type="hidden" name="quantity[]" value="1" />

     </p>
   </div>
</form>
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

Not used same name for two component..

Instead of these use loop for display component and accepting values of component..

Append loop variable to name of every component

Sandesh
  • 349
  • 2
  • 8