Need help with form-driven inquiry/update page

The place for general PHP questions and hints for PHP on IBM i

Need help with form-driven inquiry/update page

Postby patr9257 on Thu Aug 20, 2015 7:23 pm

I am starting my first PHP project at my company. We are running Zend Server on an IBM i (AS/400).

I want to create a form-driven webpage that is for internal use only in our company. I want to create a form where the user will enter an item number in a text box for inquiry. When the user submits the form, it will display item information from a DB in more textboxes. The user will have the option of making changes in the textboxes and when they submit the form again, the changes will be updated back to the DB.

What would be a good approach? Should I have the HTML form in one PHP file and have the PHP code for inquiry/update in another file(s) and use requires()? If I take this approach, I would probably need to do action="<? php echo $_SERVER['PHP_SELF']; ?." on the form. Is that correct?

Or, is there a way to do action="inquire.php" and/or action="update.php" on the "main" form? In any case, the user would need the abilty "go back" to the "original screen" to inquire on a different item number at any time.

I hope this description makes sense. I'm just looking for the best approach without getting involved with MVC at this point. Any recommendations? Thanks.
Posts: 7
Joined: Wed Jun 24, 2015 4:31 pm

Re: Need help with form-driven inquiry/update page

Postby mark397 on Thu Aug 20, 2015 8:17 pm

Since this is a new project I would lean towards using MVC so the code is more reusable, but I also see how this could be daunting to try to learn as you try to start writing it. I can give a basic layout of classes and functions that might help you get started:

DB class - all SQL routes through here so data is handled uniformly at the most generic level. It could be structured like so:
Code: Select all
class DB
    public $sql;
    public $parms;
    private $connection;

    public function __construct($dsn, $user, $password) {
        $this->connection = db2_connect($dsn, $user, $password);

    public function exePrep() {

    public function fetchRow() {

    public function fetchAllRows() {

You can then have a class for items, something like:
Code: Select all
class Item
    public $itemNumber;

    public function __construct($itemNumber) {
        $this->itemNumber = $itemNumber;
        // make some sql to gather item details

    // create functions/variables to set and retrieve data from

Now the view is going to be the HTML. I find usability is much easier when using AJAX, which is much more simple than people think. You could use the JQuery $.ajax() to post to an AJAX script. You'll define actions in a main AJAX handler and those actions will route the function calls to the appropriate classes and functions. I typically use
Code: Select all
switch ($_POST['action'])
where action is defined by the javascript form handling.. Basically you'll route everything to the Item class, as the DB class is not to be touched by controllers. The javascript could populate the form. Using Javascript's console.log() you can easily debug data issues in the web browser's console.
Posts: 87
Joined: Thu May 30, 2013 6:12 pm

Return to PHP Questions / Hints

Who is online

Users browsing this forum: No registered users and 2 guests