Good And I am a sinner, because with a question on sunday!

In general, 1 is the only page PHP + Jquery. Click on the text - the POST request is on, the page reloads the part. The second and subsequent times do not pass. To check the variable introduced into the session, it turned out that the work is not going with the session. Again, the same thing on the hosting.

What's the matter? Can anyone tell me?

<? session_start(); //страница, при передаче POST запроса Ajax'ом if($_SERVER["HTTP_X_REQUESTED_WITH"]=="XMLHttpRequest"){ //просто меняем переменную туда-сюда if(isset($_SESSION['catalog_tray']['all']) AND $_SESSION['catalog_tray']['all']=='yes'){ $_SESSION['catalog_tray']['all']=='no'; }else{ $_SESSION['catalog_tray']['all']=='yes'; } //блок для обновления echo ' <div id="super_id_101" class=" buttonUpload" value="101">101</div> <div id="super_id_115" class=" buttonUpload" value="115">115</div> '; echo time(); echo '<pre>';print_r($_SESSION['catalog_tray']);echo '</pre>'; } //страница, НЕ при передаче POST запроса Ajax'ом if($_SERVER["HTTP_X_REQUESTED_WITH"]!="XMLHttpRequest"){ ?> <html> <head> <meta charset="utf-8"> <style> body,table,tr,td,div,select,input,a{ font-family: calibri;} a{ text-decoration:none;} </style> <script type="text/javascript" src="js/jquery2.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".buttonUpload").click(function(){ $.ajax({ url:"index.php", type:"POST", cache:true, data: "id="+$(this).attr("value"), success: function(html){ $("#category_tray_one").html(html); } }) }); }); </script> </head> <body> <div id="category_tray_one" class=""> <div id="super_id_101" class=" buttonUpload" value="101">101</div> <div id="super_id_115" class=" buttonUpload" value="115">115</div> </div> </body> </html> <? } ?> 

    2 answers 2

    All because the previously declared click function is not bound to dynamically added elements. One solution to the problem

     <? // все ошибочки показываем ini_set('display_errors', 1); error_reporting(E_ALL); header("Content-Type: text/html; charset=utf-8"); session_start(); //страница, при передаче POST запроса Ajax'ом if ($_POST){ //блок для обновления echo '<div id="super_id_110" class="buttonUpload" value="110">110</div> <div id="super_id_120" class="buttonUpload" value="120">120</div>'; echo time(); } //страница, НЕ при передаче POST запроса Ajax'ом if(!$_POST){ ?> <html> <head> <meta charset="utf-8"> <style> body,table,tr,td,div,select,input,a{ font-family: calibri;} a{ text-decoration:none;} </style> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ function update(){ $(".buttonUpload").click(function(){ $.ajax({ url:"index.php", type:"POST", cache:true, data: "id="+$(this).attr("value"), success: function(html){ $("#category_tray_one").html(html); update(); } }) }); } update(); }); </script> </head> <body> <div id="category_tray_one" class=""> <div id="super_id_101" class="buttonUpload" value="101">101</div> <div id="super_id_115" class="buttonUpload" value="115">115</div> </div> </body> </html> <? } ?> 

      jquery 1.7+ can be done differently

       <script> $(document).ready(function(){ $('#element').on('click', function(){ //или $(document).on('click', '#element', function(){ //ваш код }); }); </script> 

      and everything will work fine

      • For earlier versions there is a live function. - ilyaplot