通过AJAX刷新传递php变量

时间:2021-07-10 00:06:07

I simply explain my "problem". I know nothing about JS and AJAX. But I'd make a simple AJAX function:

我只是解释我的“问题”。我对JS和AJAX一无所知。但我会做一个简单的AJAX函数:

  • My Menu "projects" displays the last 5 projects to which the user is involved
  • 我的菜单“项目”显示用户参与的最后5个项目

  • When you click on one of these projects, a small drop-down list appears and you can choose "Tasks", "presentation", "Discussions", etc..
  • 当您单击其中一个项目时,会出现一个小的下拉列表,您可以选择“任务”,“演示”,“讨论”等。

  • When you click on "Tasks" for example, the right part of the website is updated by displaying tasks on the project.
  • 例如,当您单击“任务”时,通过在项目上显示任务来更新网站的右侧部分。

Not refreshing the entire page, just a div on the right.

没有刷新整个页面,只是右侧的div。

I have to pass the ID of the project that I want to see the tasks to the right div (which simply loads tasks.php) so that it displays project tasks that I selected!

我必须将我希望看到任务的项目的ID传递给正确的div(它只是加载tasks.php),以便它显示我选择的项目任务!

By asking a few people I was advised to pass the ID of the project by putting it in a "rel" of the div of the project, that I would recover after tasks.php, only I did not have a clue how to put it into practice, as simply as possible!

通过询问一些人我被建议通过将项目的ID放在项目div的“rel”中来传递项目的ID,我将在tasks.php之后恢复,只是我没有线索如何把它付诸实践,尽可能简单!

If you could help me, it would be very helpful :)

如果你能帮助我,那将非常有帮助:)

1 个解决方案

#1


0  

Seems like all you need is to build a url to your php page. Something along the lines of tasks.php?projectId=[project id goes here]. This url would be built when the user clicked on one of the projects.

好像你需要的只是为你的php页面建立一个url。 task.php的一些东西?projectId = [项目ID到这里]。当用户单击其中一个项目时,将构建此URL。

For the request itself, I suggest you use jQuery's $.get() function (http://api.jquery.com/jQuery.get/).

对于请求本身,我建议你使用jQuery的$ .get()函数(http://api.jquery.com/jQuery.get/)。

In PHP, you could then use $_GET["projectId"] to retrieve the value, and act accordingly. As far as how to transfer data between the two, I'd recommend encoding your PHP response as json (using the proper content encoding, a simple set of header directives will do), obviously you'll need to fomat the JSON to something like:

在PHP中,您可以使用$ _GET [“projectId”]来检索值,并相应地执行操作。至于如何在两者之间传输数据,我建议将你的PHP响应编码为json(使用正确的内容编码,一组简单的头指令就可以了),显然你需要将JSON格式化为类似的东西。 :

{tasks: [{id: 'task1', name: 'My task', ...}, {id: 'task2', name: 'my other task', ...}]}

{tasks:[{id:'task1',name:'My task',...},{id:'task2',name:'my other task',...}]}

Does this answer your question?

这回答了你的问题了吗?

#1


0  

Seems like all you need is to build a url to your php page. Something along the lines of tasks.php?projectId=[project id goes here]. This url would be built when the user clicked on one of the projects.

好像你需要的只是为你的php页面建立一个url。 task.php的一些东西?projectId = [项目ID到这里]。当用户单击其中一个项目时,将构建此URL。

For the request itself, I suggest you use jQuery's $.get() function (http://api.jquery.com/jQuery.get/).

对于请求本身,我建议你使用jQuery的$ .get()函数(http://api.jquery.com/jQuery.get/)。

In PHP, you could then use $_GET["projectId"] to retrieve the value, and act accordingly. As far as how to transfer data between the two, I'd recommend encoding your PHP response as json (using the proper content encoding, a simple set of header directives will do), obviously you'll need to fomat the JSON to something like:

在PHP中,您可以使用$ _GET [“projectId”]来检索值,并相应地执行操作。至于如何在两者之间传输数据,我建议将你的PHP响应编码为json(使用正确的内容编码,一组简单的头指令就可以了),显然你需要将JSON格式化为类似的东西。 :

{tasks: [{id: 'task1', name: 'My task', ...}, {id: 'task2', name: 'my other task', ...}]}

{tasks:[{id:'task1',name:'My task',...},{id:'task2',name:'my other task',...}]}

Does this answer your question?

这回答了你的问题了吗?