简体   繁体   English

将变量从Javascript函数传递到同一php文件中的php

[英]Pass variable from Javascript function to php in the same php file

I need help to know how to pass a javascript variable in a function on php file, to a php variable. 我需要帮助来了解如何将php文件中的函数中的javascript变量传递给php变量。

How could I get the vars 'marca' and 'sucursal' from the javascript function in 2 variables in the php section? 如何从php部分的2个变量中的javascript函数中获取vars'marca'和'sucursal'?

Thanks in advance for your help 在此先感谢您的帮助

Here is my code 这是我的代码

            if (trim($linealtl)==$ltl) 
            {

                echo "<script>";

                echo "var marca;";
                echo "var sucursal;";

                echo "marca='0';";
                echo "sucursal='0';";

                echo "while(marca != 'L' && marca != 'D')";
                echo "{";
                    echo "marca = prompt('Introduce (L) para Lamp (D) para Duck para la LTL " . $ltl . "');";
                echo "}";

                echo "while(sucursal != '50' && sucursal != '62' && sucursal != 'VO' && sucursal != 'MO' && sucursal != 'NA')";
                echo "{";

                    echo "sucursal = prompt('¿A que sucursal pertenece la LTL " . $ltl . "?' + String.fromCharCode(13)";
                                    echo "+'   Valdemoro Suc 50 (50)' + String.fromCharCode(13)";
                                    echo "+'   Montornes Suc 62 (62)' + String.fromCharCode(13)";
                                    echo "+'   Valdemoro Otros (VO)' + String.fromCharCode(13)";
                                    echo "+'   Montornes Otros (MO)' + String.fromCharCode(13)";
                                    echo "+'   Ninguna de las anteriores (NA)');";
                echo "}";

                echo "</script>";

                fwrite($fileltl, $linealtl . ";" . $marca . ";" . $sucursal);
                $ltlencontrada = 'S';
            }

You can't. 你不能 There's no way to run JavaScript before you send the html to the client, since it runs in the browser (unless you implement a nodejs hack or switch to it entirely). 在将html发送到客户端之前,无法运行JavaScript,因为它是在浏览器中运行的(除非您实现了nodejs hack或完全切换为它)。

You can either use AJAX to return the calculation from the client once ran or just perform said calculations in php beforehand. 您可以使用AJAX在运行后从客户端返回计算结果,也可以预先在php中执行上述计算。 Personally, I'd go for the former. 就个人而言,我会选择前者。

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

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