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条)

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏