Fill input with array

1

Hello,

In my jquery code it has a part like this:

$("#show").append("<img src=" +attachment.url+" alt="+attachment.alt+" title="+attachment.title+" description="+attachment.caption+" class='img-responsive img-thumbnail'/><input type='hidden' name='my_image_URL[]' value="+attachment.url+"></span>");

This part adds new inputs to new selected images and inserts name="my_image_URL[]" into each of the inputs

With PHP I'm trying to create the array with the names my_image_URL[] and with JSON encode save in single input:

if ( isset( $_POST['my_image_URL'] ) ) {

        $urls = $_POST['my_image_URL'];
             echo '<input name="imagens_home" value="'.$json_encode($urls).'" />';

        }

Full Code

options.php

 register_setting(
            'tema-setting-group',//string $option_group
            'imagens_home' //string $option_name
            //calback $sanitize_calback      
        );

---
    add_settings_field(
         'home-imagens-top',//string $id
         'Imagens',//String $title
         'tema_home_imgs',//string $calback
         'opcoes_do_tema',//string $page    
         'tema-home-options'//string $section
         //string $args          
         );

//calback
function tema_home_imgs(){   
        $urlsImagens = esc_attr( get_option( 'imagens_home' ) ); // RETURN DB DATA

        include( get_template_directory() . '/inc/templates/selecao_imagens.php');

        if ( isset( $_POST['my_image_URL'] ) ) {

        $urls = $_POST['my_image_URL'];
             echo '<input name="imagens_home" value="'.$json_encode($urls).'" style="width:300px"/>';

        }   
    }

image_pictures.php

<input id="my_upl_button" type="button" value="Escolher Imagens" /><br/>

    <div class="row">
            <div id="exibe" class="sortable">       

            <?php           
            $urls = json_decode($urlsImagens, true);
                if ($urls != '' ) {
                foreach ($urls as $url) { 
             ?>          
                    <img src="<?php echo $url;?>"  class="img-responsive img-thumbnail " />
                    <input name="my_image_URL[]" value="<?php echo $url;?>"/>

            <?php
                 };
                }
            ?>
    </div>
    </div>

theme_options.php

<?php settings_errors();?>

<form method=”post” action=”options.php”>
<?php settings_fields (‘tema-setting-group’); ?>

<?php do_settings_sections (
‘opcoes_do_tema’//string $page

); ?>

<?php submit_button ();

?>

</form>

Apparently this is all ok, but I can not find the error.

After submitting the input is still empty, print_r and var_dump return empty

Thank you for your help

UPDATE

I've tried:

echo '<input name="imagens_home" value="' . htmlspecialchars(json_encode($urls)) . '" />';

But you are not yet filling out the input.

I tried only:

If (isset ($ _POST ['my_image_URL'])) {
 Print_r ($ _ POST ['my_image_URL']);

}

But after submit nothing appears on the screen, in the form correctly saves all other inputs except what I'm trying to save the array, if I put some manual information goes ok. But I do not understand why it is not capturing the names my_image_URL[] of each image input. Action in form looks like this:

<Form method = “post” action = “options.php”>

I'm using the Settings API

Thank you for your help

    
asked by anonymous 25.08.2017 / 03:20

1 answer

1

Try to get the $ front of the json_encode ($ urls) function on the line:

echo '<input name="imagens_home" value="'.$json_encode($urls).'" style="width:300px"/>';
    
25.08.2017 / 05:56