user1418018
user1418018

Reputation: 189

Passing javascript variable to new php page

I've basically have a string a user can input and hit "Send". Then this string variable should be passed to another page that opens. I've tried using session, but it doesn't work (the second page doesn't load). Here's my code:

First page.

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

Second page.

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

Upvotes: 0

Views: 30

Answers (1)

bassxzero
bassxzero

Reputation: 5041

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

Page 2

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

Upvotes: 1

Related Questions