Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Editable form fields? Reply To: Editable form fields?

#171478
__
Participant

So, let’s start with your page, and break down what it does. It:

  • checks for form submissions and redirects them to avoid duplicate submissions
  • inserts new records
  • reads existing records
  • displays existing records in a table for editing
  • shows a form for creating new records.

We’ll write functions that do each of those things.

  • redirect_POST
  • client_insert
  • client_select
  • client_table
  • client_insert_form

Then, we’ll make one more function for templating. When you make an ajax request, HTML output will ruin the JSON response, so you don’t want your php page to have any output by default.

  • client_page_HTMLtemplate

With all the work being done inside of functions, your script becomes a “controller.” It’s much cleaner and easier to see what’s going on:

<?php
session_start();

// check for form submission & do session redirect if necessary
redirect_POST();

// prepare the database connection
$DB = new mysqli( 'host','username','password','dbname' );

// if a new record was submitted, insert it
if( $_POST ){ 
    client_insert( $DB,$_POST ); 
}

// select client records from db
$records = client_select( $DB );

// display records in a <table>
$table = client_table( $records );

// display table and new record <form> in html page template
print client_page_HTMLtemplate( $table.client_insert_form() );

// all done
exit;

Here’s a gist with everything.