MVC将值发送到表单

时间:2015-04-15 13:11:11

标签: php forms

我一直在创建一个网站来了解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>

1 个答案:

答案 0 :(得分:1)

这是我之前一直在努力解决的问题,但仍未找到令人满意的解决方案。我如何看待它完成的是您的Controller将数据传递给包含您的表单的视图,然后您使用PHP echo语句自动填充数据。

我还看到了一种计数器方法,其中每个表单都是一个Object,例如:

class ItemForm{
    public function render(Item $item = null){
        //create form and fill in data if $item is not null
    }

基本上,渲染函数将决定是否为您填写信息,具体取决于它是否为空。

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

相关问题