0
I have dynamic inputs with Names my_image[url] and my_image[caption]
<input name="my_image[url]" type="hidden" value="<?php echo $imgurl['url'];?>"/>
<input name="my_image[caption]" type="text" value="<?php echo $imgurl['caption'];?>">
Even inserted several images and captions dynamically, inside the isset o $ _POST ['my_image'] is capturing only 1 image and 1 caption:
if (isset($_POST['my_image'])){
update_option('imagens_inicio',array($_POST['my_image']);
echo '<pre>'; var_dump($_POST['my_image']);echo '</pre>';
}
array(1) {
[0]=>
array(2) {
["url"]=>
string(96) "http://localhost/theme/wp-content/uploads/2017/08/4e07dd2bd752b989e9b4687129982977.jpg"
["caption"]=>
string(0) "test text"
}
}
How do I make the array to be created in a continuous sequence:
url[]
caption[]
url[]
caption[]
I appreciate help
I believe the field name should follow the format
my_image[0][url]andmy_image[0][caption], incrementing the value of 0 for each dynamic field inserted on the page.– Woss
@Andersoncarloswoss It did not work, I asked the question in English tbm, but I did not understand the proposed solution see: link
– Gislef
In my opinion the solution presented in Soen is good, renaming the inputs to
url[]andcaption[], which creates two independent arrays with automatic sequential numbering. Naturally it will change the way you read them, as indicated in that reply– Isac
@Isac ah tah I now understood the logic of his reply, but I didn’t understand that part:
($url, $caption) { return compact('url', 'caption');the variables$urland$captionare implied that refers to the post url and post caption? 'url' and 'caption' in the single quotes tbm is implied that?– Gislef
They refer to the parameters of
array_map. It uses the functionarray_mapphp that in his example takes two arrays, the, $_POST['url'], $_POST['caption'])and interconnects them at the expense of the functioncompact. Compact takes two strings with the name of the variables to put in a new array.– Isac
Right I get it, but the
var_dump($data);is returningnull– Gislef
I finally got it!
$_POST['url'];and$_POST['caption'];because directly in thearray_mapwasn’t working. thanks a lot @Isac– Gislef