How to Create Newsletter Signup Form Using jQuery and PHP

A newsletter opt-in form using jQuery, PHP, and MySQL. An alternative version using html5 form validation is included in this package. See it here. This is a newsletter sign-up for your website so visitors can subscribe to your newsletter. The visitor is required to enter a name and a valid email.

PHP and HTML Code

<?php
    define('DB_SERVER', "localhost");
    define('DB_USER', "root");
    define('DB_PASS', "");
    define('DB_DATABASE', "ieltsmedidb");
    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
    
    if(isset($_POST['submit_form']))
    {
        $name=$_POST["user_name"];
        $email=$_POST["email"];

        mysqli_query($con,"insert into news (name,email) values('$name','$email')");
        echo "Thank You For Joining Our Newsletter";
    }
?>

<html>
    <body>
        <h1 class="">Subscribe To Get Email Updates Of Our Latest Tutorials On Web Development</h1>
        <form action="" method="post">
            <p>
                <input type="text" required="" name="user_name" id="user_name" placeholder="Enter Your Name">
                <input type="text" required="" name="email" id="email" placeholder="Enter Your Email Id"/>
            </p>
            <input type="submit" value="Subscribe" name="submit_form"/>
        </form>    
    </body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *