The function is not called for each session value.

I am developing a game portal in which a professor should be able to add any questions to it. I created question types (multiple choice or descriptive) in form.php and I included it in my main file. First of all, in the loop, I call the main unit (a simple html window) in which I must add a question. then I give the opportunity to choose the type of question to add a question. When choosing the type of question, the page loads and submits the form, and I get this value and save the dynamically created session variable. Everything works fine, but the main problem is that when I select the type of question, it selects it for this particular window, but all other questions disappear. Maybe its rewrite values ​​in the session variable. Please guide me.

            <!-- BEGIN BORDERED TABLE PORTLET-->

                            <?
            $q_no=5;
            for ($i=0;$i<$q_no; $i++)
            {
            ?>
           <div class="portlet box yellow">
                        <div class="portlet-title">
                            <h4><i class="icon-coffee"></i>#<?echo $i+1;?>     </h4>
                            <div class="tools">
                                <a href="javascript:;" class="collapse"></a>
                                <a href="#portlet-config" data-toggle="modal"                      class="config"></a>
                                <a href="javascript:;" class="reload"></a>
                                <a href="javascript:;" class="remove"></a>
                            </div>
                        </div>
                        <div class="portlet-body">
                            <table class="table table-bordered table-hover">
                                <thead>

                                </thead>
                                <tbody>
                <form action="newGame.php" method="POST" id="input_type" name="input_type">
               <div class="control-group">
                                   <label class="control-label"  > Add Input</label>
                                   <div class="controls">
                                      <select onchange="this.form.submit()" class="medium m-wrap" tabindex="1" id="type<?echo $i;?>" name="type<?echo $i;?>">
                                      <option value="">Input Type</option>
                                         <option value="1">Multiple Choice</option>
                                         <option value="2">Input Field</option>
                                      </select>
                                   </div>
                                </div>
        </form>

        <?

        //qType();


        $_SESSION["input_type"][$i]= $_POST["type".$i];


        if($_SESSION["input_type"][$i]==1)
        {
        form($q_no);
        }
        elseif($_SESSION["input_type"][$i]==2)
        {

        form1();
        }

        ?>



                                </tbody>
                            </table>
                        </div>
                    </div>
                    <?
                    }

                 ?>
                    <!-- END BORDERED TABLE PORTLET-->

Here is my form.php file

    <?
    function form()
    {

    ?>

    <form  name="1" id="1" action="lecturer.php" method="POST">

    <div class="control-group">

    <div class="controls">
    <textarea class="large m-wrap"   placeholder=" Statement " cols="50"rows="3" name="statement<?echo $i;?>"  style="text-align:center;" id="statement<?echo $i;?>"></textarea>
    </div>
    </div>
    <div class="name">
    <input name="option<?echo $i.'1';?>" id="option<?echo $i.'1';?>"   placeholder="Option 1" style="width:170px;" type="text"/>

     <input name="option<?echo $i.'2';?>" id="option<?echo $i.'2';?>"    type="text" style="width:170px;" placeholder="Option 2"/>

    <input name="option<?echo $i.'3';?>" id="option<?echo $i.'3';?>"  type="text" style="width:170px;" placeholder="Option 3"/>

    <input name="option<?echo $i.'4';?>" id="option<?echo $i.'4';?>" type="text" style="width:170px;" placeholder="Option 4"/>

     </div>
     <div class="control-group">

     <div class="controls">
      <label class="radio">
      <input type="radio" name="option1_default" id="option<?echo $i.'1';?>_default" />
      Option 1
      </label>
      &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
      <label class="radio">
      <input type="radio" name="option1_default" id="option<?echo $i.'2';?>_default" checked />
      Option 2
      </label>  
      &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
      <label class="radio">
      <input type="radio" name="option1_default" id="option<?echo $i.'3'?>_default" />
      Option 3
      </label> 
      &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
      <label class="radio">
      <input type="radio" name="option1_default" id="option<?echo $i.'4';?>_default" />
      Option 4
      </label>
      </div>
      </div>
      <p><input name="Submit" type="button" value="Submit" color="green" class="button" /></p>
      </form>
     <?php
     }
    //end
    ?>

    <?php
    function form1()
    { ?>

    <div class="control-group" id ="field" name="field">
    <label class="control-  label">Answer</label>
    <div class="controls">
    <input type="text" placeholder="Answer" id ="ans" name="ans" class="m-wrap small" />
                                   </div>
                                </div>
            <?
          }
        ?>

, , , , .

+4
1

form(), $q_no, . , .


, . .

/ , .

1) , .

2) JavaScript, .


jQuery. jquery

<!-- BEGIN BORDERED TABLE PORTLET-->



    <?
            $q_no=5;
            for ($i=0;$i<$q_no; $i++)
            {
            ?>
           <div class="portlet box yellow">
                        <div class="portlet-title">
                            <h4><i class="icon-coffee"></i>#<?echo $i+1;?>     </h4>
                            <div class="tools">
                                <a href="javascript:;" class="collapse"></a>
                                <a href="#portlet-config" data-toggle="modal"                      class="config"></a>
                                <a href="javascript:;" class="reload"></a>
                                <a href="javascript:;" class="remove"></a>
                            </div>
                        </div>
                        <div class="portlet-body">
                            <table class="table table-bordered table-hover">
                                <thead>

                                </thead>
                                <tbody>
                <form action="newGame.php" method="POST" id="input_type" name="input_type">
               <div class="control-group">
                                   <label class="control-label"  > Add Input</label>
                                   <div class="controls">
                                      <select onchange="this.form.submit()" class="medium m-wrap question_type" data-question-no="<?echo $i;?>" tabindex="1" id="type<?echo $i;?>" name="type<?echo $i;?>">
                                      <option value="">Input Type</option>
                                         <option value="1">Multiple Choice</option>
                                         <option value="2">Input Field</option>
                                      </select>
                                   </div>
                                   <div id="answer_no_<?php echo $i ?>"></div>
                                </div>
        </form>

        <?

        //qType();

        ?>



                                </tbody>
                            </table>
                        </div>
                    </div>

    //jQuery code with ajax in the same file

        <script>
        $(document).ready(function(){
            $('.question_type').change(function(){
                var question_no=$(this).attr('data-question-no');
                $.ajax({
                    url: "provide_url",
                    type:'post',
                    data:{
                        type:$(this).val(),
                        i:question_no
                    },
                    success:function(data){
                        $('#answer_no_'+question_no).html(data);
                    }

               });
        });
        });
        </script>
                        <?
                        }

                 ?>
                    <!-- END BORDERED TABLE PORTLET-->

form.php file

<?
$i=$_post['i'];
$_SESSION["input_type"][$i]= $_POST["type"];


        if($_SESSION["input_type"][$i]==1)
        {
        form();
        }
        elseif($_SESSION["input_type"][$i]==2)
        {

        form1();
        }


    function form()
    {

    ?>

    <form  name="1" id="1" action="lecturer.php" method="POST">

    <div class="control-group">

    <div class="controls">
    <textarea class="large m-wrap"   placeholder=" Statement " cols="50"rows="3" name="statement<?echo $i;?>"  style="text-align:center;" id="statement<?echo $i;?>"></textarea>
    </div>
    </div>
    <div class="name">
    <input name="option<?echo $i.'1';?>" id="option<?echo $i.'1';?>"   placeholder="Option 1" style="width:170px;" type="text"/>

     <input name="option<?echo $i.'2';?>" id="option<?echo $i.'2';?>"    type="text" style="width:170px;" placeholder="Option 2"/>

    <input name="option<?echo $i.'3';?>" id="option<?echo $i.'3';?>"  type="text" style="width:170px;" placeholder="Option 3"/>

    <input name="option<?echo $i.'4';?>" id="option<?echo $i.'4';?>" type="text" style="width:170px;" placeholder="Option 4"/>

     </div>
     <div class="control-group">

     <div class="controls">
      <label class="radio">
      <input type="radio" name="option1_default" id="option<?echo $i.'1';?>_default" />
      Option 1
      </label>
      &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
      <label class="radio">
      <input type="radio" name="option1_default" id="option<?echo $i.'2';?>_default" checked />
      Option 2
      </label>  
      &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
      <label class="radio">
      <input type="radio" name="option1_default" id="option<?echo $i.'3'?>_default" />
      Option 3
      </label> 
      &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
      <label class="radio">
      <input type="radio" name="option1_default" id="option<?echo $i.'4';?>_default" />
      Option 4
      </label>
      </div>
      </div>
      <p><input name="Submit" type="button" value="Submit" color="green" class="button" /></p>
      </form>
     <?php
     }
    //end
    ?>

    <?php
    function form1()
    { ?>

    <div class="control-group" id ="field" name="field">
    <label class="control-  label">Answer</label>
    <div class="controls">
    <input type="text" placeholder="Answer" id ="ans" name="ans" class="m-wrap small" />
                                   </div>
                                </div>
            <?
          }
        ?>
+1

Source: https://habr.com/ru/post/1617331/


All Articles