i have a couple of variables that i want to save or not save to a database depending on whether or not i have set them as hidden/visible in a javascript function, i have searched for a while but cant find anything. Thanks!
            Asked
            
        
        
            Active
            
        
            Viewed 7,231 times
        
    2 Answers
5
            That's absolutely impossible since PHP runs on the server while JavaScript and CSS are client-side.
The only thing you can do is checking if the element is visible using JavaScript and sending that data to your PHP script, e.g. via a hidden <input> field.
 
    
    
        ThiefMaster
        
- 310,957
- 84
- 592
- 636
- 
                    is there any code that i can use to completely remove an element in the javascript based on its id? – user1445218 Jul 11 '12 at 19:47
- 
                    Yes, with `elem` being the element: `elem.parentNode.removeChild(elem)` – ThiefMaster Jul 11 '12 at 19:51
- 
                    for some reason this doesnt work, i saw the exact same code that you posted somewhere else and already tried it – user1445218 Jul 11 '12 at 20:01
3
            
            
        You can also have this alternative. NOTE that the page has to refresh before this take effect (i.e. before PHP has knowledge of what was going on)
//Your JavaScript
function setHidden()
{
   document.getElementById('elementForVar1').visibility = 'hidden';
   //use this to indicate field/variable is hidden. PHP will use this later
   document.getElementById('elementForVar1HiddenField').value = 1;
}
declare hidden fields in your form to store the states of the variables
<form name="xxx">
    <input type="hidden" id="elementForVar1HiddenField" name="elementForVar1HiddenField" value="0" />
</form>
Your javascript simply set the value of the hidden field to 1 indicating var1 is hidden
<?php
  if($_POST['elementForVar1HiddenField'] == 1)
     //variable was hidden
?>
 
     
    