putting javascript variables into php file

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
bhaggert
Forum Newbie
Posts: 2
Joined: Wed Jun 26, 2002 3:07 pm

putting javascript variables into php file

Post by bhaggert »

Sorry if this has been answered before, I didn't see it when I searched the archives.

I have a javascript variable that I need to pass into a PHP block:
<script language="JavaScript">
<!-- Begin
function checkRadios() {

storeNum = document.storeSelector.storeRadio.value;
<?php
$storesVisited = fopen("beenthere_$NetID.txt" , "a+");//open file for

$storeNum = storeNum; //set PHP variable to JS variable
echo "alert($storeNum);";
fputs($storesVisited , $storeNum); //write variable to file
fclose($storesVisited); //close file
echo "alert($storeNum);";
?>

return true;
}
// End -->
</script>

The alerts both work as expected and output the contents of the JS variable (e.g. "Store1").
When viewing the text file however it has the variable name "storeNum", not the contents of the variable.
Help!?

PLease respond soonest as this is the last hangup to complete a project.

TIA,

Bryan
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you read this:
http://www.devnetwork.net/forums/viewtopic.php?t=1030

Basically the PHP is run on the server so it has already executed before everything reaches the browser and the Javascript is run. It's relatively easy to pass PHP variables to Javascript but the other way round is a different story...

Mac
bhaggert
Forum Newbie
Posts: 2
Joined: Wed Jun 26, 2002 3:07 pm

Post by bhaggert »

Thanks for the reply. I've read teh article and I understand clientside/serverside programming concepts.

What I don't understand is why the JS alert 'knows' the contents for the variable but the next line that writes to te file doesn't.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It's because you are sending the alert box the Javascript variable storeNum because the PHP variable $storeNum is equal to the string 'storeNum'. PHP knows nothing about Javascript all it sees is a bunch of strings it doesn't know what the value of storeNum is. So alert(storeNum) works because the Javascript variable storeNum has been set and when you write to the file you get the value of the PHP variable $storeNum which is 'storeNum' and not the Javascript variable storeNum.

Hopefully that makes some sense to you because I've started to confuse myself...

Mac
User avatar
FireyIce01
Forum Newbie
Posts: 6
Joined: Fri Jun 21, 2002 2:16 pm
Contact:

Post by FireyIce01 »

If you use document.write (into a hidden DIV would be my suggestion) you can write a dynamic form, and in the form, you can make variables, eg, <input type=hidden name=storeNum value='whatever'> and then post the form to the PHP, and that will pass the variables, beyond that I don't know what to suggest... if you come up with something let me know!
Post Reply