javascript - 将 javascript 变量传递给新的 php 页面

标签 javascript php

我基本上有一个用户可以输入的字符串并点击“发送”。然后这个字符串变量应该被传递到另一个打开的页面。我尝试过使用 session ,但它不起作用(第二页未加载)。这是我的代码:

首页。

<?php
    session_start();
    echo "<input type=\"text\" id=\"myText\">";
    echo "<button onclick=\"submit()\">Submit</button>";

    $_SESSION['userInput'] = $input;
?>
<script type="text/javascript">
    var submit = function() {
        input = document.getElementById("myText").value;
        window.open ('runSecond.php','_self',false);}   
</script>

第二页。

<?php
    session_start();
    $input = $_SESSION['userInput'];
    system('./myPythonScript.py ' $input);
?>

最佳答案

第 1 页

 <?php
        session_start();
        echo "<input type=\"text\" id=\"myText\">";
        echo "<button onclick=\"submit()\">Submit</button>";

        $_SESSION['userInput'] = $input;
    ?>
    <script type="text/javascript">
        var submit = function() {
            input = document.getElementById("myText").value;
            window.open ('runSecond.php?userInput=' + input,'_self',false);}   
    </script>

第 2 页

<?php
    session_start();
    $input = $_GET['userInput'];
    system('./myPythonScript.py ' $input);
?>

关于javascript - 将 javascript 变量传递给新的 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41772515/

相关文章:

javascript - 向 Sammy 注册点击事件

javascript - 动态地向 JSON 子数组添加值?

javascript - Angular 中嵌套订阅的错误

php - 从我的 pdo 调用创建正确的数据结构

Javascript:将值放入两个单独的表单字段中

javascript - HTML5 Canvas : I'm trying to load an image to the canvas

php - LIMIT 在页面中查看数据库中的所有数据

php - 从数据库中检索列表

php - System-V 消息队列 PHP C++

javascript - AngularJS ng-controller 在引导后不工作