Elven6 Posted June 17, 2011 Share Posted June 17, 2011 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, Quote Link to comment https://forums.phpfreaks.com/topic/239606-dynamic-contact-form/ Share on other sites More sharing options...
redixx Posted June 17, 2011 Share Posted June 17, 2011 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 />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/239606-dynamic-contact-form/#findComment-1230858 Share on other sites More sharing options...
Fadion Posted June 17, 2011 Share Posted June 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239606-dynamic-contact-form/#findComment-1230867 Share on other sites More sharing options...
Elven6 Posted June 17, 2011 Author Share Posted June 17, 2011 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, Quote Link to comment https://forums.phpfreaks.com/topic/239606-dynamic-contact-form/#findComment-1230873 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.