我想做一个测试插件。使用自定义的post类型,我制作了一个元框,其中包含以下表单。
<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>
输入保存为post元数据的数组。我可以在按下任何按钮时成功保存文本输入,并且在加载编辑器时调用这些值。我遇到的问题是,当按下相应的按钮时,添加一个没有javascript的新输入字段。
我原以为,使用上述标记,当我单击“添加答案”按钮时,会设置$\\u POST[\'add\\u new\\u answer],但事实并非如此。
使用PHP单击该按钮时,如何获取其值?
以下是我目前正在尝试的:
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 );
}