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
-
1possible duplicate of Set Session variable using javascript in PHPMichael Berkowski– Michael Berkowski2012年02月24日 14:05:30 +00:00Commented Feb 24, 2012 at 14:05
-
This won't work at all. You will need AJAX requests.Michael Berkowski– Michael Berkowski2012年02月24日 14:06:19 +00:00Commented Feb 24, 2012 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– Jeremy Harris2012年02月24日 14:07:29 +00:00Commented Feb 24, 2012 at 14:07
-
is there any other solution than storing in session variablesKrishna Deepak– Krishna Deepak2012年02月24日 14:07:47 +00:00Commented Feb 24, 2012 at 14:07
-
possible duplicate of How do I assign a javascript variable to a PHP variable? and stackoverflow.com/questions/4747186/… and stackoverflow.com/questions/5581546/… and stackoverflow.com/questions/3711495/…Quentin– Quentin2012年02月24日 14:14:46 +00:00Commented Feb 24, 2012 at 14:14
5 Answers 5
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.
Comments
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
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
The code seems will not work as you expected even though you clear the errors.So use the ajax in jquery
Comments
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