如何在AJAX中编写PHP [英] How to Write PHP in AJAX

查看:54
本文介绍了如何在AJAX中编写PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ajax中获取会话..因为我已经编写了这样的代码

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');
           }
        });

但是如果我这样写的话,ajax就无法工作..请帮助我在ajax中获取会话。提前谢谢。

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

推荐答案

我在joomla中使用这种格式的ajax

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 
                 }                     
          });

在你的组件/控制器中你应该有一个文件yourcontroller.json.php来处理你的电话和return encoded json数组将是客户端所需的所有数据

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

这篇关于如何在AJAX中编写PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆