Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

javascript - 如何使用jquery / ajax将变量发送到php文件,然后将php回显到索引(无警报)(How to send variables to php file with jquery/ajax and then echo the php to index (no alert))

I'm a newbie around here and I've run into a dead end.

(我是这里的新手,已经走到了死胡同。)

So I've created a function to draw a calendar in php.

(因此,我创建了一个在php中绘制日历的函数。)

And I've been using css to set the colors of days and that stuff.

(而且我一直在使用CSS来设置日期和其他内容的颜色。)

The function runs well and uses 2 input variables like this:

(该函数运行良好,并使用了两个输入变量,如下所示:)

PHP file (calendar.php):

(PHP文件(calendar.php):)

<?php
function draw_calendar($month,$year)
{
// the code
}
echo draw_calendar($current_month,$current_year);
?>

What I've been trying to do is use jquery/ajax (from my index file) to post/get variables to calendar.php for $current_month and $current_year to use.

(我一直想做的是使用jquery / ajax(从我的索引文件中)将$ current_month和$ current_year的变量发布/获取到calendar.php中。)

And then echo the function to my index somehow.

(然后以某种方式将函数回显到我的索引。)

For the life of my I can't manage to do that.

(在我的一生中,我无法做到这一点。)

I could only find how to return simple strings and alert them to my index.

(我只能找到如何返回简单字符串并将它们提醒到我的索引。)

  ask by C B translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am not sure what code you have written up, but her is the general idea of how you can use ajax to send over your needed variables.

(我不确定您编写了什么代码,但是她是您如何使用ajax发送所需变量的基本思路。)

On your index file:

(在索引文件上:)

var month = "11"; //your month
var year = "2019"; //your year

$.ajax({
  type: "POST",
  url: "calendar.php",
  data: {month: month, year: year},
  dataType: "json",
  success: function(response){
    //if it is success
  },
  error: function(){
    //if it results in error
  }
});

On your calendar file:

(在您的日历文件上:)

$month = $_POST['month'];
$year = $_POST['year'];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...