I'm trying to make a quiz plugin. Using a custom post type, I've made a meta box, that has the following form table in it.
<table class="form-table">
<tbody>
<tr>
<th>
<label for="quiz_type">Quiz Type</label>
</th>
<td>
<select name="quiz_data[quiz_type]" id="quiz_type">
<option value="trivia">Trivia</option>
<option selected="selected" value="personality">Personality</option>
</select>
<br>
<span class="description">Choose the type of quiz.</span>
</td>
</tr>
<tr>
<th>
<label for="quiz_data[questions][1][question]">Question</label>
</th>
<td>
<input type="text" name="quiz_data[questions][1][question]" id="quiz_data[questions][1][question]" value="fffffff"
size="75">
<br>
<span class="description">What is the question.</span>
</td>
</tr>
<tr>
<th>
<label for="quiz_data[questions][1][answers][1][answer]">Answer</label>
</th>
<td>
<input type="text" name="quiz_data[questions][1][answers][1][answer]" id="quiz_data[questions][1][answers][1][answer]"
value="dfdfds" size="30">
</td>
<th>
<label for="quiz_data[questions][1][answers][1][is_correct]">Is Correct?</label>
</th>
<td>
<input type="checkbox" name="quiz_data[questions][1][answers][1][is_correct]" id="quiz_data[questions][1][answers][1][is_correct]">
</td>
</tr>
<tr>
<th></th>
<td>
<button class="add-answer button-secondary" id="add_new_answer" name="add_new_answer" type="submit" formmethod="post"
value="quiz_data[questions][1][answers][2][answer]">Add Answer</button>
</td>
</tr>
<tr>
<th></th>
<td>
<button class="add-question button-primary" name="add_new_question" type="submit" value="">Add Question</button>
</td>
</tr>
</tbody>
</table>
The inputs are saved as an array to the post meta data. I can successfully save the text inputs when any button is pressed, and the values are recalled when I load the editor. What I am have having trouble with is adding a new input field, without javascript, when the appropriate buttons are pressed.
I thought that, with the above markup, $_POST['add_new_answer] would be set when I click the "Add Answer" button, but it is not.
How can get the value of that button when it is clicked using PHP?
Here's what I'm trying at the moment:
if ( isset( $_POST['quiz_data'] ) || isset( $_POST['add_new_answer'] ) || isset($_POST['add_new_question'] ) ) {
if ( isset( $_POST['quiz_data'] ) ){
$updated_quiz_data = $_POST['quiz_data'];
}
if ( isset( $_POST['add_new_answer'] ) ){
$updated_quiz_data = array_merge( $updated_quiz_data, $_POST['add_new_answer'] );
}
if ( isset( $_POST['add-new-question'] ) ){
$updated_quiz_data = array_merge( $updated_quiz_data, $_POST['add-new-answer'] );
}
// Update the post meta data with the updated quiz
update_post_meta( $quiz_id, 'quiz_data', $updated_quiz_data );
}