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

Krishna Deepak
  • 1,697
  • 2
  • 19
  • 30
  • 1
    possible duplicate of [Set Session variable using javascript in PHP](http://stackoverflow.com/questions/3590293/set-session-variable-using-javascript-in-php) – Michael Berkowski Feb 24 '12 at 14:05
  • This won't work at all. You will need [AJAX requests.](http://stackoverflow.com/search?q=%5Bjavascript%5D+php+session+variable+ajax&submit=search) – Michael Berkowski Feb 24 '12 at 14:06
  • As a note, PHP is executed server side. If you include it in your javascript like that, it will execute before the javascript is loaded/run and won't work. – Jeremy Harris Feb 24 '12 at 14:07
  • is there any other solution than storing in session variables – Krishna Deepak Feb 24 '12 at 14:07
  • possible duplicate of [How do I assign a javascript variable to a PHP variable?](http://stackoverflow.com/questions/3708925/how-do-i-assign-a-javascript-variable-to-a-php-variable) and http://stackoverflow.com/questions/4747186/how-to-assign-the-value-of-a-javascript-variable-to-a-php-variable and http://stackoverflow.com/questions/5581546/is-it-possible-to-assign-a-javascript-value-to-a-php-variable and http://stackoverflow.com/questions/3711495/how-do-i-assign-javascript-variable-to-php-variable-with-out-submitting-the-for – Quentin Feb 24 '12 at 14:14
  • Please donot votedown this question. I asked this question to see if any other solutions exist other than storing in session variables – Krishna Deepak Feb 24 '12 at 14:18

5 Answers5

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.

SenorAmor
  • 3,321
  • 15
  • 27
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.

adeneo
  • 303,455
  • 27
  • 380
  • 377
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

Saiyam Patel
  • 1,160
  • 9
  • 18
0

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

sathishkumar
  • 1,700
  • 4
  • 19
  • 30
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

Marcio
  • 562
  • 4
  • 20