Arek
Arek

Reputation: 2021

php sending form data between files

I am new to php and form development and here's what I am trying to achieve:

Firstly i have a simple form to input just two text values:

Form1
<br>
<form action="gather.php" method="post">
    Catalog:
    <input type="text" name="folderName" maxlength="50">
    <br>
    File Name:
    <input type="text" name="fileName" maxlength="50">
    <br>
    <input type="submit" name="formSubmit" value="Submit">
</form>

And now I have the second file called gather.php where i get theese two lines and use them to count files inside catalog etc.

<?php
if(isset($_POST['formSubmit'])){
    $folderName = $_POST['folderName'];
    $fileName = $_POST['fileName'];
    $numberOfImages = count(glob($folderName . "/*.jpg"));
    for($i = 1; $i <= $numberOfImages; $i++){
        echo "<br><input type=\"text\" name=\"imie" . $i . "\"><br/>\n";
        echo "<img src=\"" . $folderName . "/0" . $i . ".jpg\" height=\"50px\" width=\"50px\"><br><br>\n";
    }


    echo "\n<br>" . $folderName . "<br>" . $fileName . "\n";
}

?>
<br>
Final form
<br>
<form action="build.php" method="post">
<input type="submit" name="finalSubmit" value="Submit">
</form>

And this should get me to build.php file which looks more less like this:

<?php
if(isset($_POST['finalSubmit'])){
    //loop and other stuff
    $temp = $_POST['imie1'];
    echo $temp;

}
?>

So the thing is that in this final file I'd like to get all the data that was put into text fields in the gather.php file. But I get the undefined index error on build.php saying there's nothing in the $_POST['imie1']. Can you tell me why is that? Is tehre a way to get this data from second file to the third file?

Edit: thx for the answers, as I can accept only 1 and multiple are the same I choose the user with least rep just to support her :)

Upvotes: 0

Views: 182

Answers (3)

Ana Mendoza
Ana Mendoza

Reputation: 351

You need to add the input inside the form tag, it won't be sent otherwise.

    <br>
    Final form
    <br>
    <form action="build.php" method="post">
    <?php
    if(isset($_POST['formSubmit'])){
        $folderName = $_POST['folderName'];
        $fileName = $_POST['fileName'];
        $numberOfImages = count(glob($folderName . "/*.jpg"));
        for($i = 1; $i <= $numberOfImages; $i++){
            echo "<br><input type=\"text\" name=\"imie" . $i . "\"><br/>\n";
            echo "<img src=\"" . $folderName . "/0" . $i . ".jpg\" height=\"50px\" width=\"50px\"><br><br>\n";
        }


        echo "\n<br>" . $folderName . "<br>" . $fileName . "\n";
    }

    ?>
    <input type="submit" name="finalSubmit" value="Submit">
    </form>

Upvotes: 2

andrewsi
andrewsi

Reputation: 10732

I think the <form> on the second form needs to come at the top of the file - it'll only submit elements inside the tag, so because you're generating your HTML and then opening the form, it's not being submitted.

<br> 
Final form 
<br> 
<form action="build.php" method="post"> 
<?php 
if(isset($_POST['formSubmit'])){ 
    $folderName = $_POST['folderName']; 
    $fileName = $_POST['fileName']; 
    $numberOfImages = count(glob($folderName . "/*.jpg")); 
    for($i = 1; $i <= $numberOfImages; $i++){ 
        echo "<br><input type=\"text\" name=\"imie" . $i . "\"><br/>\n"; 
        echo "<img src=\"" . $folderName . "/0" . $i . ".jpg\" height=\"50px\" width=\"50px\"><br><br>\n"; 
    } 


    echo "\n<br>" . $folderName . "<br>" . $fileName . "\n"; 
} 

?> 
<input type="submit" name="finalSubmit" value="Submit"> 
</form> 

Upvotes: 1

EaterOfCode
EaterOfCode

Reputation: 2222

Replace your gather.php with

<br>
Final form
<br>
<form action="build.php" method="post">
<?php
    if(isset($_POST['formSubmit'])){
        $folderName = $_POST['folderName'];
        $fileName = $_POST['fileName'];
        $numberOfImages = count(glob($folderName . "/*.jpg"));
        for($i = 1; $i <= $numberOfImages; $i++){
            echo "<br><input type=\"text\" name=\"imie" . $i . "\"><br/>\n";
            echo "<img src=\"" . $folderName . "/0" . $i . ".jpg\" height=\"50px\" width=\"50px\"><br><br>\n";
        }


        echo "\n<br>" . $folderName . "<br>" . $fileName . "\n";
    }

    ?>
<input type="submit" name="finalSubmit" value="Submit">
</form>

you was echo'ing the input boxes outside the form so now it will work

Upvotes: 1

Related Questions