How to pass values to an array after clicking on Submit

Asked

Viewed 238 times

0

I am working with paid market documentation, where I have a new instance/object for each product called item. Each item has to be passed to the preference->items instance where the preference->save() method will save and send a POST when I click the pay ML button. What I’m trying to do is this

  • I have a form with 3 inputs of type Submit.
  • Each input has an Hidden property and a value that corresponds to the product value
  • When the user clicks on Ubmit, which would be the same as adding to the cart, I want to take the instance/object called item and pass inside the preference->items array.

The way I’m trying is this:

<?php 
$preference = new MercadoPago\Preference();

// Cria um item na preferência
$item = new MercadoPago\Item();
$item->title = 'Produto 1';
$item->quantity = 1;
$item->unit_price = 23.00;



$item2 = new MercadoPago\Item();
$item2->title = 'Produto 2';
$item2->quantity = 1;
$item2->unit_price = 10.00;



$item3 = new MercadoPago\Item();
$item3->title = 'Produto 3';
$item3->quantity = 1;
$item3->unit_price = 1.99;
openssl_get_cert_locations();


if (isset($_POST['produto1'])) {
    $preference->items = array($item);
    $preference->save();
}
if (isset($_POST['produto2'])) {
    $preference->items = array($item2);
    $preference->save();
}
if (isset($_POST['produto3'])) {
    $preference->items = array($item3);
    $preference->save();
}

?>

As it is necessary to make use of some certifying SSL I am running this page on a free host, in case this link: https://max360vision.000webhostapp.com/playground.php

<form action="" method="POST">
    <p> Produto: <input type="text" name="produto1" hidden></p>
    <input type="submit" name="submit" value="submit">

    <p> Produto 2: <input type="text" name="produto2" hidden></p>
    <input type="submit" name="submit2" value="submit2">

    <p> Produto 3: <input type="text" name="produto3" hidden></p>
    <input type="submit" name="submit3" value="submit3">

</form>
<form action="/processar_pagamento" method="POST">
    <script src="https://www.mercadopago.com.br/integrations/v1/web-payment-checkout.js" data-preference-id="<?php echo $preference->id; ?>">
    </script>
</form>

The error that occurs to me as I am currently trying is the following: Every time I give Submit on any product, it sends 2 notices from the manager class that already comes with the market SDK

Free Notice: Undefined index: Metadata in /Storage/ssd3/656/11276656/public_html/vendor/mercadopago/dx-php/src/Mercadopago/Manager.php on line 336

Notice: Undefined index: Metadata in /Storage/ssd3/656/11276656/public_html/vendor/mercadopago/dx-php/src/Mercadopago/Manager.php on line 336

And when I click pay it redirects to pay for item 3, even if I click item 1, or 2.

2 answers

0


Well, I didn’t understand why, but after I wrote the code and tried to test it piece by piece, I did. So I noticed the logic is correct, but when I give form Ubmit, there is a Ubmit in the form of all the other buttons as if I had clicked on the 3 other buttons, after separating them into 3 different Forms, it worked, the result was this way:

if (!isset($_POST['produto1'])) {
    $preference->items = array();
}else{
    
   $preference->items = array($item);
   $preference->save();
   var_dump($preference->items);
 }

if (!isset($_POST['produto2'])) {
     $preference->items = array();
}else{
    
   $preference->items = array($item2);
   $preference->save();
   var_dump($preference->items);
} 



if (!isset($_POST['produto3'])) {
     $preference->items = array();
}else{
   $preference->items = array($item3);
   $preference->save();
   var_dump($preference->items);
    
 }


?>

<form action="" method="POST">
    <p> Produto: <input type="text" name="produto1" hidden></p>
    <input type="submit" name="submit" value="submit">
</form>
<form action="" method="POST">
    <p> Produto 2: <input type="text" name="produto2" hidden></p>
    <input type="submit" name="submit2" value="submit2">
</form>
<form action="" method="POST">
    <p> Produto 3: <input type="text" name="produto3" hidden></p>
    <input type="submit" name="submit3" value="submit3">
    </form>

0

<p> Produto 1: <input type="hidden" name="produto[]" value='1' ></p>
<p> Produto 2: <input type="hidden" name="produto[]  value='2'></p>
<p> Produto 3: <input type="hidden" name="produto[]" value='3'></p>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.