envoyer recevoir json array depuis bouton submit formulaire
Bonjour a tous et toutes
Je souhaite envoyer 1 tableau json depuis 1 bouton soumettre qui se trouve ds 1 formulaire
j'ai bien mon tab json, voici le code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| foreach ($tabNomProduitsFruits as $k => $v) {
echo $k." - ".$v;
echo "<br>";
echo "montant: ".($tabPrixProduitsFruits[$k] * $tabQuantiteDeProduitsFruitsCommandes[$k]);
$tabJsonFruits[] = array(
'nom' => $v,
'montant' => ($tabPrixProduitsFruits[$k] * $tabQuantiteDeProduitsFruitsCommandes[$k])
);
echo "<br>";
}
//=================================================================================
echo json_encode($tabJsonFruits);
echo "<br>";
echo "<br>";
// ================================================================================
$pxTotalCommandeFruits = array_sum($tabPrixDetailsFruits);
echo "<br>";
echo "le prix de votre commande de fruits s'eleve à: ".$pxTotalCommandeFruits."";
// ================================================================================
$tabJsonFruits[] = ['pxTotalCommandeFruits' => $pxTotalCommandeFruits];
$tabJsonFruits = json_encode($tabJsonFruits);
// ================================================================================
echo "<br>";
echo "le tab json composé de la commande fruits du client";
echo "<br>";
print_r($tabJsonFruits); |
le retour de print_r()
Code:
1 2
| le tab json composé de la commande fruits du client
[{"nom":"Abricots","montant":11.5},{"nom":"Fraises","montant":5.5},{"nom":"Figues","montant":15.5},{"pxTotalCommandeFruits":32.5}] |
Ensuite je crée les options de la requêtes pour envoyer le tableau json et je crée aussi le contexte
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| // creation des options de la requete pr envoyer le tab json
// et ajout du tabJsonFruits
$optionsFruits = array(
'http' => array(
'method' => 'POST',
'header' => "content-type: application/json",
'content' => $tabJsonFruits,
),
);
// creation du context avec la fonct° PHP prevue a cet effet
$contextCommandeFruits = stream_context_create($optionsFruits); |
Ensuite j'ai 1 condition qui si elle est satisfaite crée 1 formulaire avec le bouton soumettre
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| if($pxTotalCommandeFruits != 0 && $pxTotalCommandeFruits != null) {
echo "<form method='POST' action='reception_commande.php'>";
// envoi de la requete pr le tabJsonFruits
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://localhost/test_php/test04_carrousel/reception_commande.php");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $tabJsonFruits);
curl_exec($curl);
curl_close($curl);
echo "<br>";
echo "<input type='submit' />";
echo "</form>";
} else {
echo "on a pas de montant de commande";
} |
et puis pour finir j'essaye de réceptionner le tableau json dans 1 nouvelle page (reception_commande.php)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <?php
echo "test";
echo "<br>";
/*
$test = $_POST[$tabAssociatifCommandeFruits];
print_r($test);
*/
/*
print_r($_GET[$tabAssociatifCommandeFruits['prixTotal']]);
*/
/*
$test = $_GET['test'];
print_r($test);
echo "<br>";
$tabCommandeFruits= $_GET['tabAssociatifCommandeFruits'];
// print_r($tabCommandeFruits);
echo "<br>";
echo "le prix total pr la commande de fruits pr le client est de : ". $tabCommandeFruits[0];
echo "<br>";
*/
// $jsonData = file_get_contents("php://input");
/*
$jsonData = json_decode(file_get_contents('php://input'), true);
print_r($jsonData);
echo "<br>";
$tabJsonFruits= json_decode($jsonData, true);
print_r($tabJsonFruits);
echo "<br>";
echo file_get_contents('php://input');
*/
// takes raw data from the request
$json = file_get_contents('php://input');
// Converts it into a PHP object
$data = json_decode($json, true);
echo $data['nom'];
echo "<br>";
var_dump($data);
echo "<br>";
var_dump($_POST);
?> |
comme vous le voyez, j'ai essayé plusieurs choses ms là j'ai pas de résultat
Voici le retour de la page reception_commande.php
Code:
1 2 3 4
| test
NULL
array(0) { } |
Je ne sais pas ou se situe mon problème.
QQu'un aurait il (elle) 1 piste s'il vous plait.
Merci a vous
grub