简体   繁体   中英

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.

How could I get the vars 'marca' and 'sucursal' from the javascript function in 2 variables in the php section?

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

You can either use AJAX to return the calculation from the client once ran or just perform said calculations in php beforehand. Personally, I'd go for the former.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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