dreamwind1985 2015-08-06 09:52
浏览 60
已采纳

根据PHP类创建HTML表单

If I have PHP class for example like this:

class Person {    
    private $dateOfBirth;
    private $height;

    public function getDateOfBirth() {
        return $this->dateOfBirth;
    }

    public function setDateOfBirth($dateOfBirth) {
        $this->dateOfBirth = $dateOfBirth;
    }

    public function getHeight() {
        return $this->height;
    }

    public function setAge($height) {
        $this->height= $height;
    }
}

Is there some nice solution how to dynamically generate HTML form according to this class?

<form action="action.php" method="post">
    <input type="date" name="DateOfBirth" value="1900-01-01"/>
    <input type="number" name="Height" value="180"/>
    <input type="submit" class="submit" value="Submit"/>
</form>

The only solution that I came up with is to iterate all properties of class and generate inputs. And if the name of the property contains e.g. 'date' make 'input type="date"'. But I don't really like this solution. Given that property Height is obviously number type, its name doesn't indicate it. And I don't want to rename it. Property names must remain the same.

Thanks for responses

  • 写回答

3条回答 默认 最新

  • dongyupan4850 2015-08-07 08:08
    关注

    Maybe the most precise solution will be to create DB table with details for every class and generate form according to this table.

    form_fields http://www.cloud.vnovak.cz/form_fields.png

    Compound key on class and order columns.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?