1

I'm doing a online exam tool. I want to minimize the number of database requests. So I am requesting all the questions in the test at one go. After this I have to remember all the questions user has attempted and their answers. My plan is to save the answers in a php session variable like this

$('input[type=radio]').click(function()
{
    var id = ($(this).parent().attr('id'));
    id = id.slice(4);
    $('#nav'+id).css('color','red');
    <?php  $_SESSION['ques['id']']= ?> $(this).val() <?php ;?>
});

In the above code the following lines are to change the color of attempted questions.

    var id = ($(this).parent().attr('id'));
    id = id.slice(4);
    $('#nav'+id).css('color','red');

Here id is the id of the question. Problem is I'm getting the following error

Parse error: syntax error, unexpected ';' in /var/www/sites/onlinetest/test.php on line #x

Also I'm pretty sure the following is wrong

$_SESSION['ques['id']']

since id is the javascript variable here. Please help me. Also I appreciate if any other better solution than 'storing in the session variables' is posted

6

5 Answers 5

1

1) Your problem lies in the last line of click block. I'm not entirely sure what you're trying to do, but you have <?php ; ?> and the semicolon is causing your script to fail to parse. The first part

<?php $_SESSION['ques['id']']= ?>

doesn't do anything either. It looks like you're trying to assign a Javascript value $(this).val() to the PHP SESSION global, which you can't do. You'll need to make an AJAX call back to your server to update your session var.

2) Use double quotes when nesting arrays like so: $_SESSION["$ques[id]"]

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Just store the answers in JS, and send them to the server when last question is answered.

Here's a really basic example of storing to an array:

var answers=[];

$('input[type=radio]').click(function()
{
    var id = ($(this).parent().attr('id'));
    id = id.slice(4);
    $('#nav'+id).css('color','red');
    answers.push($(this).val());
});

Sending it serverside:

$.ajax({
   type: 'POST',
   data: {answers : answers},
   url: '/path/myfile.php',
   success: function(data) {},
   error: function() {}
});

Catching it in PHP:

if (isset($_POST['answers'])) { 
   $answers = $_POST['answers']; 
}else{ 
   die('error'); 
}

This is the way I would do it, just show/hide the questions with javascript.

If you are refreshing the page for every question you could just store the answers in PHP on every new page load, should be pretty straight forward as long as you're not trying to use PHP code inside your JS file.

1 Comment

my whole point is not to loose data when the page is refreshed, which probably happens with all kinds of users.
0

I think we never get any client side script variable to server side script language

Solution store in some hidden form variable then at the form submit time you can get in GET or POST variable

Thanks

2 Comments

I cannot do this as if the user refreshes the page all the data is lost.
can we make one ajax call which pass that javascript value to some php page and at that time you can store that data in session
0

The code seems will not work as you expected even though you clear the errors.So use the ajax in jquery

Comments

0

The problem with PHP is that it gets executed before the javascript so you can't just add php and try to assign a javascript variable into php varivable.

You will have to use ajax to solve your problem. Do a ajax request and send all values trough GET. After this you can use all variables in your php file where you did the ajax request. Iterate trough all variables and insert them into the Session

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.