All functions work every time except the "btn_replay" which only works on the first iteration.
It does not use the session variable
File name is add_hock_query_js.php and is included in file ad_hoc_query.php
I have scoured the internet without a result and am stuck.

<?php session_start(); ?>
<script type="text/javascript">
$(document).ready(function(){
    $('input#btn').hide();
      $('#btn_clear, #btn_reset,#btn_save, #div_sql').hide();
      $('textarea[id="text"]').mouseover(function(){
             var xs = $(this).val();
             var xs = xs.substring(0, 6); 
            if (xs == 'select') {
                 $('input#btn, #btn_save').show();
            } else {                
                $('input#btn, #btn_save').hide();
            }
        });

  $("#btn_reset").click(function () {
       var d = "select * from  " ;     
       $("#text").val(d); 
       $('input#btn, #btn_save').hide();

     });  

  $("#btn_clear").click(function  ()  {
       var d = "" ;     
       $("#text").val(d);
       $('input#btn, #btn_save').hide();

     }); 

  $("#btn_save").click(function  ()  {
       var d = $('#text').val();   
       values = { "ad_hoc_query": d };  
       $.post("../inc/set_sessions.php", $.param(values));       
     });      

   $("#btn_replay").click(function()  {
       var q = '<?php echo $_SESSION["ad_hoc_query"]; ?>';       
       $("#text").val(q);
       $('input#btn').show();
     });

  $("#input_db").click(function () { 
       var d = $(this).val();  
       values = { "ad_hoc_db": d};   
       $.post("../inc/set_sessions.php", $.param(values)); 
       $('#div_sql, #btn_clear, #btn_reset').show();          
     });         

 });
</script>

File set_sessions.php

<?php session_start();?>
<?php

 foreach($_REQUEST as $key => $value)
{
    $_SESSION[$key] = $value;
}
?>

So if session variables are assigned in set_sessions.php, but as far as I can see you're only including it using an JS in the "replay" function.
Make sure you're including set_sessions.php into ad_hoc_query.php.

If thats not the case, run a simple test: place <?php echo $_SESSION['']; ?> somewhere in your <body> tag, and see if it has any output. This will tell us if the variable is being assigned the correct values.

I see no reason in your jQuery why it shouldn't work second time, but thats nothing to be certain of.

Member Avatar for diafol

Difficult to know what you're trying to do. So the only thing that's not working is...

$("#btn_replay").click(function()  {
   var q = '<?php echo $_SESSION["ad_hoc_query"]; ?>';       
   $("#text").val(q);
   $('input#btn').show();
 });

q will always be the same, even if you update the $_SESSION["ad_hoc_query"] variable via ajax on another control event, e.g. btn_save.

Your ajax $.post actions are not actually making changes to the page, they seem to be "post and forget" actions. They update the session variables on the server, but are doing nothing to change anything on the client (the live page in the browser).

1. Line 3 does update the current page. #text is a textarea input element.

I soved the problem by adding another php file. Also updated the .js file.

   $("#btn_replay").click(function()  {
       var d = "ad_hoc_query"; 
       values = { "get_session_key": d};
       $.post("../inc/set_sessions.php", $.param(values)); 

       $.post("../inc/get_session_var.php",function(result) {;
         $("#text").val(result);
       });   
       $('input#btn, #btn_save').show();
     });
<?php session_start(); ?>  
<?php 
    $k =   $_SESSION["get_session_key"];
    $new_sql = trim($_SESSION[$k]);   
    echo  trim($new_sql);
?> 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.