Search code examples
formsphp-include

make php form-generated variable available in two php files


I have a form, Form_hour.php, that is loaded into the browser, and allows a user to select an hour. The form sends the variable $hour, via action="get", to the file View_A.php which uses $hour to query a database, and then loads View_A.php into the browser window, showing data for the selected hour.

In View_A, there is a button allowing the user to switch to View_B. The idea is that when the user clicks on View_B, the file View_B.php loads in the browser. View_B.php, like View_A.php, queries the database using that same $hour.

So what I want is for the same user-selected variable $hour to be available to both View_A.php AND View_B.php. i.e., if the user selects 6 a.m., then I want View_A and View_B to show data for 6 a.m.

I thought that having Form_hour.php send $hour to the file common.php, and then include common.php in View_A.php and View_B.php would work. But I keep getting the error "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 4327 bytes)" in View_A.php.

Does anyone have any ideas what I'm doing wrong?

Thanks in advance!


Solution

  • If I'm not mistaken you could just do it like I wrote below.
    This will direct the user to a difrent page, but will do what you want.


    form_hour.php

    <form action="View_A.php" method="post">
         your code
    </form>
    

    View_A.php

    <?php
    $_POST['hour'] = $hour
    // query database and do something with it!
    ?>
    
    <form action="View_B.php" method="post">
         <input type="hidden" name="hour" value=<?php print('"'.$hour.'"'); ?>>
         <input type="submit" ..... >
    </from>
    

    Like you asked, If you want the form as a var.

    <?php
    $_POST['hour'] = $hour
    // query database and do something with it!
    
    $form = '<form action="View_B.php" method="post">
         <input type="hidden" name="hour" value="'.$hour.'">
         <input type="submit" ..... >
    </from>';
    
    //to show it just print/echo it.
    print($from);
    ?>
    

    View_B.php

    <?php
    $_POST['hour'] = $hour
    // query database and do something with it!
    ?>