如何将JavaScript变量用作PHP变量? [重复]

时间:2022-08-27 17:46:45

This question already has an answer here:

这个问题在这里已有答案:

I'm trying to include JavaScript variables into PHP code as PHP variables, but I'm having problems doing so. When a button is clicked, the following function is called:

我试图将JavaScript变量作为PHP变量包含在PHP代码中,但是我遇到了这样的问题。单击按钮时,将调用以下函数:

<script type="text/javascript">
    function addTraining(leve, name, date)
    {
        var level_var = document.getElementById(leve);
        var training_name_var = document.getElementById(name);
        var training_date_var = document.getElementById(date);

        <?php
            $result = "INSERT INTO training(level, school_name, training_date) VALUES('level_var', 'training_name_var', 'training_date_var')" or die("Query not possible.");
        ?>

</script>

Is it possible?

可能吗?

7 个解决方案

#1


36  

PHP is run server-side. JavaScript is run client-side in the browser of the user requesting the page. By the time the JavaScript is executed, there is no access to PHP on the server whatsoever. Please read this article with details about client-side vs server-side coding.

PHP在服务器端运行。 JavaScript在请求页面的用户的浏览器中运行客户端。在执行JavaScript时,无论如何都无法访问服务器上的PHP。请阅读本文,其中包含有关客户端和服务器端编码的详细信息。

What happens in a nutshell is this:

简而言之,这是:

  • You click a link in your browser on your computer under your desk
  • 您在桌面下的计算机上单击浏览器中的链接
  • The browser creates an HTTP request and sends it to a server on the Internet
  • 浏览器创建HTTP请求并将其发送到Internet上的服务器
  • The server checks if he can handle the request
  • 服务器检查他是否可以处理请求
  • If the request is for a PHP page, the PHP interpreter is started
  • 如果请求是针对PHP页面的,则启动PHP解释器
  • The PHP interpreter will run all PHP code in the page you requested
  • PHP解释器将在您请求的页面中运行所有PHP代码
  • The PHP interpreter will NOT run any JS code, because it has no clue about it
  • PHP解释器不会运行任何JS代码,因为它没有任何关于它的线索
  • The server will send the page assembled by the interpreter back to your browser
  • 服务器会将解释器汇编的页面发送回您的浏览器
  • Your browser will render the page and show it to you
  • 您的浏览器将呈现该页面并显示给您
  • JavaScript is executed on your computer
  • JavaScript在您的计算机上执行

In your case, PHP will write the JS code into the page, so it can be executed when the page is rendered in your browser. By that time, the PHP part in your JS snippet does no longer exist. It was executed on the server already. It created a variable $result that contained a SQL query string. You didn't use it, so when the page is send back to your browser, it's gone. Have a look at the sourcecode when the page is rendered in your browser. You will see that there is nothing at the position you put the PHP code.

在您的情况下,PHP会将JS代码写入页面,因此可以在浏览器中呈现页面时执行。到那时,JS片段中的PHP部分不再存在。它已经在服务器上执行了。它创建了一个包含SQL查询字符串的变量$ result。你没有使用它,所以当页面发送回你的浏览器时,它已经消失了。在浏览器中呈现页面时,请查看源代码。你会看到你放置PHP代码的位置没有任何东西。

The only way to do what you are looking to do is either:

做你想做的事的唯一方法是:

  • do a redirect to a PHP script or
  • 重定向到PHP脚本或
  • do an AJAX call to a PHP script
  • 对PHP脚本执行AJAX调用

with the values you want to be insert into the database.

使用要插入数据库的值。

#2


20  

<script type="text/javascript">
var jvalue = 'this is javascript value';

<?php $abc = "<script>document.write(jvalue)</script>"?>   
</script>
<?php echo  'php_'.$abc;?>

#3


4  

You seem to be confusing client-side and server side code. When the button is clicked you need to send (post, get) the variables to the server where the php can be executed. You can either submit the page or use an ajax call to submit just the data. -don

您似乎混淆了客户端和服务器端代码。单击按钮时,您需要将变量发送(发布,获取)到可以执行php的服务器。您可以提交页面或使用ajax调用来仅提交数据。 -don

#4


3  

PHP runs on the server. It outputs some text (usually). This is then parsed by the client.

PHP在服务器上运行。它输出一些文本(通常)。然后由客户端解析。

During and after the parsing on the client, JavaScript runs. At this stage it is too late for the PHP script to do anything.

在客户端解析期间和之后,JavaScript运行。在这个阶段,PHP脚本做任何事都为时已晚。

If you want to get anything back to PHP you need to make a new HTTP request and include the data in it (either in the query string (GET data) or message body (POST data).

如果您想获得任何返回PHP的信息,您需要创建一个新的HTTP请求并在其中包含数据(在查询字符串(GET数据)或消息体(POST数据)中)。

You can do this by:

你可以这样做:

  • Setting location (GET only)
  • 设置位置(仅限GET)
  • Submitting a form (with the FormElement.submit() method)
  • 提交表单(使用FormElement.submit()方法)
  • Using the XMLHttpRequest object (the technique commonly known as Ajax). Various libraries do some of the heavy lifting for you here, e.g. YUI or jQuery.
  • 使用XMLHttpRequest对象(通常称为Ajax的技术)。各种图书馆为您做了一些繁重的工作,例如: YUI或jQuery。

Which ever option you choose, the PHP is essentially the same. Read from $_GET or $_POST, run your database code, then return some data to the client.

您选择的哪个选项,PHP本质上是相同的。从$ _GET或$ _POST读取,运行数据库代码,然后将一些数据返回给客户端。

#5


1  

I had the same problem a few weeks ago like yours; but I invented a brilliant solution for exchanging variables between PHP and JavaScript. It worked for me well:

几个星期前我和你的一样有同样的问题;但我发明了一个在PHP和JavaScript之间交换变量的出色解决方案。它对我有用:

  1. Create a hidden form on a HTML page

    在HTML页面上创建隐藏的表单

  2. Create a Textbox or Textarea in that hidden form

    以隐藏的形式创建文本框或文本区域

  3. After all of your code written in the script, store the final value of your variable in that textbox

    在脚本中编写完所有代码之后,将变量的最终值存储在该文本框中

  4. Use $_REQUEST['textbox name'] line in your PHP to gain access to value of your JavaScript variable.

    在PHP中使用$ _REQUEST ['textbox name']行来访问JavaScript变量的值。

I hope this trick works for you.

我希望这个技巧对你有用。

#6


0  

You can do what you want, but not like that. What you need to do is make an AJAX request from JavaScript back to the server where a separate PHP script can do the database operation.

你可以做你想做的事,但不是那样的。您需要做的是从JavaScript返回到服务器的AJAX请求,其中单独的PHP脚本可以执行数据库操作。

#7


0  

You can take all values like this:

你可以采取这样的所有值:

$abc = "<script>document.getElementByID('yourid').value</script>";

#1


36  

PHP is run server-side. JavaScript is run client-side in the browser of the user requesting the page. By the time the JavaScript is executed, there is no access to PHP on the server whatsoever. Please read this article with details about client-side vs server-side coding.

PHP在服务器端运行。 JavaScript在请求页面的用户的浏览器中运行客户端。在执行JavaScript时,无论如何都无法访问服务器上的PHP。请阅读本文,其中包含有关客户端和服务器端编码的详细信息。

What happens in a nutshell is this:

简而言之,这是:

  • You click a link in your browser on your computer under your desk
  • 您在桌面下的计算机上单击浏览器中的链接
  • The browser creates an HTTP request and sends it to a server on the Internet
  • 浏览器创建HTTP请求并将其发送到Internet上的服务器
  • The server checks if he can handle the request
  • 服务器检查他是否可以处理请求
  • If the request is for a PHP page, the PHP interpreter is started
  • 如果请求是针对PHP页面的,则启动PHP解释器
  • The PHP interpreter will run all PHP code in the page you requested
  • PHP解释器将在您请求的页面中运行所有PHP代码
  • The PHP interpreter will NOT run any JS code, because it has no clue about it
  • PHP解释器不会运行任何JS代码,因为它没有任何关于它的线索
  • The server will send the page assembled by the interpreter back to your browser
  • 服务器会将解释器汇编的页面发送回您的浏览器
  • Your browser will render the page and show it to you
  • 您的浏览器将呈现该页面并显示给您
  • JavaScript is executed on your computer
  • JavaScript在您的计算机上执行

In your case, PHP will write the JS code into the page, so it can be executed when the page is rendered in your browser. By that time, the PHP part in your JS snippet does no longer exist. It was executed on the server already. It created a variable $result that contained a SQL query string. You didn't use it, so when the page is send back to your browser, it's gone. Have a look at the sourcecode when the page is rendered in your browser. You will see that there is nothing at the position you put the PHP code.

在您的情况下,PHP会将JS代码写入页面,因此可以在浏览器中呈现页面时执行。到那时,JS片段中的PHP部分不再存在。它已经在服务器上执行了。它创建了一个包含SQL查询字符串的变量$ result。你没有使用它,所以当页面发送回你的浏览器时,它已经消失了。在浏览器中呈现页面时,请查看源代码。你会看到你放置PHP代码的位置没有任何东西。

The only way to do what you are looking to do is either:

做你想做的事的唯一方法是:

  • do a redirect to a PHP script or
  • 重定向到PHP脚本或
  • do an AJAX call to a PHP script
  • 对PHP脚本执行AJAX调用

with the values you want to be insert into the database.

使用要插入数据库的值。

#2


20  

<script type="text/javascript">
var jvalue = 'this is javascript value';

<?php $abc = "<script>document.write(jvalue)</script>"?>   
</script>
<?php echo  'php_'.$abc;?>

#3


4  

You seem to be confusing client-side and server side code. When the button is clicked you need to send (post, get) the variables to the server where the php can be executed. You can either submit the page or use an ajax call to submit just the data. -don

您似乎混淆了客户端和服务器端代码。单击按钮时,您需要将变量发送(发布,获取)到可以执行php的服务器。您可以提交页面或使用ajax调用来仅提交数据。 -don

#4


3  

PHP runs on the server. It outputs some text (usually). This is then parsed by the client.

PHP在服务器上运行。它输出一些文本(通常)。然后由客户端解析。

During and after the parsing on the client, JavaScript runs. At this stage it is too late for the PHP script to do anything.

在客户端解析期间和之后,JavaScript运行。在这个阶段,PHP脚本做任何事都为时已晚。

If you want to get anything back to PHP you need to make a new HTTP request and include the data in it (either in the query string (GET data) or message body (POST data).

如果您想获得任何返回PHP的信息,您需要创建一个新的HTTP请求并在其中包含数据(在查询字符串(GET数据)或消息体(POST数据)中)。

You can do this by:

你可以这样做:

  • Setting location (GET only)
  • 设置位置(仅限GET)
  • Submitting a form (with the FormElement.submit() method)
  • 提交表单(使用FormElement.submit()方法)
  • Using the XMLHttpRequest object (the technique commonly known as Ajax). Various libraries do some of the heavy lifting for you here, e.g. YUI or jQuery.
  • 使用XMLHttpRequest对象(通常称为Ajax的技术)。各种图书馆为您做了一些繁重的工作,例如: YUI或jQuery。

Which ever option you choose, the PHP is essentially the same. Read from $_GET or $_POST, run your database code, then return some data to the client.

您选择的哪个选项,PHP本质上是相同的。从$ _GET或$ _POST读取,运行数据库代码,然后将一些数据返回给客户端。

#5


1  

I had the same problem a few weeks ago like yours; but I invented a brilliant solution for exchanging variables between PHP and JavaScript. It worked for me well:

几个星期前我和你的一样有同样的问题;但我发明了一个在PHP和JavaScript之间交换变量的出色解决方案。它对我有用:

  1. Create a hidden form on a HTML page

    在HTML页面上创建隐藏的表单

  2. Create a Textbox or Textarea in that hidden form

    以隐藏的形式创建文本框或文本区域

  3. After all of your code written in the script, store the final value of your variable in that textbox

    在脚本中编写完所有代码之后,将变量的最终值存储在该文本框中

  4. Use $_REQUEST['textbox name'] line in your PHP to gain access to value of your JavaScript variable.

    在PHP中使用$ _REQUEST ['textbox name']行来访问JavaScript变量的值。

I hope this trick works for you.

我希望这个技巧对你有用。

#6


0  

You can do what you want, but not like that. What you need to do is make an AJAX request from JavaScript back to the server where a separate PHP script can do the database operation.

你可以做你想做的事,但不是那样的。您需要做的是从JavaScript返回到服务器的AJAX请求,其中单独的PHP脚本可以执行数据库操作。

#7


0  

You can take all values like this:

你可以采取这样的所有值:

$abc = "<script>document.getElementByID('yourid').value</script>";