如何将JavaScript变量用作PHP变量? [英] How can I use a JavaScript variable as a PHP variable?

查看:183
本文介绍了如何将JavaScript变量用作PHP变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

<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>

有可能吗?

推荐答案

PHP在服务器端运行。 JavaScript在请求页面的用户的浏览器中运行客户端。在执行JavaScript时,无论如何都无法访问服务器上的PHP。请阅读本文,详细了解客户端与服务器端编码

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.

简而言之,这是:


  • 您在桌面下的计算机上单击浏览器中的链接

  • 浏览器创建HTTP请求并将其发送到服务器在Internet上

  • 服务器检查他是否可以处理请求

  • 如果请求是针对PHP页面的,则启动PHP解释器

  • PHP解释器将在您请求的页面中运行所有PHP代码

  • PHP解释器不会运行任何JS代码,因为它没有任何关于它的线索

  • 服务器会将解释器汇编的页面发送回您的浏览器

  • 您的浏览器将呈现该页面并显示给您

  • JavaScript在您的计算机上执行

  • 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
  • The server checks if he can handle the request
  • If the request is for a PHP page, the PHP interpreter is started
  • The PHP interpreter will run all PHP code in the page you requested
  • The PHP interpreter will NOT run any JS code, because it has no clue about it
  • 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

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

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.

你要做的事情的唯一方法是:

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


  • 重定向到PHP脚本或

  • 对PHP脚本执行AJAX调用

包含要插入数据库的值。

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

这篇关于如何将JavaScript变量用作PHP变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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