Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文本框读入数组php_Php_Html_List_Post - Fatal编程技术网

从文本框读入数组php

从文本框读入数组php,php,html,list,post,Php,Html,List,Post,file index.php <html> <head> <title>Guset Book</title> </head> <body> <h3>Guest book</h3> <a href="/addNew.php"> <p><input type="button" value="Add in book" ><

file index.php

<html>  
<head>
    <title>Guset Book</title>
</head>

<body>
    <h3>Guest book</h3>
    <a href="/addNew.php">
        <p><input type="button" value="Add in book" ></p>
    </a>
    <a href="/readAll.php">
        <p><input type="button" value="Read all"></p>
    </a>
</body>
文件addNew.php

<html>  
<head>
    <title>Guset Book</title>
</head>

<body>
    <h3>New</h3>

    <form name='formAddNew' method='post' action="ControllerAdd.php">
        <p>Author: <input type="text" name="nameAuthor"></p>
        <p>Comment:</p>
        <p><textarea rows="5" cols="40" name="commentAuthor" style="resize: none;"></textarea></p>  
        <p><input type="submit" name="submitAuthor" value="Submit"></p>
    </form>
</body>
文件Model.php

<?php
class GuestBook
{
    private $author;
    private $comment;

    function __construct($author, $commment)
    {
        $this->author = $author;
        $this->comment = $commment;
    }

    public function getAuthor()
    {
        return $this->author;
    }

    public function getComment()
    {
        return $this->comment;
    }
}
$guestBookList = new ArrayObject();
$guestBookList[] = new GuestBook("Author", "Comment");

function addInList($author, $comment)
{
    $guestBookList[] = new GuestBook($author, $comment);
}
?>

文件ControllerAdd.php

<html>
<head>
    <title>Add</title>
</head>

<body>
    <?php
        require_once "Model.php";
        addInList($_POST["nameAuthor"], $_POST["commentAuthor"]);
    ?>
    <h3>Succes</h3>
    <a href="/"><input type="button" value="On main"></a>
</body>
文件readAll.php

<html>
<head>
    <title></title>

</head>

<body>
    <?php 
        require_once "Model.php"; 

        foreach($guestBookList as $value)
        {
            echo("<br>-----------<br>");
            echo($value->getAuthor());
            echo("<br>");
            echo($value->getComment());
        }
    ?>
</body>
问题是编译器不会抛出错误,但不会将代码从文本框写入数组。它以正确的方式从文本框中读取信息,但不要写入数组Plz帮助。

会话是在这种情况下传递变量的最佳方式

若我理解正确,那个么你们需要从两个页面传递文本字段值,并在数组的第三个页面中使用它们

以此为参考

index.php

<?php
session_start();
if(isset($_POST['submit'])){
    if(!empty($_POST['tex1'])){
        $_SESSION['tex1'] = $_POST['tex1'];
        header('location:form.php');        
    }   
}
?>
<form method="POST">
<input type="text" name="tex1">
<input type="submit" name="submit" value="submit">
</form>

form.php
<?php
session_start();
if(isset($_POST['submit'])){
    if(!empty($_POST['tex2'])){
        $_SESSION['tex2'] = $_POST['tex2'];
        header('location:final_page.php');      
    }

}
?>
<form method="POST">
<input type="text" name="tex2">
<input type="submit" name="submit" value="submit">
</form>

final_page.php

<?php
session_start();
print_r($_SESSION);
$\会话是php的全局变量。 阅读有关会话的更多信息。请阅读

file1.php

<form action="file2.php" method="POST">
<textarea rows="4" cols="50" name="data[]"> </textarea>
<input type="submit" value="Submit">
</form>
file2.php

<?php
    session_start();
    $formData = $_POST['data'];
    //echo '<pre>'; print_r($formData); die;
    $_SESSION['formData'] = $formData;
    echo 'Open File 3 to check submitted data.'
?>
file3.php

<?php
class GuestBook {
    private $source;
    private $book;

    function __construct($filename) {
        $this->book = array();
        $this->source = $filename;
        $this->restore();
    }

    function getBook() {
        return $this->book;
    }

    function restore() {
        if (file_exists($this->source)) {
            $records = file($this->source);
            if (is_array($records)) {
                while (count($records)) {
                    $line = trim(array_shift($records));
                    list($author, $comment) = explode(':splitter:',$line);
                    $this->book[] = new GuestBookRecord($author, $comment);
                }
            }
        }
    }

    function save() {
        $fd = fopen($this->source, 'w');
        foreach ($this->book as $record) {
            fwrite($fd, $record->getAuthor().':splitter:'.$record->getComment().PHP_EOL);
        }
        fclose($fd);
    }

    function addComment($author, $comment) {
        $this->book[] = new GuestBookRecord($author, $comment);
        $this->save();
    }
}

class GuestBookRecord {
    private $author;
    private $comment;

    function __construct($author, $commment) {
        $this->author = $author;
        $this->comment = $commment;
    }

    public function getAuthor() {
        return $this->author;
    }

    public function getComment() {
        return $this->comment;
    }
}

$guestBook = new GuestBook('sample.txt');

// compatibility with OP source
$guestBookList = $guestBook->getBook();

// compatibility with OP source
function addInList($author, $comment) {
    global $guestBook;
    $guestBook->addComment($author, $comment);
}

我建议在您的特殊情况下,您需要更改模型的行为,如下所示:

这只是一个例子,不能照原样使用

我想,这段代码不会使源代码崩溃


但是不太好。这里至少有两个问题,第一个是代码将所有记录读入内存,第二个是并发访问。这只是一个例子。

这里没有你自己的代码,我们可以看到你的错误或建议。你的代码没有从外部存储(如数据库或文本文件)检索/存储你的数据,将此类数据保存到Cookie/session在我看来毫无用处。每次访问Model.php时,$guestBookList都会创建为对象的新实例,没有数据,并添加一条带有“author and comment”的记录。我想您总是只能看到这个记录。这很好,但是如果我返回file1.php,$\u会话不会保存值。如何在我更改页面时使其保存值?删除会话\u销毁;从file3.php开始,现在将保存您的值,直到您使用file1.php再次提交新数据
<?php
class GuestBook {
    private $source;
    private $book;

    function __construct($filename) {
        $this->book = array();
        $this->source = $filename;
        $this->restore();
    }

    function getBook() {
        return $this->book;
    }

    function restore() {
        if (file_exists($this->source)) {
            $records = file($this->source);
            if (is_array($records)) {
                while (count($records)) {
                    $line = trim(array_shift($records));
                    list($author, $comment) = explode(':splitter:',$line);
                    $this->book[] = new GuestBookRecord($author, $comment);
                }
            }
        }
    }

    function save() {
        $fd = fopen($this->source, 'w');
        foreach ($this->book as $record) {
            fwrite($fd, $record->getAuthor().':splitter:'.$record->getComment().PHP_EOL);
        }
        fclose($fd);
    }

    function addComment($author, $comment) {
        $this->book[] = new GuestBookRecord($author, $comment);
        $this->save();
    }
}

class GuestBookRecord {
    private $author;
    private $comment;

    function __construct($author, $commment) {
        $this->author = $author;
        $this->comment = $commment;
    }

    public function getAuthor() {
        return $this->author;
    }

    public function getComment() {
        return $this->comment;
    }
}

$guestBook = new GuestBook('sample.txt');

// compatibility with OP source
$guestBookList = $guestBook->getBook();

// compatibility with OP source
function addInList($author, $comment) {
    global $guestBook;
    $guestBook->addComment($author, $comment);
}