在PHP代码中使用javascript变量 [英] Use javascript variable in php code

查看:63
本文介绍了在PHP代码中使用javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用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变量在这里我需要传递loc值I尝试了很多东西,但无法找到解决方案。

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.

这篇关于在PHP代码中使用javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆