Forum Moderators: coopster

Message Too Old, No Replies

Using a common add/edit page

Contact management system

         

Snappa

9:25 pm on May 22, 2011 (gmt 0)

10+ Year Member



My contact management script uses 'Add' and 'Edit' pages which have an identical core display layout, with differences to the header and available menu items. Obviously, each page also has different data selection, display and saving (update/insert) actions.

I want to combine these two pages into one, to have a single page with different functions which are called according to whether an 'Add' or 'Edit' button was clicked on the calling page.

How can I do this ? I have looked here and elsewhere, but can't find what I'm looking for.

Thanks.

rainborick

4:22 pm on May 23, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would rethink your approach a bit. It's always handy for programmers when they can consolidate functions into a single page, but having a form either add or edit a record depending on which button a user pushes seems like it could often confuse the user or make it too easy for them to accidentally push the wrong button and cause more problems than it's worth.

You might try having a separate page and form for adding and editing, and then use an include() to create the <input> elements based on a template file. Or, since it's likely that the user arrives at this page from a link that specifies "add" or "edit", you could include a command parameter in those links that would tell the script which function is being requested by the user, and then display the proper form and ultimately perform the appropriate action.

jspeed

4:45 pm on May 23, 2011 (gmt 0)

10+ Year Member



If I understand what you are going for, it is simple to achieve. You basically just want all the code in one php page?

You could just query the page off the link, for example:

admin.php?action=add

and have a simple conditional statement with desired code and form:

$action = $_GET['action'];

if ($action == "add") {
// code and form
}

elseif ($action == "edit") {
// etc etc
}

May not be the most graceful, but works.

rocknbil

4:47 pm on May 23, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Snappa!

Something like this, I do this all the time. This is actually a good first step to recycling and re-using code, so you don't have long redundant blocks or script after script using the same stupid code over and over again across many files. :-)

First you define, somewhere, a list of fields used in the form. This is so you can more or less "black box" the function and not rely on globals. It will be the only required parameter for the function. I'll explain why that's important in a bit . . .

$edit_user_fields = Array(
'fname' => 'First Name',
'lname' => 'Last Name',
'email' => 'Email Address'
// etc.
);

Then you have a function


function edit_user_form($formfields,$state=null, $uid=null) {
if (($state and is_numeric($uid) and ($uid > 0)) {
$submitlabel='Update User';
$formaction='1';
// or the URL for your add script, if you don't combine them like
//you do with this your form - which you should. :-)
// do some stuff here to look up the user $uid
// populate $_POST with these variables
}
else {
$submitlabel = 'Add User';
$formaction='0';
}
foreach ($formfields as $key => $value) {
$_POST[$formfield] = isset($_POST[$formfield])?$_POST[$formfield]:null;
}
// build your form
return $form;
}


(<aside> you could probably eliminate $state altogether and only act on edit if there is a $uid is present and found. Error checking is your friend.)

This function accepts the form field names array and optionally the form state and uid (if it's edit.) You compose the form itself into the scalar $form, and return it, like

if ($my_uid) {
echo edit_user_form($edit_user_fields,'1',$my_uid);
}
// Otherwise add
else { echo edit_user_form($edit_user_fields); }

Why pass the fields to the function? One of the basic concepts of user input security is accept only what you want, throw everything else away. It may be a little tedious, but by controlling the list of fields your form will accept you eliminate a large portion of potential attacks, and can focus your cleansing on those fields (which of course would all be cleansed and validated before hitting this form.)

This allows us to do the little foreach before outputting the form, using the (already cleansed!) $_POST values - if they have not been set, we set them now, to null, so you can do things like

$form .= "<label for=\"email\">" . $formfields['email'] . "</label> <input type=\"text\" name=\"email\" id=\"email\" value=\"" . $_POST['email'] . "\">";

You can now see two advantages of the external list, if you want to change the form labels, you just do it in the centrally located array - not go digging through your script looking for the form. Values populate automatically to re-populate under error conditions.

In practical application, you'd want to add another parameter to pass errors to your function if they exist:

function add_user() {
// return NULL or a string containing the errors
$errors = check_user_input();
if ($errors) {
echo edit_user_form($edit_user_fields,null,null,$errors);
exit;
}
// Normal updates here
}

... this further isolates your form from dependency on other parts of your program.

This is not really a full optimized way of working with forms as it still involves a bit of hard coding in the function, but it's a little better and will allow you to determine the form state externally, using one form for add or edit.

Once you go down this path I swear, you will start combining functions everywhere and your speed will increase tenfold. You won't have an add_user and edit_user function you'll have one update function that adjusts based on the state. You'll compile select statements based on that state. Error checking will adjust based on the state as well. Overall you'll code a lot less, your programs will be easier to debug, and here comes the really cool part - as you build programs for new clients, you really WILL be able to go through a few scripts and copy and paste and it will all just work, because your functions are independent.

Snappa

8:50 pm on May 23, 2011 (gmt 0)

10+ Year Member



Wow ! Thanks guys.

jspeed; that is exactly what I was looking for.

rocknbil; your reply goes beyond what I was looking for, but has really got me thinking. I'll set up a trial system using your suggestions, and see how it goes.

Thanks again.

agent_x

9:33 pm on May 23, 2011 (gmt 0)

10+ Year Member



Another simple way to do this is to take all the code that is common to the two pages and put it in an include file and then just include it in both.