在 php 代码中使用 javascript 变量

2022-01-06 00:00:00 php javascript joomla

我一直在 joomla 中处理表单提交,但在使用 javascript 变量和 php JRoute_ 时遇到了一些麻烦

I have been working with a form submission in joomla and have a few trouble using javascript variable with php JRoute_

这是代码片段:

function ValidateForm()
{
    var loc = $("#locality").val();
    var action = '<?php echo JRoute::_('index.php?option=com_add&view=malllists&Itemid=233&districtname='.$dit.'&loc='Pass javascript Variable Here) ;?>';
    document.miniSurveyView481.action = action;
}

代替 Pass javascript Variable 这里我需要传递 loc 值我尝试了很多东西但找不到解决方案.

In the place of Pass javascript Variable Here I need to pass the loc value I tried plenty of things but Could not find a solution.

推荐答案

您不能在 php 代码中使用 javascript 变量.Php 代码在服务器端运行,javascript 代码在客户端运行.你不能要求浏览器运行 php 代码.

You can't use javascript variable in php code. Php code run's on the serverside and javascript code runs in the client side. You can't ask the browser to run php code.

只有当代码到达浏览器时,您的变量 loc 才会有值.

Your variable loc will have a value only when the code reaches the browser.

如果您想从服务器获取一些值并将其与 javascript 变量结合,请执行以下操作.

If you want to get some value from server and combine it with javascript variables then do the following.

使用 ajax 请求并将所需的值发送到服务器.服务器将返回一个响应.使用该响应文本并将其存储在您的操作变量中.

Use an ajax request and send the desired values to server. The server will return with a response. Use that response text and store it in your action variable.

相关文章