• 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

    (@diondesigns)

    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

    Thread Starter Gisele

    (@gislef)

    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.
    Dion

    (@diondesigns)

    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

    (@gislef)

    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

    (@gislef)

    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.