JavaScript value to PHP Array?

Soldato
Joined
30 Nov 2005
Posts
3,086
Location
London
Hey,

Can anyone help me pass a JavaScript value to PHP.

It's based around jQuizMe script. I'm trying to pass the JavaScript value to a new page, so I can do what I like with it in PHP.

quizInfo.numOfRight is the JavaScript array of the value of the persons final quiz score is stored in.

PHP:
if( quizInfo.hasQuit ){

			SOMETHING HERE with quizInfo.numOfRight

		}

I'm thinking that perhaps we will need to redirect the person to another page when quizInfo.hasQuit is ran, then storing the JavaScript value into a PHP Array.

But rather confused about how I go about it.

Thanks
 
You could stick it in a query parameter:
Construct the URL then navigate to that page.
Code:
var newUrl = 'resultspage.php?correct=' + quizInfo.numOfRight;
window.location = newUrl;
Then php can grab it from the query param collection (Don't know the code for this, not a PHP dev)

You could also have a look at the jQuery library and post data to the server via an AJAX call.
 
Last edited:
You could stick it in a query parameter:
Code:
var newUrl = 'resultspage.php?correct=' + quizInfo.numOfRight;
window.location = newUrl;
Then php can grab it from the query param collection (Don't know the code for this, not a PHP dev)


It'll be stored in $correct if I remember correctly. Long time since I messed with php.

Is the javascript value the array of multiple scores or something?

if you pass it like the above then add a delimiter like a comma and then I think maybe base64 encode it to pass it to the php script.

Then you can decode it and explode the string into a php array.

If you're worried about changing to a higher score then implement some kind of obfuscation in the score value, so they'd have to work out how the number is calculated before they can change it. Still possible but longer.
 
Last edited:
Thanks for your help.

This does seem like a very long winded process.

Is there nothing simpler?

Like once
PHP:
if( quizInfo.hasQuit ){
is executed could I not pass it into a FORM and use POST?
 
Last edited:
Well if you want to prevent users from changing the scores themselves then you've got to do something to prevent that. Simple scripts are simple to abuse.
 
Thanks m0rte that works.

Though my only worry would be that people could technically change the URL to a higher score.

No probs.
As I mentioned before AJAX would be a good way to send the score to the server, sort of in the background where the user can't see it.
For example:
The javascript in quiz.php sends an AJAX request to results.php?correct=numOfRight. results.php responds with a url or some other data which you can use to update quiz.php or redirect to the returned url - up to you. This all happens without a page refresh or the results URL being visible.
Hope that makes sense?
Either way, learn to love AJAX, its frigging awesome :D
 
Could something along these lines work?

PHP:
		if( quizInfo.hasQuit ){
			
document.getElementById('scores').value=quizInfo.numOfRight;
		}

Then further below:

PHP:
<input name="scores" type="text" value="scores" size="10" readonly/>

Though I think there's something I'm missing above.
 
BOOM! Worked it out!

PHP:
if( quizInfo.hasQuit ){
document.forms['f1'].score.value = quizInfo.numOfRight;	
		}

PHP:
<form name='f1'>
		<input name="score" type="text" value=""  readonly/>
</form>

Works perfectly!

Now I can submit it using PHP and POST :D

Thanks all!
 
Back
Top Bottom