PHP使用File_get_Contents()预填充表单 [英] PHP Using File_get_Contents() to pre populate a form

查看:68
本文介绍了PHP使用File_get_Contents()预填充表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个网站上帮助学习一些PHP MVC.目前该站点执行CURD命令,但我希望对其进行改进.

I am working on a site to help learn some PHP MVC. At the moment the site carries out the CURD commands but I am looking to improve on them.

数据库的项目在列上显示有两个按钮,一个按钮删除,一个按钮更新.当用户单击更新按钮时,将显示一个表格(update_item_form.php),该表格将允许用户输入要更新的项目信息.表单包含三个字段(标题,价格和说明)

The items of the database are displayed in on column with two buttons, one to delete and one to update. When the user clicks the update button a form (update_item_form.php) is displayed that will allow the user enter the item information to be update. The form consists of three fields (title, price and description)

我要执行的操作:当用户单击更新"按钮时,将在该表单中预先填充与该行相关的所有项目信息.

What I am trying to do: When the user clicks the update button the form will be pre populated with all the item information connected with that row.

当用户单击按钮时,如何将行信息发送到表单?

How will can I send the row information to the form when the user clicks the button?

file_get_contents()

$rightBox = file_get_contents( "templates/update_item_form.php" [database value] );

Update_item_form.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">Submit</button>
        </div>
    </div>
    </p>
</fieldset>

推荐答案

file_get_contents不会解析PHP文件.您要做的只是将 code 加载到$rightBox中,而不是我认为是的 output 中.

file_get_contents won't parse a PHP file. All you're doing is loading the code into $rightBox, not the output that I assume you are after.

为此,您可以使用输出缓冲.

For that, you can use output buffering.

ob_start();
include "templates/update_item_form.php";
$rightBox = ob_get_clean();

这会将ob_start()ob_get_clean()之间的输出存储到$rightBox.

This will store output between ob_start() and the ob_get_clean() into $rightBox.

这篇关于PHP使用File_get_Contents()预填充表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆