Remove Unwanted Items from $_POST Array

May 21, 2013 Development, PHP

For a contact form script, I was using a PHP foreach loop to run through the $_POST array and print out the visitor’s message no matter the number of fields.

If the contact form had 10 fields, 20 fields or more, the loop would process each one and create an email message. This script was being used on multiple websites and I didn’t want to custom tailor each one to match the form fields.

However, the $_POST[‘submit’] variable was coming through as well along with its value of Send. This was quite easy to remove with the unset PHP function.

PHP

unset($_POST['submit']);

Before the loop started, I executed the unset function on the fields I didn’t want to include. The loop then ran and my email message included only the necessary fields.

This would also be useful to run on $_POST[‘recaptcha_challenge_field’] and $_POST[‘recaptcha_response_field’] after they have been utilized to remove those fields as well.

Let's Talk About Your New Website

View Portfolio Contact Today