0

我是 php 和表单开发的新手,这就是我想要实现的目标:

首先,我有一个简单的表单来输入两个文本值:

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>

现在我有了第二个名为gather.php的文件,我在其中得到了这两行并使用它们来计算目录等中的文件。

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

这应该让我看到 build.php 文件看起来不像这样:

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

}
?>

所以问题是,在这个最终文件中,我想获取所有放入gather.php 文件中文本字段的数据。但是我在 build.php 上收到未定义的索引错误,说 $_POST['imie1'] 中没有任何内容。你能告诉我这是为什么吗?有没有办法将这些数据从第二个文件获取到第三个文件?

编辑:谢谢答案,因为我只能接受 1 并且多个相同我选择代表最少的用户只是为了支持她:)

4

3 回答 3

2

您需要在表单标签内添加输入,否则不会发送。

    <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>
于 2012-08-06T12:06:19.883 回答
1

将您的gather.php替换为

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

你在表单外回显输入框,所以现在它可以工作了

于 2012-08-06T12:06:36.513 回答
1

我认为<form>第二个表单上的 需要位于文件的顶部 - 它只会提交标签内的元素,所以因为您正在生成 HTML 然后打开表单,所以它不会被提交。

<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> 
于 2012-08-06T12:08:08.027 回答