【问题标题】:Data from form to jquery/php array从表单到 jquery/php 数组的数据
【发布时间】:2014-05-29 07:12:56
【问题描述】:

我想问,如何将表单中的数据移动到 php 或 jquery 中的数组中,以便在测验结束时作为总结,查看哪些问题是正确的,哪些是错误的。 我还想从数据库中随机抽取 30 个问题。 请帮忙。

index.php

<?php require_once 'config.php';?>
<!DOCTYPE html>
<html>
<head>
<title>Testy Kwalifikacja Wstępna kat. C, CE, D, DE OCK OLSZTYN</title>
<meta charset='utf-8'>
<link rel='stylesheet' href='css/style.css'/>
</head>

<body>
        <div id="top-logo">
        </div>
        <div id="wrapper">
            <div id="content">
<?php $response=mysql_query("select * from pytania WHERE id='2' OR id='3' OR id='1'");?>

<form method='post' id='quiz_form'>
<?php while($result=mysql_fetch_array($response)){ ?>



<div id="question_<?php echo $result['id'];?>" class='questions'>
<div class="images">
<img src="img/<? echo $result['obrazek']?>">
</div>
<div class="questions-2">
<h2 id="question_<?php echo $result['id'];?>"><?php echo $result['id'].".".$result['pytanie'];?></h2>
</div>
<div class='align'>
<input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'>
<label id='ans1_<?php echo $result['id'];?>' for='1'><?php echo $result['odp_a'];?></label>
<br/>
<input type="radio" value="2" id='radio2_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'>
<label id='ans2_<?php echo $result['id'];?>' for='1'><?php echo $result['odp_b'];?></label>
<br/>
<input type="radio" value="3" id='radio3_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'>
<label id='ans3_<?php echo $result['id'];?>' for='1'><?php echo $result['odp_c'];?></label>
<br/>

<input type="radio" checked='checked' value="5" style='display:none' id='radio4_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'>
</div>
<br/>
<input type="button" id='next<?php echo $result['id'];?>' value='Next!' name='question' class='butt'/>
</div>
<?php }?>
</form>
<div id='result'>

<br/>
</div>

<div id="demo1" class="demo" style="text-align:center;font-size: 25px;">00:00:00</div>
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/watch.js"></script>
<script>
$(document).ready(function(){
    $('#demo1').stopwatch().stopwatch('start');
    var steps = $('form').find(".questions");
    var count = steps.size();
    steps.each(function(i){
        hider=i+2;
        if (i == 0) {   
            $("#question_" + hider).hide();
            createNextButton(i);
        }
        else if(count==i+1){
            var step=i + 1;
            //$("#next"+step).attr('type','submit');
            $("#next"+step).on('click',function(){

               submit();

            });
        }
        else{
            $("#question_" + hider).hide();
            createNextButton(i);
        }

    });
    function submit(){
         $.ajax({
                        type: "POST",
                        url: "ajax.php",
                        data: $('form').serialize(),
                        success: function(msg) {
                          $("#quiz_form,#demo1").addClass("hide");
                          $('#result').show();
                          $('#result').append(msg);
                        }
         });

    }
    function createNextButton(i){
        var step = i + 1;
        var step1 = i + 2;
        $('#next'+step).on('click',function(){
            $("#question_" + step).hide();
            $("#question_" + step1).show();
        });
    }
    setTimeout(function() {
          submit();
    }, 50000);
});
</script>
</div>
</div>
</body>
</html>

【问题讨论】:

  • 如果我正确理解了您的问题,只需点击此链接即可解决您的问题。api.jquery.com/serializearray
  • 我很确定您没有编写该代码。这并没有错,但公然寻求解决方案并不是要走的路。如果您尝试破译代码的作用,您会做得更好
  • 请注意 mysql 扩展(提供 mysql_ 函数)自 2012 年以来已被弃用,取而代之的是 mysqli 和 PDO 扩展。强烈建议不要使用它。见stackoverflow.com/questions/12859942/…

标签: javascript php jquery arrays forms


【解决方案1】:

如果您想将表单中的值存储在 php 变量中,您可以这样做:

if(isset($_POST['something']))
{
    $answer = $_POST['something'];
}

其中 something 是表单中输入字段的名称。此代码需要在您提交表单后重定向到的 php 页面中。在您上面提到的代码中,它会重定向到同一页面,因为表单没有 action 属性。对不起,如果这个答案看起来很简单,但这就是你需要做的。

编辑: 要从您的数据库中获得 30 个随机问题而无需重复,我想您可以试试这个:

$used = array();

$i=1;

while($i<=30)
{
    $num = rand(0,99); //assuming 100 is the maximum number of questions in your db

    bool already_used=false;        

    for($j=0;$j<sizeof($used);$j++)
    {
        if($num==$used[j])
        {
             already_used=true;
             break;
        }
    }

    if(!$already_used)
    {
        //get the question numbered '$num' from your db
        array_push($used,$num);
        i++;
    }
}

这基本上是用于生成 30 个唯一随机数的代码。如果您想使用这些随机数从您的数据库中获取问题,您可以使用mysql_result()like:

$query="SELECT questions FROM quiz";
$query_run = mysql_query($query);
$question = mysql_result($query_run,24,'questions');

这是为了获取第 24 行中的问题,假设所有问题都作为字符串存储在名为 quiz 的表中名为 questions 的列中。

【讨论】:

  • 谢谢,您知道我是如何从数据库中获得 30 个随机问题而不会重复的吗?在数据库上我有大约一千个问题......你的解决方案很有帮助,让我走上了正确的道路,我做了一些不同的事情并且工作正常。
  • @Kubol 查看编辑。这是你想要的吗?
猜你喜欢
  • 2012-04-15
  • 2012-11-22
  • 2014-03-31
  • 2023-03-24
  • 2013-02-05
  • 1970-01-01
  • 2012-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多