Create settings field on button click (settings api)

currently I facing the problem how to add an option field or setting dynamical with a button click.
There are a few similar questions in other boards but no one helped me respectively many where not answered.

So I hope that I can give you all the informations you need to give me a hint what I can do.

My idea was to create a section like here:

function initialise_content_options(){

add_settings_section(
‘content_design_section’,
‘Content design options’,
‘content_design_callback’,
‘theoretisch_staticPage_options&tab=content_options’
);

if (!isset($_POST[“add_or_delete”])) {
$numberOfSections = 1;
}else{
$numberOfSections = $_POST[“add_or_delete”];
}

for ($i = 0; $i < $numberOfSections; $i++) {
// here I create a section with many fields

with a loop which creates the other sections dynamical.

In the section above (content_design_section) I print a description and an add button. So if the user clicks on that button I increase the value of a hidden field (with the name add_or_delete) with javascript. Then I read the value of the hidden field with $_POST["add_or_delete"] and create the number of sections with its fields in the loop. This is inside the loop:

$option_name = ‘content_options_group’.$numberOfSections;

// fetch existing options
$option_values = get_option($option_name);

// is called to automate saving the values of the fields
register_setting(
‘content_section’,
$option_name
);

$default_values = array(
‘topic’ => ”,
‘description’ => ”,
‘picUpload’ => ”
);

// parse option value into predefined keys
$data = shortcode_atts($default_values,$option_values);

add_settings_section(
‘section_design_section_’ . $numberOfSections,
‘Section ‘ . $numberOfSections,
‘section_design_callback’,
‘theoretisch_staticPage_options&tab=content_options’
);

add_settings_field(
‘topic’,
‘Topic’,
‘topic_callback’,
‘theoretisch_staticPage_options&tab=content_options’,
‘section_design_section_’ . $numberOfSections,
array(
‘name’	=> ‘topic’,
‘value’	=> esc_attr($data[‘topic’]),
‘option_name’ => $option_name
)
); // and some more fields

So this is my approach, but it doesn’t work. I think it is because the php is not executed again when I press the add Button.
So do I only have to reload my site, or is it possible without reload?

Each section has a delete link too. If that link is clicked the section of this link should be deleted. This currently doesn’t work either, but I think the problem is the same like with the add button.

If you need more informations please write it in the comments I will add it then.
Thank you!

So I conclude from that, its just not possible, is it ?

What “that” are you referring to?

If someone answered you in a PM you should answer them in that message or better yet, ask them to post in this topic publicly rather that privately.

No, the “that” refers to that I get nowhere an answer to my question.
I asked in different boards and chats now, but every time no one can give me an answer.

OK, thanks, a “bump” (tsk, tsk, finger wag)

I’m guessing it isn’t because no one can, but that the amount of code is too much to digest.

Problems are best served in byte sized portions. (pun intended)

I guess the place to start is to look at the “button” HTML (the <form>...</form> assuming that’s what it’s in) and any JavaScript code involved with processing it before it’s sent off to the server.

I have two buttons. The first one (submit_button() = Save changes) is in the form. And the second one (<input type="submit" name="Add" onclick="add_delete_button_clicked(1)" class="button-secondary" value="Add Section" /> = add new section) is in the same form too.

You didn’t post the <form> HTML.
That means the form doesn’t submit (i.e. no action or method attributes) and the sumission is done entirely and exclusively by JavaScript?

I don’t see how only the add_delete_button_clicked(1) would do all of that.

Is there a framework or something you’re using that does the “magic”?
It doesn’t look like any HTML or JavaScript I’ve seen before.

Hm the safe button works well.
But the second button which should add the section doesn’t, it seems it only reloads the page.
The js function only adds a new value to the hidden field. Which then should be read by the php.

function add_delete_button_clicked (button) {
	var numberOfSections = document.getElementById("add_or_delete").value;
	document.getElementById("add_or_delete").value = numberOfSections + button;
}

No I don’t use any frameworks =D
So the reason is that my add submit button doesn’t run the php again ?
Or what can I do that not only the page is reloaded.
Is the php not executed when the page reloads ?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.