简体   繁体   English

将JavaScript变量从CakePHP视图传递到控制器

[英]Passing JavaScript Variables from a CakePHP View to Controller

I'm new to CakePHP and plan on developing an application with it. 我是CakePHP的新手,并计划使用它开发应用程序。 I was wondering if there is a way to pass JavaScript variables created in a View to it's corresponding Controller? 我想知道是否有一种方法可以将在视图中创建的JavaScript变量传递给它的相应Controller?

For example, if I generated two coordinate variables using the HTML5 Geolocation JavaScript API in a View, how could I pass these JS variables to a corresponding Controller in order to use them to query a database in a function? 例如,如果我在视图中使用HTML5 Geolocation JavaScript API生成了两个坐标变量,如何将这些JS变量传递给相应的Controller,以便使用它们在函数中查询数据库?

Thanks for the help! 谢谢您的帮助!

Use AJAX Make a request to that controller action and send those values. 使用AJAX向该控制器动作发出请求并发送这些值。 The controller should send back a response that you can use in javascript 控制器应发送回响应,您可以在javascript中使用

You can: 您可以:

  • Use AJAX 使用AJAX
  • Use JavaScript to submit a form 使用JavaScript提交表单
  • Use JavaScript to redirect to a URL with ?get variables 使用JavaScript重定向到带有?get变量的URL

Use Ajax and send javascript values as query string in your URL. 使用Ajax并将javascript值作为查询字符串发送到您的URL中。

In controller, you can get those values using PHP way $_Get['name'] or using cake way $this->request->query['variable_name']; 在控制器中,可以使用PHP方式$ _Get ['name']或蛋糕方式$ this-> request-> query ['variable_name'];获得这些值。

Hope it helps. 希望能帮助到你。

基本上,您需要以任何您喜欢的方式将数据发送到控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM