PHP: Email validation

This entry is part 51 of 54 in the series PHP Tutorial

A lot of the times, we would need to ensure users have entered the right format of email address.

We can use a function filter_var() provided by PHP to validate emails.

<?php
$email_a = 'joe@example.com';
$email_b = 'bogus';

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "Your Email is " . $email_a;
    echo "<br><br>";
}

if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_b) email address is considered valid.";
} else
    echo "This ($email_b) email address is considered invalid.";
?>

You can try the above code here.

PHP: Form email validation

This entry is part 52 of 54 in the series PHP Tutorial

We combine the simple form that we have worked on with the email validation method we have discussed earlier in the previous post.

<html>
<body>

<form action="" method="post">
    Name: <input type="text" name="name"><br>
    E-mail: <input type="text" name="email"><br>
    <input type="submit" name="submit">
</form>

</body>
</html>


<html>
<body>

<?php
$your_name = $_POST["name"];
$your_email = $_POST["email"];

if (isset($_POST['submit'])) {
    echo "Welcome " . $your_name . "<br>";
    if (filter_var($your_email, FILTER_VALIDATE_EMAIL)) {
        echo "Your Email is " . $your_email;
    } else {
        echo "This ($your_email) email address is considered invalid.";
    }
}
?>
</body>
</html>

You can try the code here

PHP: URL validation

This entry is part 53 of 54 in the series PHP Tutorial

If we are to validate a web address or URL entered using PHP, we will have to ensure that users will also type in http:// in front of the web address.

<html>
<body>
<form action="" method="post">
    URL (Pls  include http://): <input type="text" name="url"><br><br>
    <input type="submit" name="submit">
</form>
</body>
</html>


<html>
<body>

<?php
$url = @$_POST["url"];

if (isset($_POST['submit'])) {
    if (filter_var($url, FILTER_VALIDATE_URL)) {
        echo "The valid URL is " . $url;
    } else {
        echo "This ($url) web address is considered invalid.";
    }
}
?>

</body>
</html>

You can try entering some URLs in the form here.

PHP: Passing variables from Javascript to PHP

This entry is part 54 of 54 in the series PHP Tutorial

Javascript is a client side programming language and it can’t interact with server side database such as MySQL.

If you are developing HTML 5 apps for Andoid or IOS, you will have to save some inputs into the database.  To store data, you can always pass them into php and store them using PHP.

A common technique is to use AJAX is illustrated by the example below.

ajax.html
<html>
<head>
    <script>
        function submitForm(form)
        {
            var text_input = form.inputbox.value;

            var xmlhttp=new XMLHttpRequest();

            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("text_id").innerHTML=xmlhttp.responseText;
                }
            }            
            xmlhttp.open("GET","get.php?str="+text_input,true);
            xmlhttp.send();
        }
    </script>
</head>

<body>
<form>Enter something in the box: <br><br>
    <input type="text" name="inputbox" value=""><P>
        <input type="button" name="button" value="Click" onClick="submitForm(this.form)">
</form>
<p id="text_id">Input your name to get a response from server</p>
</body>
</html>

get.php:

<?php
$name = $_GET['str'];
echo 'Hello ' . $name . '!';
?>

Put the above 2 files, ajax.html and get.php in the same directory and run ajax.html.

You can try it here.

In the line

xmlhttp.onreadystatechange=function(){}

We put the browser in a ready state to receive a response from the server.

We then send a request to the server using:

xmlhttp.open("GET","get.php?str="+text_input,true);
xmlhttp.send();

The syntax for the method is open(method,url,async)

Method is either in post or get method, we are using get method which is faster and simpler.

async needs to be set to true to enable asynchronous rather than synchronous.

The variable will be passed into the get.php script and store as $name variable.  We can then input the $name variable into MySQL if needed.