Is there a way to pass jQuery variables to PHP variables and from PHP to jQuery? I want to take and ID from a div (wich is generated with PHP) with jQuery and give that value to a variable.
            Asked
            
        
        
            Active
            
        
            Viewed 186 times
        
    -3
            
            
        - 
                    yes, learn AJAX (and possibly jquery ajax) – Aug 18 '13 at 11:51
- 
                    There are literally _thousands_ of questions on this topic. Look at those listed in the Related section of the sidebar as a start. http://stackoverflow.com/search?q=%5Bphp%5D+pass+javascript+variable+to+php – Michael Berkowski Aug 18 '13 at 11:52
- 
                    What makes the div different from other elements in the page, in order to select that div, you will need a unique selector, so what make it different? – Nabil Kadimi Aug 18 '13 at 11:53
- 
                    possible duplicate of [Pass a PHP string to a Javascript variable (and escape newlines)](http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-and-escape-newlines) and http://stackoverflow.com/questions/12261113/passing-variable-javascript-into-php – Michael Berkowski Aug 18 '13 at 11:55
- 
                    possible duplicate of [how to pass jquery variables to php variable?](http://stackoverflow.com/questions/5202070/how-to-pass-jquery-variables-to-php-variable) – Marcel Gwerder Aug 18 '13 at 11:56
2 Answers
2
            PHP to jQuery :
<script type="text/javascript">
    jQuery(function($){
        $('#<?php echo $elementId; ?>').hide();
    });
</script>
jQuery to PHP :
<script type="text/javascript">
    jQuery(function($){
        $.post('page.php', { var: 'value' }).done(function(data){
            // Do something
        });
    });
</script>
 
    
    
        mimipc
        
- 1,354
- 2
- 14
- 28
-2
            
            
        You can use ajax in post or get here is an example
//Javascript file
$("input[type=checkbox]").click(function () {
   $.post('my_ajax_receiver.php', 'val=' + $(this).val(), function (response) {
      alert(response);
   });
});
//PHP file ajax_remote.php
<?php
   $value = $_POST['val'];
   echo "I got your value! $value";
?>
 
    
    
        Sjaak Nabuurs
        
- 62
- 5
- 
                    1
- 
                    If there's a helpful answer already on SO, it is best to just link there, rather than to duplicate it. – halfer Aug 18 '13 at 12:34
