Title: Fill input with array
Last modified: August 24, 2017

---

# Fill input with array

 *  [Gisele](https://wordpress.org/support/users/gislef/)
 * (@gislef)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/fill-input-with-array/)
 * In my jquery code I have:
 * `$("#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>");`
 * My jquery code adds fields for news selected images and fills inputs names `my_image_URL[]`
 * But this part of code PHP does not fill the 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"/>';
   
               }   
           }
       ```
   
 * selecao_imagens.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 do not know where the error is.
 * After the submit the field becomes empty, `print_r` and `var_drump` return empty
 * Thanks

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/fill-input-with-array/#post-9439863)
 * This line:
 * `echo '<input name="imagens_home" value="'.$json_encode($urls).'" />';`
 * has two problems. First, `$json_encode` should be `json_encode`. And second, 
   a JSON-encoded string contains double-quotes which will break your HTML. The 
   line should look something like this:
 *     ```
       echo '<input name="imagens_home" value="' . htmlspecialchars(json_encode($urls)) . '" />';
       ```
   
 * You should also review the _options_ parameter for json_encode():
 * [https://secure.php.net/manual/en/function.json-encode.php](https://secure.php.net/manual/en/function.json-encode.php)
 *  Thread Starter [Gisele](https://wordpress.org/support/users/gislef/)
 * (@gislef)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/fill-input-with-array/#post-9441123)
 * Thanks DionDesigns
 * I tried according to your suggestion, but it is not yet filling in the input.
 * I also tried:
 *     ```
       If (isset ($ _POST ['my_image_URL'])) {
        Print_r ($ _ POST ['my_image_URL']);
   
       }
       ```
   
 * But after the submit does not appear anything on the screen, in the form correctly
   saves all other inputs except what I am trying to save the array, if I put some
   manual information goes ok. But I do not understand why it is not capturing the`
   my_image_URL []` names of each image input. The action in form is like this:
 * `<Form method = “post” action = “options.php”>`
 * I’m using the Settings API
    -  This reply was modified 8 years, 9 months ago by [Gisele](https://wordpress.org/support/users/gislef/).
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/fill-input-with-array/#post-9441397)
 * I see the following line in your second block of code:
 * `$urls = json_decode($urlsImagens, true);`
 * Where is the `$urlsImagens` variable being defined?
 * In any case, I’d suggest that you check your PHP error log to see if you have
   any other issues.
 *  Thread Starter [Gisele](https://wordpress.org/support/users/gislef/)
 * (@gislef)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/fill-input-with-array/#post-9441553)
 * Yes the variable exists it is defined.
 * `Isset` works when I created a metabox with the following code in action:
 *     ```
       add_action( 'save_post', function ( $post_id ) {
       	if ( isset( $_POST['my_image_URL'] ) ) { 
       	    delete_post_meta( $post_id, 'my-image-for-post' ); 
       		$urls = $_POST['my_image_URL'];		
       		add_post_meta( $post_id, 'my-image-for-post', $urls );	
       	}
       });
       ```
   
 * But now I’m trying to use the options in my theme, for the home page, using settings
   API:
 *     ```
       //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="' . htmlspecialchars(json_encode($urls)) . '" />';
               }   
           }
       ```
   
 * If I put `print_r ($urls);` Returns empty
 *  Thread Starter [Gisele](https://wordpress.org/support/users/gislef/)
 * (@gislef)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/fill-input-with-array/#post-9441569)
 * Yes exists it is defined.
 * when I created a metabox with the following code in action, `Isset` works :
 *     ```
       add_action( 'save_post', function ( $post_id ) {
       	if ( isset( $_POST['my_image_URL'] ) ) { 
       	    delete_post_meta( $post_id, 'my-image-for-post' ); 
       		$urls = $_POST['my_image_URL'];		
       		add_post_meta( $post_id, 'my-image-for-post', $urls );	
       	}
       });
       ```
   
 * For the home page, options of my theme I´m using settings API:
 *     ```
       //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="' . htmlspecialchars(json_encode($urls)) . '" />';
               }   
           }
       ```
   
 * `print_r ($urls);` or `var_dump($urls);` Returns empty

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Fill input with array’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 2 participants
 * Last reply from: [Gisele](https://wordpress.org/support/users/gislef/)
 * Last activity: [8 years, 9 months ago](https://wordpress.org/support/topic/fill-input-with-array/#post-9441569)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
