【问题标题】:How to read Key and value array in php when passing from Ajax Post从Ajax Post传递时如何在php中读取键和值数组
【发布时间】:2017-07-04 01:39:06
【问题描述】:

我有一个键和值数组,我通过 ajax 调用传递给 PHP 函数。 如何使用 $_POST 在 PHP 端读取数组?

                var eImages = [{
                    ProductID: ProductID,
                    Image: image1Name, 
                    ImagePath: image1Path
                },
                {
                   ProductID: ProductID,
                   Image: image2Name, 
                   ImagePath: image2Path         
                },
                {
                   ProductID: ProductID,
                   Image: image3Name, 
                   ImagePath: image3Path
                }];

              $.ajax({
                url: "adminProcess.php/",
                method: "post",
                data:{
                    images: eImages,
                    action: 'SaveImages'
                },
                dataType: "text",
                success: function(strMessage){
                    $('#Message').text(strMessage)
                    //fnClearControls();
                }
            })

【问题讨论】:

标签: javascript php ajax


【解决方案1】:
<?php
    if($_POST["action"] == "SaveImages") {
        yourFunctionToSave($_POST["images"]);
    }

    function yourFunctionToSave($data) {
        foreach($data as $product) {
            $ProductID = $product['ProductID'];
            $Image = $product['Image'];
            $ImagePath = $product['ImagePath'];
        }
    }
?>

数据存储为$_POST 数组。

【讨论】:

    【解决方案2】:

    您可以通过以下方式访问产品详细信息(在后端PHP页面中),

    foreach($_POST['images'] as $product){
        $ProductID = $product['ProductID'];
        $Image = $product['Image'];
        $ImagePath = $product['ImagePath'];
        // your code
    }
    

    当然,您可以使用$_POST['action'] 简单地访问$_POST 数组的action 键,在这种情况下会给出SaveImages。这是更完整的示例:

    if(isset($_POST['action']) && $_POST['action'] == "SaveImages"){
        foreach($_POST['images'] as $product){
            $ProductID = $product['ProductID'];
            $Image = $product['Image'];
            $ImagePath = $product['ImagePath'];
            // your code
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 2017-03-20
      • 2019-09-15
      • 2012-03-08
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      相关资源
      最近更新 更多