Php MVC将值发送到表单

Php MVC将值发送到表单,php,forms,Php,Forms,我一直在制作一个网站来了解MVC,我正在尝试向更新选项添加一个函数。目前,我在每行上有两个按钮(一个删除,一个更新),当按下更新按钮时,屏幕右侧会出现一个更新行的表单 当用户按下“更新”按钮时,我试图预先填充表单,但我不知道如何在MVC中这样做。我应该创建一个这样做的函数,还是应该在表单顶部添加一些PHP来预填充 任何帮助或建议都会很好 更新要预填充的表单 <h2>Update Item!</h2> <h4>Fill in the form to up

我一直在制作一个网站来了解MVC,我正在尝试向更新选项添加一个函数。目前,我在每行上有两个按钮(一个删除,一个更新),当按下更新按钮时,屏幕右侧会出现一个更新行的表单

当用户按下“更新”按钮时,我试图预先填充表单,但我不知道如何在MVC中这样做。我应该创建一个这样做的函数,还是应该在表单顶部添加一些PHP来预填充

任何帮助或建议都会很好

更新要预填充的表单

<h2>Update Item!</h2>
    <h4>Fill in the form to update an entry.</h4>
    <form action="index.php" method="post">
        <fieldset>
            <input id='action' type='hidden' name='action' value='updateItem' />
            <p>
                <label for="fTitle">Title</label> <input type="text"
                    id="fTitle" name="fTitle" placeholder="title"
                    maxlength="25" required />
            </p>
            <p>
                <label for="fPrice">Price</label> <input type="text"
                    id="fPrice" name="fPrice" placeholder="price"
                    maxlength="25" required />
            </p>
            <p>
                <label for="fDescription">Description</label> <input type="text"
                    id="fDescription" name="fDescription" placeholder="description"
                    maxlength="500" required />
            </p>

            <p>
            <div class="form-group">
                <div class="controls">
                    <button type="submit" class="btn btn-success">Update</button>
                </div>
            </div>
            </p>
        </fieldset>
    </form>
更新项目!
填写表格以更新条目。

标题

价格

描述

更新

在接收按钮行ID的视图中更新按钮

            $update = file_get_contents( "templates/updateButton.php");
            $HTMLItemList = "";
            foreach ( $this->model->itemList as $row ) 
                    $HTMLItemList .= "<li><strong>" . $row ["title"] . ": </strong>" . "€" .$row ["price"] . "<blockquote>" . 
                        $row ["description"] .  " " . str_replace("value2replace", $row['id'], $delete) . 
                        str_replace("value2replace", $row['id'], $update) . "</blockquote></li>";
<form action="index.php" method="post">
        <fieldset>
            <input id='action' type='hidden' name='action' value='updateStart' />
            <div class="form-group">
                <div class="controls">
                    <input type="hidden" id="fId" name="fId" value="value2replace"> 
                    <input type="submit" class="btn btn-success" value="Update" />
                </div>
            </div>
        </fieldset>
    </form>
$update=file_get_contents(“templates/updateButton.php”);
$HTMLItemList=“”;
foreach($this->model->itemlistas$row)
$HTMLItemList.=“
  • ”$第[“标题”]行。“:”。“€”。$行[“价格”]。"" . $row[“说明”]。" " . str_replace(“value2replace”、$row['id']、$delete)。 str_replace(“value2replace”、$row['id']、$update)。“
  • ”;
    更新按钮

                $update = file_get_contents( "templates/updateButton.php");
                $HTMLItemList = "";
                foreach ( $this->model->itemList as $row ) 
                        $HTMLItemList .= "<li><strong>" . $row ["title"] . ": </strong>" . "€" .$row ["price"] . "<blockquote>" . 
                            $row ["description"] .  " " . str_replace("value2replace", $row['id'], $delete) . 
                            str_replace("value2replace", $row['id'], $update) . "</blockquote></li>";
    
    <form action="index.php" method="post">
            <fieldset>
                <input id='action' type='hidden' name='action' value='updateStart' />
                <div class="form-group">
                    <div class="controls">
                        <input type="hidden" id="fId" name="fId" value="value2replace"> 
                        <input type="submit" class="btn btn-success" value="Update" />
                    </div>
                </div>
            </fieldset>
        </form>
    

    这是一个我以前一直在努力解决的问题,但至今仍未找到令人满意的解决方案。我所看到的是,您的控制器将数据传递给包含表单的视图,然后使用PHP echo语句自动填充数据

    我还看到了一种反方法,其中每个表单都是一个对象,例如:

    class ItemForm{
        public function render(Item $item = null){
            //create form and fill in data if $item is not null
        }
    
    基本上,渲染函数将根据项目是否为null来决定是否为您填充信息


    就我个人而言,我更喜欢这个类版本,因为如果需要的话,你可以在不同的地方使用它

    感谢您的回复,我将研究添加您的代码,我也一直在研究
    file\u get\u contents
    函数,并尝试将数据库值发送到模板中,然后检索其内容,而不是以那种方式将file\u get\u内容用于表单。这是个很酷的主意。你会有一个base.html文件,一旦你得到字符串内容,你就会替换其中的值吗?是的,我想是的,所以我会有和
    update.html
    表单,然后我会使用文件“get\u contents”(“update.html+SomeDBValue”)来填充表单,这是一个有趣的可能性。我得亲自调查一下。很好的建议只是想知道您是否有任何使用文件内容来预填充表单的外观?我还没有机会调查此事