Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Phone number UK - Converting string to integer - retain digits.

$
0
0

Good mornng all,

 

I have a simple contact form which ask for some details from the user including name, number, email, referrer and any comments they may have:

<form method="post" action="?page=Contact">
    <fieldset>
        <legend>TSPV-Websites</legend>
        
        <p><label for="name">Name:</label><input type="text" id="name" name="name" value="<?php if(isset($_POST['name'])){ print(htmlspecialchars($_POST['name'])); } ?>" /> <span>* Required</span></p>
        <p><label for="phone">Phone:</label><input type="text" id="phone" name="phone" value="<?php if(isset($_POST['phone'])){ print(htmlspecialchars($_POST['phone'])); } ?>" /> <span>* Required</span></p>
        <p><label for="email">Email:</label><input type="text" id="email" name="email" value="<?php if(isset($_POST['email'])){ print(htmlspecialchars($_POST['email'])); } ?>" /></p>
        <p><label for="referrer">Referrer:</label>
            <select name="referrer" id="referral_list">
                <option name="default">Choose an option...</option>
                <option name="search_engine">Search Engine</option>
                <option name="facebook">Facebook</option>
                <option name="twitter">Twitter</option>
                <option name="friend">Friend</option>
                <option name="other">Other</option>
            </select>
        </p>
        <p><label class="last_label" for="comments">Comments:</label><textarea name="comments" rows="7" cols="35"><?php if(isset($_POST['comments'])){ print($_POST['comments']); } ?></textarea></p>
        <p class="form_btns"><input type="submit" value="Send" /><input type="reset" value="Clear" /></p>
    </fieldset>
    
</form>

 

I want to convert the phone number into an integer but keep losing the preceeding 0's and I've just entered a mobile number example: 07973 762 960 and after I get redirected back to the form (because of an invalid email), it shows only: 7973.

 

Not sure what to do. My database field is set to string for a phone number but I want to change it to int. I want to perform some checks on the phone number to make sure it contains 10 or 11 digits and starts with a 0. I realize there are string functions to check first characters of a string and length but need it to be an integer once sent to database and this issue will arise then.

 

My form processing code is:


//Process the contact form

if(isset($_POST['name']))
{
    foreach($_POST as &$v)
    {
        $v = trim($v);
    }
    
    
    if($_POST['name'] == "" || $_POST['phone'] == "")
    {
        $error = "<span class='error'>Please fill in both required (*) fields.</span>";
    }
    
    $_POST['phone'] = str_replace(array("(",")","-"),"",$_POST['phone']);
    $len = strlen($_POST['phone']);
    
    var_dump($len);
    $_POST['phone'] = (int) $_POST['phone'];
    if(!ctype_digit($_POST['phone']))
    {
        if($len < 10 || $len > 11)
        {
            if(isset($error))
            {
                $error = str_replace("</span>"," ",$error);
                $error .= "<br />Please enter a valid number</span>";
            }
            else
            {
                $error = "<span class='error'>Please enter a valid number.</span>";
            }    
        }
    }
    if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
    {
        if(isset($error))
        {
            $error = str_replace("</span>"," ",$error);
            $error .= "<br />Please enter a valid email</span>";
        }
        else
        {
            $error = "<span class='error'>Please enter a valid email.</span>";
        }        
    }
    
    if(!isset($error))
    {
        //no errors, we have required data - continue
        
    
    }
    
}

 

Thank you for you time and guidance.

 

Kind regards,

 

L2c.


Viewing all articles
Browse latest Browse all 13200

Trending Articles