草庐IT

javascript - 没有为每个 session 值调用函数

coder 2024-04-20 原文

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

            <!-- 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,因为我不知道问题的确切数量,用户应该能够添加他想要的任意数量的问题。

最佳答案

当您调用form() 函数时,您传递的是$q_no,但在声明时没有参数。先解决这个问题,然后告诉我。


您只接收当前选择的一个输出,但访问整个循环迭代。这是一个错误。

如果你想为单个问题添加多项选择/输入字段,那么你必须做两件事之一。

1) 为每个问题选择选项,并在单击任何按钮而不是选项更改时发送表单数据。

2) 使用 JavaScript 技巧解决问题。


我将在这里用 jQuery 给你一个例子。为此,您必须在 header 中导入 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-->

表单.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>
            <?
          }
        ?>

关于javascript - 没有为每个 session 值调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33904923/

有关javascript - 没有为每个 session 值调用函数的更多相关文章

  1. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  2. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  3. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  4. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  5. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  6. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  7. ruby - 调用其他方法的 TDD 方法的正确方法 - 2

    我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent

  8. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  9. 【鸿蒙应用开发系列】- 获取系统设备信息以及版本API兼容调用方式 - 2

    在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList​()Obt

  10. C51单片机——实现用独立按键控制LED亮灭(调用函数篇) - 2

    说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时

随机推荐