如何在我的插件设置页面中在wordpress中创建可重复的字段

时间:2018-02-21 07:10:41

标签: javascript php wordpress

这是我的HTML代码,我在插件设置页面中创建了一个简单的HTML表单:

<?php
/**
 * Adds a submenu page under a custom post type parent.
 */

function books_register_ref_page()
    {
    add_submenu_page('edit.php?post_type=fscf_contact', __('Setting', 'fscf') , __('Settings', 'fscf') , 'manage_options', 'setting-fscf-contacts', 'fscf_contact_page_callback');

    // call register settings function

    add_action('admin_init', 'register_my_fscf_contact_settings');
    }

function register_my_fscf_contact_settings()
    {

    // register our settings

    register_setting('fscf-plugin-settings-group', 'fscf_field_1');
    }

/**
 * Display callback for the submenu page.
 */

function fscf_contact_page_callback()
    {
?>
<div class="wrap">
<h1><?php
    _e('Contact Form Setting', 'textdomain'); ?></h1> <hr>


<div class="wrap">


<form method="post" action="" id="myform">
<?php
    settings_fields('fscf-plugin-settings-group'); ?>
<?php
    do_settings_sections('fscf-plugin-settings-group'); ?>
<table class="form-table" id="p_scents">

<tr valign="top">
<th scope="row">New Option Name</th>
<td> <p>
<label for="p_scnts">

<input type="text" name="fscf_field_1" id="fscf_field_1"  value="<?php
    echo esc_attr(get_option('fscf_field_1')); ?>" /> <input type="button" id="addScnt" name="" value="+"> 
</label>
</p> </td>

</tr>

</table>

<?php
    $other_attributes = array(
        'id' => 'submit_repeatable_fields'
    );
    submit_button("Submit", "submit", "submit", false, $other_attributes); ?>

<span id="fscf_error"></span>
</form>
</div>



</div>
<?php
    }

add_action('init', 'books_register_ref_page');

这是我的.js代码,我在HTML格式中重复<tr>

jQuery(document).ready(function() {
var scntDiv = jQuery('#p_scents');
var i = jQuery('#p_scents p').size() + 1;

jQuery('#addScnt').live('click', function() {
jQuery('<tr valign="top"><th scope="row">New Option Name</th><td> <p><label for="p_scnts"><input type="text" name="fscf_field_' + i +'" id="fscf_field_' + i +'"  value="" /> <a href="#" id="remScnt">Remove</a></p> </td></tr>').appendTo(scntDiv);
i++;
return false;
});

jQuery('#remScnt').live('click', function() { 
if( i > 2 ) {
jQuery(this).parents('#p_scents tr').remove();
i--;
}
return false;
});


// Making Ajax Request for settings


jQuery("#myform").submit(function(e) {
e.preventDefault();


var formdata = jQuery("#myform input[type='text']").serializeArray();
var numberflds = jQuery("#myform input[type='text']").size();


var data = {

'action' : 'fscf_create_repeatable_fields',
'fullform':jQuery("#myform input[type='text']").serializeArray(),
'fields': numberflds,



//nonce : security 

};

// We can also pass the url value separately from ajaxurl for front end AJAX implementations

jQuery.post(ajax_obj.ajax_url, data, function(response) {


console.log(response);
jQuery("#fscf_error").html(response);

});






});




});

以下是我的php代码,我希望保存我在.js文件中重复但我无法保存数据的字段:

function fscf_create_repeatable_fields(){

$mydata = $_POST['fullform'];
$fields = $_POST['fields'];
//$i=0;
foreach ($_POST['fullform'] as $form) {
register_setting( 'fscf-plugin-settings-group', $form['name']); 
//$i++;
}

echo "Field Created.";

wp_die();

}

add_action( 'wp_ajax_nopriv_fscf_create_repeatable_fields', 'fscf_create_repeatable_fields' );
add_action( 'wp_ajax_fscf_create_repeatable_fields', 'fscf_create_repeatable_fields' );

所以,问题是我无法在插件页面中保存可重复的字段。我在做什么错误?

0 个答案:

没有答案