Populate input with array

Asked

Viewed 317 times

1

Hello,

In my code jquery 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 as new images are selected and inserts name="my_image_URL[]" in 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).'" />';

        }

Complete 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"/>';

        }   
    }

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 it’s all right, but I can’t find the error.

After Submit the input remains empty, print_r and var_dump return empty

I appreciate help

UPDATING

I tried to:

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

But not yet filling in the input.

I’ve only tried:

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

}

But after Submit nothing appears on the screen, the form correctly saves all other inputs except what I am trying to save the array, if I put some manual information will ok. But I don’t understand why you’re not capturing the Names my_image_URL[] of each image input. The action in the form is like this:

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

I’m using the Settings API

I appreciate help

1 answer

1

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

echo '<input name="imagens_home" value="'.$json_encode($urls).'" style="width:300px"/>';
  • Hi Sérginho, I tried but continued not filling in the input, updated my question I added more information, about what I tried

  • Replace it with this line and see if it works, if I don’t later mount a test on my machine to try to help you: echo '<input name="imagens_home" value="'.implode(" , ", $urls).'" />';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.