August 7, 200917 yr Hi all, I have been using PHP for about 3 months so bare with me... I'm building a site that uses lots of forms, thankfully they're all quite simple requests for email address/comments etc, at the moment I am getting the variables from the post array on each individual page like this: <?php $name=$_POST['Name']; $business=$_POST['Business']; $email=$_POST['Email']; ?> which works fine, but it isnt very efficient, especially if theres lots of variables. I want to create a generic form handler that i can call as an include, so use the POST array more like; <?php $userinput=$_POST ; mycheckInputfunction($userinput); echo "Thanks for your enquiry".$userinput['0'] ; ?> Any help would be greatly appreciated
August 8, 200917 yr You could loop through the POST array and assign a variable for each item in the array, this would probably not be a good idea though as people could potentially post anything they want and start messing with your variables. Perhaps this would be better than manually assigning each variable. $form = array('Name', 'Address', 'Postcode', 'Etc'); foreach ($form as $field) { if (isset($_POST[$field])) { $$field = $_POST[$field]; } } This will create $Name, $Address, $Postcode etc for you and on line 4 where the variables are defined you can use any clean up functions you may want to perform on the user input.
Create an account or sign in to comment