Removing "_" in a value?
Posted: Wed Oct 12, 2011 5:51 pm
This might be an easy one but I'm stumped...
So when the error message is printed to the screen it leaves the "_" in the name. However I can't rename it since it is dependent on the value="" and yet I can't remove the "_" in the value field... Any ideas? I tried the str_replace and it was a no go... Maybe my logic is backwards here...
The Script
The Form
So when the error message is printed to the screen it leaves the "_" in the name. However I can't rename it since it is dependent on the value="" and yet I can't remove the "_" in the value field... Any ideas? I tried the str_replace and it was a no go... Maybe my logic is backwards here...
The Script
Code: Select all
/*
* Specify the field names that are in the form. This is meant
* for security so that someone can't send whatever they want
* to the form.
*/
$allowedFields = array(
'First_Name',
'Last_Name',
'Email',
'Message',
);
// Specify the field names that you want to require...
$requiredFields = array(
'First_Name',
'Last_Name',
'Email',
'Message',
);
// Loop through the $_POST array, which comes from the form...
$errors = array();
foreach($_POST AS $key => $value)
{
// first need to make sure this is an allowed field
if(in_array($key, $allowedFields))
{
$$key = $value;
// is this a required field?
if(in_array($key, $requiredFields) && $value == '')
{
$errors[] = "The field $key is required.";
}
}
}
// were there any errors?
if(count($errors) > 0)
{
$errorString = '<p>Oops! Looks like there was a problem.</p>';
$errorString .= '<ul>';
foreach($errors as $error)
{
$errorString .= "<li>$error</li>";
}
$errorString .= '</ul>';
// display the previous form
include 'contact.php';
}
else
{
// At this point you can send out an email or do whatever you want
// with the data...
// each allowed form field name is now a php variable that you can access
// display the thank you page
mail($to, $subject, $completemessage, $headers);
header("Location: thankyou.php");
}
?>
Code: Select all
<!-- Return Validation -->
<?=$errorString?>
<!-- End Validation -->
<div class="form-box first">
<div class="form-label"><label>First Name</label></div>
<div class="form-input"><input name="First_Name" value="<?=$First_Name?>" type="text" /></div>
</div>