【问题标题】:PHP for each loop in $_POST$_POST 中每个循环的 PHP
【发布时间】:2021-05-13 11:08:12
【问题描述】:

我正在创建一个可共享的表单并使用 JSON 作为结构。当有人要提交表单上的数据时,我只是卡住了。

{
"table-name": "Locations-Grid view(1)",
"created-on": "May 13, 2021",
"token": "nKdcXx2d0bbLifg",
"columns": "Locations,Lat,Lng",
"data": [
    {
        "Locations": "Mumbai 1",
        "Lat": "19.088008",
        "Lng": "72.882959"
    },
    {
        "Locations": "Mumbai 2",
        "Lat": "19.088008",
        "Lng": "72.882959"
    },
    {
        "Locations": "Mumbai 3",
        "Lat": "19.088008",
        "Lng": "72.882959"
    },
    {
        "Locations": "Mumbai 4",
        "Lat": "19.088008",
        "Lng": "72.882959"
    },
    {
        "Locations": "Mumbai 5",
        "Lat": "19.088008",
        "Lng": "72.882959"
    },
    {
        "Locations": "Mumbai 6",
        "Lat": "19.088008",
        "Lng": "72.882959"
    },
    {
        "Lng": "45254"
    },
    {
        "Lng": "455"
    }
]

}

如您所见,最后 2 个项目没有存储“Locations”和“Lat”,仍然只有“Lng”。在这里,我使用 PHP 对输入元素执行的操作,在检查每个输入名称后,在 html 上成功打印,如您所见,我使用元素名称,与我的 JSON 中的列完全相同

$jsn = file_get_contents('tables/test.json');
$arr = json_decode($jsn, true);
$data = $arr["columns"];
$pars = explode(',',$data);

//create DOM inputs according to each columns in my array

foreach ($pars as $value) {
    $DOM .= '<div class="relative md:w-full lg:w-full mr-30 popup">
                <label for="hero-field" class="leading-7 text-sm text-gray-600"> <span>'.$value.'</span</label>
                <input type="text" name="'.$value.'" class="w-full bg-gray-100 rounded border bg-opacity-50 border-gray-300 focus:ring-2 focus:ring-indigo-200 focus:bg-transparent focus:border-indigo-500 text-base outline-none text-gray-700 py-1 px-3 leading-8 transition-colors duration-200 ease-in-out" value="" required>
            </div>';
}

这是它在我的 html 中的外观。和工作

<form class="w-full" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"style="margin-left:40px;">
    <?php echo $DOM; ?>
    <button type="submit" class="inline-flex text-white bg-indigo-500 border-0 py-2 px-6 focus:outline-none hover:bg-indigo-600 rounded text-lg">Submit form</button>
</form>

问题是提交时使用 POST 方法,它保存在 JSON 上,但只保存“Lng”。这是我所做的

$data1 = $arr["data"];
$data = $arr["columns"];
$pars = explode(',',$data);
foreach ($pars as $value) {
    $temp = array( $value => $_POST[$value],);
}  

//sending it
array_push($data1,$temp);
$arr["data"] =$data1
if(file_put_contents('tables/test.json',json_encode($arr,JSON_PRETTY_PRINT))){
    echo 'submitted';
        
}else{
    echo 'failed';
}

【问题讨论】:

    标签: php json foreach


    【解决方案1】:

    此循环每次循环都会覆盖$temp,因此您只能从数组中获取最后一次出现

    foreach ($pars as $value) {
        $temp = array( $value => $_POST[$value],);
    } 
    

    改为

    foreach ($pars as $value) {
        $temp[$value] = $_POST[$value];
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多