www.webdeveloper.com
Results 1 to 2 of 2

Thread: JavaScript value to PHP Array?

  1. #1
    Join Date
    Feb 2009
    Posts
    17

    JavaScript value to PHP Array?

    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 Code:
    if( quizInfo.hasQuit ){

                var 
    ScoreForPHP =  quizInfo.numOfRight;

                            
    WHAT NEXT?

            } 
    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

  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,129
    One way of doing this is sending the variable through the URL and picking it up with a PHP request. Here's a basic example:

    Code:
    <!doctype html>
    
    <html>
    	<head>
    		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    		<title>Title</title>
    		<meta name="language" content="en" />  
    		<script type="text/javascript">
    			//<![CDATA[  
    				function passVariable(){
    				
    					// set your variable
    					var x = '123';
    					
    					// get the current url and append your variable
    					var url = document.location.href + '?x=' + x;
    					
    					// to prevent looping, check to make sure your current url contains a common string
    					var exists = document.location.href.indexOf('?x=');
    					if(exists < 0){
    						// redirect passing your variable in
    						window.location = url;
    					}
    					
    				}
    			//]]> 
    		</script>
    	</head>
    	<body onload="passVariable()">
    		<?php echo $_REQUEST['x']; ?>
    	</body>
    </html>
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles