Removing "_" in a value?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smurfman82
Forum Newbie
Posts: 2
Joined: Wed Oct 12, 2011 5:45 pm

Removing "_" in a value?

Post by smurfman82 »

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

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");
    }
?>
The Form

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>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Removing "_" in a value?

Post by Celauran »

Code: Select all

if(in_array($key, $requiredFields) && $value == '')
{
    $errors[] = "The field " . str_replace("_", " ", $key) . " is required.";
}
smurfman82
Forum Newbie
Posts: 2
Joined: Wed Oct 12, 2011 5:45 pm

Re: Removing "_" in a value?

Post by smurfman82 »

@celauran Why didn't I think of that... So obvious... Eh... Thank you for the help. :)
Post Reply