• cosimo

    (@cosimoamicagmailcom)


    Hi to all. I’m tryin to create a metabox with undefined number of fields. The fields can be added by the users by their necessity if necessary. My problem is how to save the post data passed in this way.

    This is the code i used to create the fileds in the metabox

    echo'<textarea name="'.$meta_box3['name'].'_prezzo_servizio[]" value="'.$meta_box_value3.'" cols="30" rows="5">'.$meta_box_value3['prezzo_servizio'].'</textarea>';
    
    echo'<input type="text" name="'.$meta_box3['name'].'_descrizione_servizio[]" value="'.$meta_box_value3['descrizione_servizio'].'" size="30" /><br />';
    
    echo'<input type="text" name="'.$meta_box3['name'].'_nome_servizio[]" size="30" /><br />';

    And this is how i try to save the post data:

    $data = array();
                foreach ($_POST[$meta_box3['name'] . '_nome_servizio'] as $k => $v) {
                    $data[] = array(
                        'nome_servizio' => $v,
                        'descrizione_servizio' => $_POST[$meta_box3['name'] . '_descrizione_servizio'][$k],
                        'prezzo_servizio' => $_POST[$meta_box3['name'] . '_prezzo_servizio'][$k]
                    );
                }
    		/*$data = $_POST[$meta_box3['name'].'_value'];*/
    
    		if(get_post_meta($post_id, $meta_box3['name'].'_value') == "")
    			add_post_meta($post_id, $meta_box3['name'].'_value', $data, true);
    		elseif($data != get_post_meta($post_id, $meta_box3['name'].'_value', true))
    			update_post_meta($post_id, $meta_box3['name'].'_value', $data);
    		elseif($data == "")
    			delete_post_meta($post_id, $meta_box3['name'].'_value', get_post_meta($post_id, $meta_box3['name'].'_value', true));
    		}

    It don’t work.
    Anyone can help me or have any suggestion?

  • The topic ‘save data on metaboxes with undefined fields’ is closed to new replies.