All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Use Sweet Alert In PHP

Last Updated : Mar 11, 2024

How To Use Sweet Alert In PHP

In this tutorial we will show you the solution of how to use sweet alert in PHP, here first we need to import sweet alert cdn link then only we can access that alert in php.

We defined form within form we just defined two input fields and a submit button.

In php we used isset() method for recognize whether submit is set and not null then we doing further process.

Then we checks whether two input fields are empty or not by empty() method if they empty then we used sweet alert process to alert user with some prebuilt layout alerts.

Step By Step Guide On How To Use Sweet Alert In PHP :-

Here we declared form with two input fields ‘name and email id’ and submit button.

In we need to confirm two thing which is in form tag must need attribute ‘method’ with ‘post’ value then within form each elements must define with ‘name’ attribute with respective values because we are going to use those element values in php block of code.

In php block we using isset() method for checks whether submit button is set and not null then we collects user entered name, email fields values by post method.

Then we checks input field filled by user or not if they not filled then sweet alert will popup to alert users.

<!Doctype html>
<html>
    <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/11.4.24/sweetalert2.all.js"></script>
    </head>
    <body>
        <form method="post">
            <input name="nm" type="text" placeholder="Name">
            <input name="em" type="text" placeholder="Email">
            <input name="sub" type="submit" value="Submit">
        </form>
        <?php
    if(isset($_POST['sub'])){
        $n=$_POST['nm'];
        $e=$_POST['em'];
        if(empty($n) || empty($e)){
        ?>
        <script>
            Swal.fire({
                icon: 'error',
                title: 'Invalid',
                text: 'Input Fields Cant Be Empty'
            })
        </script>
        <?php
        }
}
?>
    </body>
</html>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. First we need to import cdn of sweet aler link because we need to use they already built method on webpage.
  4. Here we defined form with ‘method’ attribute with value ‘post’ because when we process form values in php this attribute will only provide permission for access form elements values.
  5. Within form we created two input tag for define two input fields ‘name,email’ and last one for define submit button.
  6. Then in php block we checks using if condition whether submit is set or not by isset() method and if returns true then we collects user entered input field details and stored to variable ‘$n,$e’.
  7. Another if checks whether variables $n, $e null or not if they null then we need to notify user to fill those input fields for that we used sweet alert method.
  8. This is a prebuilt method we can use them on any website for alert user and its advantages are less code, great output, no complexity and time save.

Conclusion :-

In conclusion we are able to know how to use sweet alert in php.

First we need to start our xampp server then we load this program on browser we can see result of two input fields for collects input from user then when user clicks submit php block will start the process then if they not provide input then sweet alert error message will displayed on screen.

In sweet alert they provided more types of alert method you can try some other methods also.

I hope this tutorial on how to use sweet alert in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪