0
votes

I am trying to get my session in ajax.. for that i had written my code like this

BTLJ.ajax({
           type: "POST",
           url: btlOpt.BT_AJAX,
           data: datasubmit,
           success: function(html){
                //if html contain "Registration failed" is register fail
              BTLJ("#btl-register-in-process").hide();  
              if(html.indexOf('$error$')!= -1){
                  ...
                  ...
                  }
               }else{                  
                   BTLJ(".btl-formregistration").children("div").hide();
                   BTLJ("#btl-success").html(html); 
                   BTLJ("#btl-success").show(); 
                   alert(<?php session_start(); print_r($_SESSION); ?>);
                   setTimeout(function() { ); BTLJ(".kcregbox").show();},7000);
                  // BTLJ("#btl-success").hide();
               }
           },
           error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus + ': Ajax request failed');
           }
        });

but ajax is not working if i write like that.. please help me in getting my session in ajax. thanks in advance.

2
Have you tried observing your browsers console to see what happens with the request/response? - juco
or run the php file on its own with no ajax from the url and see what it outputs. - nathan hayfield
@Gireesh : you cannot alert session like this because the php code generated is generated in the first access of your page so you cannot generate dynamic php while using this page in the traditional way but you must send a request to an external php script in the background that return your session value then take it by using your ajax or jquery or javascript to handle it out :) - Last Breath

2 Answers

1
votes

Your AJAX query in Joomla should be formatted as below.

           jQuery.post('index.php',{
                'option'        : 'com_componentname',
                'controller'    : 'controllername',
                'task'          : 'task_name',
                'format'        : 'raw',            
                'data'          :  data
            }).success(function(result) { 
                //if the request is success
            }).error(function() { 
                //if the request is fails
            });
1
votes

I'm using this format for ajax in joomla

$.ajax({
        type: 'GET',
        url: 'index.php', 
        data: {option: 'com_componenetname', task: 'taskname.youroperation', format: 'json', tmpl: 'raw'},
    dataType: 'json',
        async: true, // can be false also
        error: function(xhr, status, error) {
                console.log("AJAX ERROR in taskToggleSuceess: ")
                var err = eval("(" + xhr.responseText + ")");
                console.log(err.Message);
                },
        success: function(response){

                // on success do something
                // use response.valuname for server's data
                        }
                ,
        complete: function() {
            // stop waiting if necessary 
                 }                     
          });

In your component/controllers you should have a file yourcontroller.json.php which will process your call and return encoded json array will all the data you need in the client