没有为每个会话值调用函数

Function is not being called for every session value

本文关键字:会话 值调用 函数      更新时间:2023-09-26

我正在开发一个游戏门户,教授应该可以在其中添加任何类型的问题。我已经在form.php中创建了问题类型(多选或描述性)函数,并将其包含在我的主文件中。首先,在循环中,我调用主框(简单的html框),在其中我必须添加问题。然后我给出了一个选项来选择要添加问题的问题类型。在选择问题类型时,页面加载并提交表单,我将获得该值并保存在动态创建的会话变量中。一切都很好,但主要问题是,当我选择问题类型时,它会为特定的框选择该类型,但所有其他问题都会消失。可能是它在会话变量中的覆盖值。请引导我。

            <!-- 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-->

这是我的表单.php文件

    <?
    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 ="answers" name="answers" class="m-wrap small" />
                                   </div>
                                </div>
            <?
          }
        ?>

问题是,我必须动态更改所有内容的id,因为我不知道问题的确切数量,用户应该能够添加他想要的问题。

当您调用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="answerswer_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文件

<?
$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 ="answers" name="answers" class="m-wrap small" />
                                   </div>
                                </div>
            <?
          }
        ?>