不为每个会话值调用函数

时间:2015-11-24 22:17:21

标签: javascript php jquery html json

我正在开发一个游戏门户网站,其中教授应该能够在其中添加任何类型的问题。我在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-->

这是我的form.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 ="ans" name="ans" class="m-wrap small" />
                                   </div>
                                </div>
            <?
          }
        ?>

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

1 个答案:

答案 0 :(得分: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文件

<?
$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>
            <?
          }
        ?>