0

I wanted to ask, how can I move the data from my form into an array in php or jquery, so I could at the end of the quiz, as a summary, see which questions were correct and which incorrect. I would also like 30 questions taken at random from the database. Please help.

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>
Kubol
  • 136
  • 10
  • If i had understood your question correctly, just follow this link which will solve your problem.. http://api.jquery.com/serializearray/ – Mr.Chowdary May 29 '14 at 07:18
  • I'm pretty sure you didn't write that code. There's nothing wrong in that, but blatantly asking for solutions isn't the way to go. You'd do yourself more good if you attempt to *decipher* what the code does – asprin May 29 '14 at 07:24
  • Please be aware that the mysql extension (supplying the mysql_ functions) has been deprecated since 2012, in favor of the mysqli and PDO extensions. It's use is highly discouraged. See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Oldskool May 29 '14 at 12:20

1 Answers1

0

If you want to store values from the form in php variables you can just do this:

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

where something is the name of the input field in the form. This code needs to be in the php page that you are redirected to after you submit the form. In the code you mentioned above, it'd be redirecting to the same page because there is no action attribute to the form. Sorry if this answer seems to simple but, that's all you need to do.

EDIT: To get 30 random question from your db without repition i guess you can try this:

$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++;
    }
}

This is basically the code you'd use for generating 30 unique random numbers. If you want to use these random numbers to get the questions from your db you can use mysql_result()like:

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

This is to get the question in the 24th row assuming all the questions are stored as strings in a column called questions in a table called quiz.

pale_rider
  • 309
  • 1
  • 3
  • 14
  • Thanks, and may you know how I get 30 random questions from the database without the possibility of repetition? On database I have about a thousand questions ... Your solution is helpful, led me to the right path, I did a little differently and working fine. – Kubol May 29 '14 at 10:43
  • @Kubol check out the EDIT. Is this what you wanted? – pale_rider May 29 '14 at 11:11