Jump to content

Dynamic Contact Form


Elven6

Recommended Posts

Hello,

 

I'm in the process of creating a dynamic PHP contact form where a user can change the number of options available in the form and their type using an array, for example,

 

$items = array(
'Name' => field,
'Email' => field,
'Message' => textbox
);

 

In this case a form will be generated with text fields for Name and an E-mail address along with a text box for the message the user wants to send out.

 

The array is then processed in a separate function that spits out the form for the end user. After a user fills out the fields and submits the form, in theory, the form should be processed and mailed using the fields described in the array.

 

The part I'm having trouble with is the form submission, how can I go about processing the form based on the values the user entered into the contact form and the values available in the array? The form simply uses PHP and some HTML, no database is used and as a result I've pretty much hit a brick wall with this so I'm hoping "new eyes" might help.

 

Thanks,

Link to comment
Share on other sites

If I am understanding you right, do something like this. Note that I restructured your items array to be a little more flexible.

 

On your form page:

session_start();

$items[] = array('name' => 'Name', 'type' => 'text', 'value' => '', 'required' => true);
$items[] = array('name' => 'Email', 'type' => 'text', 'value' => '', 'required' => true);
$items[] = array('name' => 'Message', 'type' => 'textarea', 'value' => '', 'required' => true);

$_SESSION['form_items'] = $items;

foreach($items as $item)
{
switch($item['type'])
{
	case "text":
		echo '<input type="text" name="' . $item['name'] . '" value="' . $item['value'] . '" />';
	break;

	case "textarea":
		echo '<textarea name="' . $item['name'] . '">' . $item['value'] . '</textarea>';
	break;
}
}

 

On the process page:

session_start();

$items = $_SESSIOn['form_items'];

$errors = array();

foreach($items as $item)
{
if (!isset($_POST[$item])) {
	$errors[$item] = $item;
}
}

if (empty($errors)) {
echo 'no errors';
} else {
echo 'The following errors occured:<br />';
foreach($errors as $error)
{
	echo '- ' . $error . ' was empty<br />';
}
}

Link to comment
Share on other sites

When generating a contact form, appart from the typical "name, email, message", you can offer things such as dropdowns (<select>), checkboxes, etc. Visitors may need to generate a form that's more specific, where a "department" may be needed and stuff like that. Anyway, redixx's approach is a good start.

 

What you'll need to generate is PHP code too. If the contact form is supposed to be a copy-paste solution, it must have some PHP code that actually processes the user generated form and sends an email. Having some validation fields for each form element (required, email, phone number, etc) will help too. Finally, generate the form from the filled-in values and some PHP code too that validates the form (based on validation fields) and sends the email. It should be fairly easy with the code you got.

Link to comment
Share on other sites

Thanks for your responses,

 

If I understood the code correctly the,

 

foreach($items as $item)
{	
if (!isset($_POST[$item])) 
{		
$errors[$item] = $item;	
}
}

pPart would be what contains the actual mail(); aspect of the form?

 

Thanks for showing an alternative way to write the form, it seems like a much easier way to do things than how I was doing them previously (an HTML form with "if" statements for each of the possible types). My form validation previously consisted of "if" statements again (a user would need to manually add if values for the required variables) but the method you showed seems more practical, it also gives me ideas for other security features.

 

Thanks again,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.