我可以在不使用AJAX的情况下在Javascript中运行PHP代码吗?

时间:2013-04-16 10:09:43

标签: php javascript

如何在JavaScript函数中不使用AJAX的情况下将JavaScript变量转换为php变量。

 function getProduct(category)
{
    document.galaxy.action = '<?php echo JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id=11') ?>';
    document.getElementById('galaxy').submit();
}

我需要将js变量“category”转换为php变量。

4 个答案:

答案 0 :(得分:1)

无效。

Javascipt 是一种客户端语言,这意味着它将在访问者的计算机上运行。

PHP 是一种服务器端语言,这意味着它将在您的服务器上运行。

答案 1 :(得分:0)

你做不到。查看网站时会发生什么:

  1. PHP执行服务器上的所有PHP代码
  2. 网络服务器将网站发送到客户端
  3. 客户端浏览器执行JavaScript
  4. 当您想要使用PHP的JavaScript变量时,您可以通过某种方式将其发送回服务器(AJAX)。

答案 2 :(得分:-1)

var DivVal=document.getElementById('ChkStatus');
       //var InnerVal=DivVal.innerHTML;
       var editRedirect='<?php echo $this->createUrl("Employee/Print"); ?>';
       window.location=editRedirect;

答案 3 :(得分:-2)

正如其他人所说,你对这些概念有点误解。

如果您希望将JS中的内容与表单一起发送到PHP脚本,则应将其放在表单字段中。

表单中的

使用隐藏值:

<input type="hidden" id="categoryInput" name="category" />
在你的JS中

//Before you submit do this..
document.getElementById('categoryInput').value = category;
PHP中的

$category = $_REQUEST['category'];

修改

好像你要求这样:

document.galaxy.action = '<?php echo JRoute::_('index.php?option=com_virtuemart&view=category') ?>&virtuemart_category_id=' + category;