how do i display an alert box after inserting records into the database using javascript and php?

The code below is able to insert into the database but doesn't display anything.

I tried playing around with the code using JavaScript and AJAX though I only have basic understanding of the two and wasn't able to change anything.

I am a beginner and any help will be appreciated.

<?php
 require_once 'connect.php';
 if (isset($_POST['submit'])) 
 {
    {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];

        $sql = "INSERT INTO message (Names, Email, Numbers,Message)
        VALUES ('$name','$email','$phone','$message')";
        echo"Record Enterd";

        if ($conn->query($sql) === true) {

        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }

    }


 }
 ?>

Upvotes: 0

Views: 5042

Answers (2)

Pratik Karmakar
Pratik Karmakar

Reputation: 257

Try This:

echo "<script>alert('Record Added Successfully')</script>";

Upvotes: 2

Hiren Vaghasiya
Hiren Vaghasiya

Reputation: 5544

Use this

if ($conn->query($sql) === true) {
        ?>
    <script type="text/javascript"> alert('data submitted successfully'); </script>
    <?php
                } else {
                    echo "Error: " . $sql . "<br>" . $conn->error;
                }

Upvotes: 2

Related Questions