Contact form to manage section

3 posts by 3 authors in: Forums > CMS Builder
Last Post: June 9, 2014   (RSS)

By esupport - June 2, 2014

Hi,

I would like to build a contact form section.

If user fill the form.

We can get the form info on admin panel.

Is there any form feature for user to fill and for admin to manage?

Thanks!

Jac

-Jax H.

By gkornbluth - June 9, 2014 - edited: June 9, 2014

Hi Jac,

You  could build on (or remove code from) the form structure in this recipe from my CMSB Cookbook http://www.thecmsbcookbook.com

There are other applicable recipes in the cookbook as well, including some on incorporating CAPTCHA code on the submission for to limit the amount of robot submissions you get.

If you're not using membership plugin, I wouldn't recommend implementing an updating process, too much chance of dangerous code being inserted into the database.

Hope you find something you can use.

Best,

Jerry Kornbluth
Head Chef

POPULATING A FORM FROM A MULTI RECORD DATABASE, UPDATING RECORDS, CREATING NEW RECORDS - Jul 21st, 2013

To pre-populate forms so that information about a specific record can be viewed and updated if desired, you’ll need to choose a field value that can uniquely identify that record. It can be an email address, the record number, or any other easily input unique identifier.

This recipe uses the email address field of a Publicity Contacts database.

It also uses two forms.

A short, email address submission form, and a longer form that can display all the desired information in your record and either updated or inserted into a new record.

For security purposes, access to this page should be restricted to appropriate individuals, but that’s covered elsewhere in the cookbook.

First you’ll need to cycle through all of the existing records in your table and compare the submitted email address with those found.

If a match is found, the main form needs to be populated with the existing information from the matching record, and a procedure for updating that information begun.

If there is no match, and a new record is to be created, appropriate error checking for required fields needs to take place before the record is created.

At each step, there need to be appropriate error messages to guide the user.

Here’s one approach to accomplishing this task:

At the top of the viewer, just after your normal list records calls, there are some blocks of code.

The code to pre-populate the form fields if a match is found after an email address is submitted.

_____ code ________________________________________________
<?php if (@$_REQUEST['save2'] ) :?>
<?php

$email_address = $_REQUEST['email_address'] ;
list($publicity_listingsRecords, $publicity_listingsMetaData) = getRecords(array(
'tableName' => publicity_listings',
'allowSearch' => false,
'where' => "email_address = '$email_address'",
));
?>
<?php foreach ($publicity_listingsRecords as $record):?>
<?php foreach ($record as $name => $value):?>
<?php if (array_key_exists($name, $_REQUEST)) { continue; }
$_REQUEST[$name] = $value;

?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif ?>

The code to set a counter ($count) if a matching email addresses is found,

<?php $count= 0 ?>
<?php // check for matching email
$email = mysql_escape(@$_REQUEST['email_address']);
$count = mysql_count(publicity_listings', "email_address = '$email'");
?>
__________________________________________________________
Based on that count, the code to determine if the next process will be to update an existing record or insert a new record
_____ code ________________________________________________
<?php // Set a $show update variable to change the form submit name to “Save” if no duplicate emails are found?>
<?php if ($count == 1):?>
<?php $showupdate = 1 ?>
<?php else:?>
<?php $showupdate = 0 ?>
<?php endif ?>
__________________________________________________________
the code to check for errors if a new record is to be created,

_____ code ________________________________________________
<?php

// process form
if (@$_REQUEST['save']) {

// error checking
$errorsAndAlerts = "";
if (!@$_REQUEST['first_name']) { $errorsAndAlerts .= "Please enter a first_name<br />\n"; }
if (!@$_REQUEST['last_name']) { $errorsAndAlerts .= "Please enter a last_name<br />\n"; }
// if (!@$_REQUEST['media_type']) { $errorsAndAlerts .= "Please select a media type<br />\n"; }
if (!@$_REQUEST['specialty_group']) { $errorsAndAlerts .= "Please select a specialty group<br />\n"; }
if (!@$_REQUEST['source']) { $errorsAndAlerts .= "Please select a source<br />\n"; }
if (!@$_REQUEST['email_address']) { $errorsAndAlerts .= "Please enter an email address<br />\n"; }
elseif(!isValidEmail(@$_REQUEST['email_address'])) { $errorsAndAlerts .= "Please enter a valid email address(example: user@example.com)<br />\n"; }



// turn off strict mysql error checking for: STRICT_ALL_TABLES
mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)

?>
__________________________________________________________
The code to insert a new record into the database.
_____ code ________________________________________________
<?php
if ($count == 0 &&!$errorsAndAlerts ) {

mysql_query("INSERT INTO `{$TABLE_PREFIX}publicity_listings` SET

organization = '".mysql_escape( $_REQUEST['organization'] )."',
department = '".mysql_escape( $_REQUEST['department'] )."',
first_name = '".mysql_escape( $_REQUEST['first_name'] )."',
last_name = '".mysql_escape( $_REQUEST['last_name'] )."',
title = '".mysql_escape( $_REQUEST['title'] )."',
email_address = '".mysql_escape( $_REQUEST['email_address'] )."',
street_address = '".mysql_escape( $_REQUEST['street_address'] )."',
city = '".mysql_escape( $_REQUEST['city'] )."',
state = '".mysql_escape( $_REQUEST['state'] )."',
zip = '".mysql_escape( $_REQUEST['zip'] )."',
phone = '".mysql_escape( $_REQUEST['phone'] )."',
cell = '".mysql_escape( $_REQUEST['cell'] )."',
description = '".mysql_escape( $_REQUEST['description'] )."',
createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'")
or die("MySQL Error Creating Record:<br />\n". htmlspecialchars(mysql_error()) . "\n");
$userNum = mysql_insert_id();


$first_name = $_REQUEST['first_name'];
$last_name = $_REQUEST['last_name'] ;
$_REQUEST = array(); // clear form values
$errorsAndAlerts = "A record for $first_name $last_name has been successfully created.<br />You can now enter another email address.";
}
}
?>
__________________________________________________________
Some additional error checking code,
_____ code ________________________________________________
<?php
if ( @$count > 0 && @$_REQUEST['save2'] && @!$_REQUEST['go']) {
$errorsAndAlerts = "The email address $email already exists in the Publicity database.<br />Update the information below as required, then re-submit the form.<br /><br />Remember, before you can update a contact's information, you must check the check box above the 'Submit' button.";

}

if ( @$count == 0 && @$_REQUEST['save2'] ) {

$email = @$_REQUEST['email_address'];

$errorsAndAlerts = "The email $email is not currently in the Publicity database.<br /> Please fill out the form below to create a new record for this contact.";
}
?>
__________________________________________________________
And, the code to update an existing record,
_____ code ________________________________________________
<?php if (@$count > 0 && @$_REQUEST['go'] && @$_REQUEST['save2'] ): ?>
<?php

$query = "UPDATE `{$TABLE_PREFIX}publicity_listings` SET
organization = '".mysql_escape( @$_REQUEST['organization'] )."',
department = '".mysql_escape( @$_REQUEST['department'] )."',
first_name = '".mysql_escape( @$_REQUEST['first_name'] )."',
last_name = '".mysql_escape( @$_REQUEST['last_name'] )."',
title = '".mysql_escape( @$_REQUEST['title'] )."',
email_address = '".mysql_escape( @$_REQUEST['email_address'] )."',
street_address = '".mysql_escape( @$_REQUEST['street_address'] )."',
city = '".mysql_escape( @$_REQUEST['city'] )."',
state = '".mysql_escape( @$_REQUEST['state'] )."',
zip = '".mysql_escape( @$_REQUEST['zip'] )."',
phone = '".mysql_escape( @$_REQUEST['phone'] )."',
cell = '".mysql_escape( @$_REQUEST['cell'] )."',
description = '".mysql_escape( @$_REQUEST['description'] )."',
updatedByUserNum = '0',
updatedDate = NOW()
WHERE email_address = '".mysql_escape( @$_REQUEST['email_address'] )."'";
mysql_query($query) or die("MySQL Error:<br />\n". htmlspecialchars(mysql_error()) . "\n");
$userNum = mysql_insert_id();
// on success
$first_name = @$_REQUEST['first_name'] ;
$last_name = @$_REQUEST['last_name'] ;
$_REQUEST = array(); // clear form values
$errorsAndAlerts = "The contact information for $first_name $last_name has been updated.<br />You can now enter another email address.";
?>
<?php endif ?>
__________________________________________________________
In the body of the viewer are some identifying information
_____ code ________________________________________________
<h1 class="body-text-white-bold-11">ADD/MODIFY PUBLICITY RECORDS</h1>
<span class="body-text-white-10">Enter as much information as you can.<br />
A </span><span class="body-text-bold-red-11">*</span> <span class="body-text-white-10">indicates a required field.</span>
<!-- PUBLICITY RECORD FORM -->
<?php if (@$errorsAndAlerts): ?>
<div style="color: #C00; font-weight: bold; font-size: 14px; font-family: arial;"><br />
<?php echo $errorsAndAlerts; ?><br />
</div>
<?php endif ?>
<br />

<?php if (@$errorsAndAlerts ==""):?><br /><span class="body-text-white-11">Enter the contact's email address here.<br />
If it exists, the form will be populated with the existing information and can be updated.<br />
If not, you can add a new record for this contact.</span><br /> <?php endif ?>
__________________________________________________________
The email address submission form
_____ code ________________________________________________
<form method="post" action="?">
<input type="hidden" name="save2" value="1" />
<table border="0" cellspacing="10" cellpadding="12">
<tr>
<td class=" body-text-bold-white-10">Email Address</td>
<td><input class="form" type="text" name="email_address" value="<?php echo htmlspecialchars(@$_REQUEST['email_address']); ?>" size="50" /></td>
</tr>
<tr>
<td colspan="2" align="center"><br />
<input class="button" type="submit" name="save2" value="Click To Enter Email &gt;&gt;" /></td>
</tr>
</table>
</form>
__________________________________________________________
And the main information form (note the use of $showupdate variable to set the update confirmation check box and the form name attributes for either updating or inserting records and the hidden email_address field so that the user is forced to pre-populate existing records when email addresses match):
_____ code ________________________________________________
<form method="post" action="?">
<input type="hidden" <?php if ($showupdate == 0 ): ?>name="save"<?php else:?>name="save2"<?php endif ?> value="1" />
<table border="0" cellspacing="10" cellpadding="12">
<tr>
<td colspan="2"><hr color="#b2b2b2" /></td>
</tr>
<tr>
<td colspan="2"><hr color="#b2b2b2" /></td>
</tr>
<tr>
<td colspan="2"><hr color="#b2b2b2" /></td>
</tr>
<tr>
<td class=" body-text-bold-white-10">Organization</td>
<td><input class="form" type="text" name="organization" value="<?php echo htmlspecialchars(@$_REQUEST['organization']); ?>" size="50" /></td>
</tr>
<tr>
<td class=" body-text-bold-white-10">Department</td>
<td><input class="form" type="text" name="department" value="<?php echo htmlspecialchars(@$_REQUEST['department']); ?>" size="50" /></td>
</tr>
<tr>
<td class=" body-text-bold-white-10"><span class="body-text-bold-red-11">*</span> First Name</td>
<td><input class="form" type="text" name="first_name" value="<?php echo htmlspecialchars(@$_REQUEST['first_name']); ?>" size="50" /></td>
</tr>
<tr>
<td class=" body-text-bold-white-10"><span class="body-text-bold-red-11">*</span> Last Name</td>
<td><input class="form" type="text" name="last_name" value="<?php echo htmlspecialchars(@$_REQUEST['last_name']); ?>" size="50" /></td>
</tr>
<tr>
<td class=" body-text-bold-white-10">Title</td>
<td><input class="form" type="text" name="title" value="<?php echo htmlspecialchars(@$_REQUEST['title']); ?>" size="50" /></td>
</tr>
<tr>
<td colspan="2"><hr color="#b2b2b2" />
<input type="hidden" class="form" name="email_address" value="<?php echo htmlspecialchars(@$_REQUEST['email_address']); ?>" /></td>
</tr>
<tr>
<td class=" body-text-bold-white-10">Street Address</td>
<td><input class="form" type="text" name="street_address" value="<?php echo htmlspecialchars(@$_REQUEST['street_address']); ?>" size="50" /></td>
</tr>
<tr>
<td class=" body-text-bold-white-10">City</td>
<td><input class="form" type="text" name="city" value="<?php echo htmlspecialchars(@$_REQUEST['city']); ?>" size="50" /></td>
</tr>
<tr>
<td class=" body-text-bold-white-10">State</td>
<td><input class="form" type="text" name="state" value="<?php echo htmlspecialchars(@$_REQUEST['state']); ?>" size="50" /></td>
</tr>
<tr>
<td class=" body-text-bold-white-10">Zip Code</td>
<td><input class="form" type="text" name="zip" value="<?php echo htmlspecialchars(@$_REQUEST['zip']); ?>" size="50" /></td>
</tr>
<tr>
<td colspan="2"><hr color="#b2b2b2" /></td>
</tr>
<tr>
<td class=" body-text-bold-white-10">Land Phone</td>
<td><input class="form" type="text" name="phone" value="<?php echo htmlspecialchars(@$_REQUEST['phone']); ?>" size="50" /></td>
</tr>
<tr>
<td class=" body-text-bold-white-10">Cell Phone</td>
<td><input class="form" type="text" name="cell" value="<?php echo htmlspecialchars(@$_REQUEST['cell']); ?>" size="50" /></td>
</tr>
<tr>
<td colspan="2"><hr color="#b2b2b2" /></td>
</tr>
<tr>
<td class=" body-text-bold-white-10">Additional Information</td>
<!-- Note: The first preg_replace <br /> should have no slash and no spaces between the <br and the >, the second a <br with no space before the slash>, the third with a space before the slash>) -->
<?PHP @$_REQUEST['description'] = preg_replace("[<br />]", "", @$_REQUEST['description'] ); // suppress in text area ?>
<?PHP @$_REQUEST['description'] = preg_replace("[<br />]", "", @$_REQUEST['description'] ); ?>
<?PHP @$_REQUEST['description'] = preg_replace("[<br />]", "", @$_REQUEST['description'] ); ?>
<td style="text-align:left"><textarea cols="50" rows="5" name="description" ><?php echo @$_REQUEST['description']; ?></textarea></td>
</tr>
<tr>
<td colspan="2"><hr color="#b2b2b2" /></td>
</tr>
<?php if ($showupdate == 1 ): ?>
<tr>
<td colspan="2"><input type = "checkbox" id="go" name="go" value = "1" <?php checkedIf(1, @$_REQUEST['go']);?> />
<span class=" body-text-bold-white-10">When you're ready to update, check this box and click "Submit"</span></td>
</tr>
<?php endif ?>
<tr>
<td colspan="2" align="center"><br />
<input class="button" type="submit" <?php if ($showupdate == 0 ): ?>name="submit"<?php else:?>name="submit2"<?php endif ?> value="Submit &gt;&gt;" /></td>
</tr>
</table>
</form>
__________________________________________________________

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php