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
putting javascript variables into php file
Moderator: General Moderators
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
Hopefully that makes some sense to you because I've started to confuse myself...
Mac
- FireyIce01
- Forum Newbie
- Posts: 6
- Joined: Fri Jun 21, 2002 2:16 pm
- Contact:
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!