weixin_33686714 2016-09-27 11:57 采纳率: 0%
浏览 90

Ajax更改PHP变量

I've got this variable $type and I want it to be month or year. It should be changed by pressing a div.

I've tried creating an onclick event with an ajax call.

The ajax call and the variable are in the same script (index.php)

Inside the onclick function:

var curr_class = $(this).attr('class');


$.ajax({
    type: "POST",
    url: "index.php",
    data: {
        type: curr_class
    },
    dataType: 'text',
    success: function(data) {
        // Test what is returned from the server
        alert(data);
    }
});

But the alert returns the whole html page. When I console.log the data (create a var data = { type:curr_class }) and console.log *that data* it returnstype = month` (which is correct)

while I just want it to return month or year

So on top of the page I can call

if(empty($_POST['type'])){
     $type = 'month';
} else {
     $type = $_POST['type'];
}

and change the PHP variable so I can use it in the rest of my script.

But how can I accomplish this?

With kind regards,

  • 写回答

2条回答 默认 最新

  • weixin_33698823 2016-09-27 12:14
    关注

    as you are sending request to the same page so as a result full page is return .You will have to send it to another page and from that page return the type variable

    if(empty($_POST['type'])){
     $type = 'month';
     } else {
       $type = $_POST['type'];
       echo $type;
    

    keep this code in separate file and make an ajax call to that page

    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)