transfering html or javascript variable to php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

transfering html or javascript variable to php

Post by Mini-Me »

Ok, I'm making a quiz, and can't figure out how to keep score. Before I say anything else, here's the quiz, the php file in txt format, and a simplified version of the quiz:
http://minime001.net/quiz/
http://minime001.net/quiz/quiz.txt

top of page:

Code: Select all

<?php
  $page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : "";
  $self = getenv("SCRIPT_NAME");
?>
javascripts:

Code: Select all

if (document.images) &#123;
  cho= new Image(30,30);
  cho.src="c.PNG";
  rig= new Image(30,30);
  rig.src="r.PNG";
  wro= new Image(30,30);
  wro.src="w.PNG";
  nul= new Image(30,30);
  nul.src="n.PNG";
&#125;
var clicked = "no";
function switchImg(picName,imgName,nul1,nul2,nul3)
&#123;
  if (clicked == "no") &#123;
    if (document.images)
    &#123;
      imgOn=eval(imgName + ".src");
      document&#1111;picName].src= imgOn;
      imgOn2=eval("nul.src");
      document&#1111;nul1].src= imgOn2;
      imgOn3=eval("nul.src");
      document&#1111;nul2].src= imgOn3;
      imgOn4=eval("nul.src");
      document&#1111;nul3].src= imgOn4;
    &#125;
    clicked = "yes";
  &#125;
&#125;
question buttons:

Code: Select all

&lt;img src="c.PNG" border="0" alt="" name="choose1" onmouseover="this.style.cursor='pointer';" onClick="javascript:switchImg('choose1','wro','choose2','choose3','choose4');" /&gt;
&lt;img src="c.PNG" border="0" alt="" name="choose2" onmouseover="this.style.cursor='pointer';" onClick="javascript:switchImg('choose2','wro','choose1','choose3','choose4');" /&gt;
&lt;img src="c.PNG" border="0" alt="" name="choose3" onmouseover="this.style.cursor='pointer';" onClick="javascript:switchImg('choose3','rig','choose1','choose2','choose4');" /&gt;
&lt;img src="c.PNG" border="0" alt="" name="choose4" onmouseover="this.style.cursor='pointer';" onClick="javascript:switchImg('choose4','wro','choose1','choose2','choose3');" /&gt;
form:

Code: Select all

&lt;form action="&lt;?php print $self ?&gt;" method="get"&gt;
&lt;input type="hidden" name="page" value="q02" /&gt;
&lt;input type="submit" value="Next Question" /&gt;
&lt;/form&gt;
Problem is, I can't see how to set a variable in php when you click one of the pics, and I can't see how to read a html/javascript variable with php. I was talking to a guy in IRC and he said to use this:
<?=' . PHP_SELF . '?>?q#<script>document.writeln(var);</script>
but then I had to go and didn't get to hear an explanation of how to use it. ^^;
Any ideas?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

add hidden field(s) to your form and change it via javascript. You'll be able to access that/these field(s) in PHP via $_REQUEST superglobals.
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

hm.. I'm missing something here. I get adding these (right?):
$score = (isset($_REQUEST['score'])) ? $_REQUEST['score'] : "";
<input type="hidden" name="score" value="(what goes here?)" />
-> btw, I would put this /\ in the form I have, right? I wouldn't have to change the pictures into forms? 'cause that can't happen the way I have it--it'd screw the look over. :/

but I'm missing how I would change the input score with javascript. Can I simply create a javascript variable named score and set it how I want and it'll be read??
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

set is as

Code: Select all

<script language='JavaScript' type='text/javascript'>
function img_click() &#123;
  form = document.getElementById('id_of_your_form');
  form.score.value = 'whatever';
&#125;
</script>
<img src='blah-blah-blah.gif' onclick='img_click()' />
<form id='id_of_your_form' action='' method='POST'>
  <input type='hidden' name='score' id='score' value='' />
  <input type='submit' />
</form>
on submit you will get 'whatever' as value of $_REQUEST['score'] (if user has clicked on the image)
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

ah, wow, that's nifty. Thanks.
Post Reply