-1

Here is code for the 1st php file:

    <html>
    <body>
        <form name="form" method="post">
              Size of array: <input type="number" name="size">
        <input type="submit" name="Send" button style="width:700px;height:250px" value="Show array"></span></p> 
        </form>
    </body>
</html>

<?php
  session_start();
    if(isset($_POST['Send']))
    {
            for ($i = 0; $i < $_POST['size']; $i ++) 
            {
                $random[] = rand(0, 10);
            }
            $_SESSION['random'] = $random;
            var_dump($_SESSION['random']);
            //sort($random);


         echo "<br>";
         echo "<br>";
         echo "Amount of array's elements: ";
         echo '<strong>'.count($random).'</strong>';    
         header('Location: 2nd_form.php');
         exit();
    }
?>

And here is code for the 2nd file:

<?php
// Start the session
session_start();
//session_start();
var_dump($_SESSION['random']);
?>

So, how to save array which is at the 1st form and code must save it for the 2nd file ? Pls help :-(

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
adamku
  • 3
  • 3
  • 1
    `session_start()` is missing from the first file –  Oct 24 '18 at 23:21
  • sorry ,yes..... – adamku Oct 24 '18 at 23:23
  • what do you mean by "save array" ? write to a file? –  Oct 24 '18 at 23:25
  • No, i mean, i can generate array, then i see it on screen, ok, but, when i click at the 2nd form, this array saves, and i can see it also at the 2nd form, but already without textbox'es and button....that's what i need – adamku Oct 24 '18 at 23:28
  • 2
    you're also outputting before header which is most likely why your code failed. Enable error reporting. – Funk Forty Niner Oct 24 '18 at 23:33
  • I really dont understand what you are asking for here. –  Oct 24 '18 at 23:34
  • Maybe any of u can fix my errors ? i really dont know what to do right now! :-( – adamku Oct 24 '18 at 23:36
  • _"fix my errors"_ - we don't know about any errors yet! – Jeff Oct 24 '18 at 23:37
  • _"but already without textbox'es and button"_ - where should textboxes appear and why? The second file does nothing but output `$_SESSION['random']` – Jeff Oct 24 '18 at 23:39
  • at the 2nd form i see `string(6) "random"` - instead of array, which is at the 1st form – adamku Oct 24 '18 at 23:39
  • at the 1st form i see this `array(6) { [0]=> int(9) [1]=> int(5) [2]=> int(10) [3]=> int(0) [4]=> int(10) [5]=> int(6) } ` right now and this array must saved into the 2nd form – adamku Oct 24 '18 at 23:41
  • @Jeff "where should textboxes appear and why" - there must not appear smth, there just need be showed array,which is generated at the 1st form – adamku Oct 24 '18 at 23:42
  • Put `session_start()` at the beginning of the first file. It has to be before any of the HTML that you show. – Barmar Oct 24 '18 at 23:55
  • See https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Barmar Oct 24 '18 at 23:55
  • @Barmar did not help – adamku Oct 25 '18 at 00:04

1 Answers1

-1

this will work with you

<?php
session_start();

if(isset($_POST['Send']))
{
    for ($i = 0; $i < $_POST['size']; $i ++) 
    {
        $random[] = rand(0, 10);
    }
    $_SESSION['random'] = $random;
    //var_dump($_SESSION['random']);
    //use die to stop script and do var_dump
    //die();
    //
    //dont print anything before header
    //
    header('Location: 2nd_form.php');
    exit();
}else{
    ?>
        <html>
    <body>
        <form name="form" method="post">
              Size of array: <input type="number" name="size">
        <input type="submit" name="Send" button style="width:700px;height:250px" value="Show array"></span></p> 
        </form>
    </body>
</html>
<?PHP
}
?>

2nd_form.php:

<?php
// Start the session
session_start();
//session_start();
var_dump($_SESSION['random']);
?>
Mohammed Ahmed
  • 431
  • 5
  • 11