简体   繁体   English

是否可以在不使用AJAX的情况下以Javascript运行PHP代码?

[英]Can I run PHP code in Javascript without using AJAX?

How to convert JavaScript variable to php variable without using of AJAX inside the JavaScript function. 如何在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();
}

I need to convert js variable "category" to php variable. 我需要将js变量“类别”转换为php变量。

This is not posible. 这是不可能的

Javascipt is an client-side language, this means that its going to run on the computer of your visitor. Javascipt是一种客户端语言,这意味着它将在访问者的计算机上运行。

PHP is an server-side language, this means that is going to run on your server. PHP是一种服务器端语言,这意味着它将在您的服务器上运行。

You can't. 你不能 What happens when a site is viewed: 浏览网站时会发生什么:

  1. PHP executes all PHP code on the server PHP在服务器上执行所有PHP代码
  2. The Webserver sends the site to the client Web服务器将站点发送到客户端
  3. The client browser executes JavaScript 客户端浏览器执行JavaScript

When you want use a JavaScript variable with PHP, you have so send it back to the server in some way (AJAX). 如果要在PHP中使用JavaScript变量,则可以通过某种方式将其发送回服务器(AJAX)。

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

As other people mentioned, you are misunderstanding the concepts a little. 正如其他人所提到的,您对概念有些误解。

If you want something from your JS sent along with the form to your PHP script you should put it in a form field. 如果您希望将JS中的某些内容与表单一起发送到PHP脚本,则应将其放在表单字段中。

in your form use a hidden value: 在您的表单中使用隐藏值:

<input type="hidden" id="categoryInput" name="category" />

in your JS: 在您的JS中:

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

in your PHP: 在您的PHP中:

$category = $_REQUEST['category'];

EDIT: 编辑:

Seems like you are asking for this: 好像您要这样:

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

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

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