OOP PHP,抽象类,无条件 - 如何根据表单中选择的选项调用方法?

问题描述 投票:0回答:1

我的任务是根据用户选择的选项创建动态表单。限制是我必须使用抽象主类来实现多态性。这也意味着没有条件(if/else、switch/case)。不允许使用框架。我一直无法弄清楚如何调用该方法并将 html 插入到dynamicForm div 中。我对 php 和一般编程非常陌生,正在寻找前进的方向。我不想接受人工智能的建议,因为我已经收到了不好的反馈,但没有表明我到底应该做什么。到目前为止我使用的所有视频材料显然都没有考虑到这些限制。如果我能让这个表单变得动态,我就可以继续启动 SQL 连接来存储提交的表单。但目前我只需要表单动态部分的帮助。

这是我的index.php代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Product add</title>
</head>
<body>
    <header>
        <h1>Product Add</h1>
        <button type="submit" form="product_form">Save</button>
        <button>Cancel</button>
    </header>
    <hr>
    <form action="post" id="product_form">
        <div>
            <label for="sku">SKU</label>
            <input type="text" name="sku" id="sku"><br><br>
            <label for="name">Name</label>
            <input type="text" name="name" id="name"><br><br>
            <label for="price">Price ($)</label>
            <input type="number" name="price" id="price"><br><br>
            <label for="productType">Type Switcher</label>
            <select name="productType" id="productType">
                <option value="empty"></option>
                <option value="DVD">DVD</option>
                <option value="Book">Book</option>
                <option value="Furniture">Furniture</option>
            </select><br><br>
        </div>
        <div id="dynamicForm"></div>
    </form>
    <hr>
    <footer>
    </footer>
</body>
</html>

这是我的classes.php 文件:

<?php

abstract class Products {
    abstract public function setAttributes();
}

class DVD extends Products {
    public function setAttributes()
    {
        return '
            <div id="DVD">
                <label for="size">Size (MB)</label>
                <input type="number" name="size" id="size">
                <p>*Please provide size*</p>
            </div>
        ';
    }
}

class Book extends Products {
    public function setAttributes()
    {
        return '
            <div id="Book">
                <label for="weight">Weight (KG)</label>
                <input type="number" name="weight" id="weight">
                <p>*Please provide weight*</p>
            </div>
        ';
    }
}

class Furniture extends Products {
    public function setAttributes()
    {
        return '
        <div id="Furniture">
                <label for="height">Height (CM)</label>
                <input type="number" name="height" id="height"><br><br>
                <label for="width">Width (CM)</label>
                <input type="number" name="width" id="width"><br><br>
                <label for="length">Length (CM)</label>
                <input type="number" name="length" id="length">
                <p>*Please provide dimensions*</p>
        </div>
        ';
    }
}
php oop polymorphism abstract-class
1个回答
0
投票
Here is my index.php code:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Product add</title>
</head>
<body>
    <header>
        <h1>Product Add</h1>
        <button type="submit" form="product_form">Save</button>
        <button>Cancel</button>
    </header>
    <hr>
    <form action="post.php" id="product_form">
        <div>
            <label for="sku">SKU</label>
            <input type="text" name="sku" id="sku"><br><br>
            <label for="name">Name</label>
            <input type="text" name="name" id="name"><br><br>
            <label for="price">Price ($)</label>
            <input type="number" name="price" id="price"><br><br>
            <label for="productType">Type Switcher</label>
            <select name="productType" id="productType">
                <option value="empty"></option>
                <option value="DVD">DVD</option>
                <option value="Book">Book</option>
                <option value="Furniture">Furniture</option>
            </select><br><br>
        </div>
        <div id="dynamicForm"></div>
    </form>
    <hr>
    <footer>
    </footer>
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.