I can not understand how I can pass through scripts (below) an "action" variable and a "products" array created with php.
<?php
$actionFile = "add";
$products = array("Name product", "price", "sku", ....);
?>
<script>
function uploadProducts() {
var formData = new FormData();
formData.append("action", <?php echo json_encode($actionFile); ?>);
formData.append("products", <?php echo json_encode($products); ?>);
$.ajax({
type: 'POST',
url: './controllers/uploadProductsController.php',
data: formData,
processData: false,
contentType: false,
beforeSend: function(){
// Show image container
//$("#loader").show();
console.log("1");
},
success: function(response){
//$('.response').empty();
//$('.response').append(response);
console.log("2");
console.log(response);
},
complete: function(){
// Hide image container
//$("#loader").hide();
console.log("3");
}
});
}
</script>
uploadProductsControlle.php (code below) I print the array as a single string and no as Array.
<?php
include("../../functions.php");
$action = $_POST['action'];
$productsArr = $_POST['products'];
$response = array();
var_dump(json_encode($productsArr)); //ERROR THIS PRINT STRING
foreach($productsArr as $product) {
echo $product;
}
?>
I need "products" as an array to do a cycle. Thanks
你应该使用 json_decode 函数
$productsArr = json_decode($_POST['products']);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.