0

I am trying to store a multidimensional array of a Quiz where I dynamically add question box. But I am not able to figure out how to store dynamically added question box. The code is below

function wporg_add_custom_box()
{
$screens = ['sr-quiz'];
foreach ($screens as $screen) {
    add_meta_box(
        'quiz_box_id',
        'Quiz Box', 
        'wporg_custom_box_html',  
        $screen                   
    );
   }
 }
 add_action('add_meta_boxes', 'wporg_add_custom_box');

 function wporg_custom_box_html($post)
 {
 jQuery(document).ready(function(e) {
    function addQuestion(){
        var question = jQuery('#question-template').clone();
        question.css("display","block").removeAttr('id');
        jQuery('#questions').append(question);
    }

    function renameQuestions(){
        jQuery('.question-box').each(function(i,v){
            jQuery(this).find('.question_id').html(i);
        });
    }

    jQuery('#add-question').on('click', function(e) {
        e.preventDefault();
        addQuestion();
        renameQuestions();
    });

    jQuery(document).on('click','.del-question', function(e)
    {
        e.preventDefault();
        jQuery(this).closest('.question-box').remove();
        renameQuestions();
    });
});

<div id="questions">
<div class="question-box" id="question-template" style="display: none;">
    <h2>Question <span class="question_id"></span></h2>

    <input type="text" name="quiz[english][ques_title]" class="ques_title" placeholder="Enter question title" value="">

    <textarea name="quiz[english][ques_desc]" class="ques_desc" rows="4" placeholder="Explaination here...."></textarea>

    <a class="del-question button" href="#" data-id="1">Remove</a>
</div>
<a id="add-question" class="button" href="#">Add</a>
</div>
<?php
}
function wporg_save_postdata($post_id)
{
   $arr = $_POST['quiz'];
   update_post_meta(
        $post_id,
       'quiz_data',
       $arr
   );
}
add_action('save_post', 'wporg_save_postdata');

I am not able to figure out the solution, I even tried quiz[][quiz][english][ques_title]. But this did not work

3
  • I don't see any php here. Did you intend to do something like <?php echo quiz['english']['ques_title'] ?> ? Commented Aug 22, 2018 at 18:09
  • @ChrisStage, sorry for that now I have added the PHP code
    – topper1309
    Commented Aug 22, 2018 at 18:50
  • I want to have different arrays like, quiz[0][quiz][english][quiz_title].....quiz[1][quiz][english][quiz_title]....so on. But I am confused on how to achieve that.
    – topper1309
    Commented Aug 22, 2018 at 18:51

1 Answer 1

1

I think it has nothing to do with wordpress. Shouldn't this question be on stackoverflow?

HTML:

name="quest_title[]"
name="quest_desc[]"

PHP:

foreach($_POST['quest_title'] as $key => $title)
{
    $desc = $_POST['quest_desc'][$key];
    //Here do whathever you want with this $title and $desc.
}

Solution: use empty brackets at the END of the name tag.

2
  • I have added some wordpress function which I forgot to add. Now could you help ?
    – topper1309
    Commented Aug 22, 2018 at 18:57
  • You want "quiz[0][quiz][english][quiz_title]" but you have to put brackets at the end, so HTML would identify it as an array. Then, iterate as I said.
    – SomeRSGuy
    Commented Aug 22, 2018 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.