最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to show alert box after successful data insertion in mysql? - Stack Overflow

programmeradmin9浏览0评论

I want to show JavaScript alert after successful data insertion in MySQL. How to do this? I have written this code but it shows JavaScript alert everytime I open this page and as soon as i click on OK of JavaScript alert it redirects me to finalmem.php, without the action of taking values from users!

$sql="INSERT INTO members VALUES ('$name', '$email', '$ybr', '$ach')";

if(!mysqli_query($con,$sql)) 
{
    die('Error: ' . mysqli_error($con));
}

else
{

    echo '<script language="javascript">';
    echo 'alert("Successfully Registered"); location.href="finalmem.php"';
    echo '</script>';
}       

Thanks in advance.

I want to show JavaScript alert after successful data insertion in MySQL. How to do this? I have written this code but it shows JavaScript alert everytime I open this page and as soon as i click on OK of JavaScript alert it redirects me to finalmem.php, without the action of taking values from users!

$sql="INSERT INTO members VALUES ('$name', '$email', '$ybr', '$ach')";

if(!mysqli_query($con,$sql)) 
{
    die('Error: ' . mysqli_error($con));
}

else
{

    echo '<script language="javascript">';
    echo 'alert("Successfully Registered"); location.href="finalmem.php"';
    echo '</script>';
}       

Thanks in advance.

Share Improve this question edited Aug 16, 2015 at 15:57 hemnath mouli 2,7552 gold badges20 silver badges37 bronze badges asked Aug 16, 2015 at 12:50 Sankalp NigamSankalp Nigam 371 gold badge1 silver badge8 bronze badges 7
  • try this – Drew Commented Aug 16, 2015 at 12:53
  • Why not use PDO - here's a good simple tutorial: code.tutsplus./tutorials/… – Trung Nguyen Commented Aug 16, 2015 at 12:59
  • you have to put above code in if statement, e.g if you posting from form so if(isset($_POST['submit']){ put above code here} right now your code is like a door without lock, anyone can pass through. – Shehary Commented Aug 16, 2015 at 13:00
  • It's unclear how your code above would work. Is there anyplace you are attempting to submit this? – nomistic Commented Aug 16, 2015 at 13:27
  • @Shehary i m using session also ... because i m validating data before form submission.<?php session_start(); $_SESSION['sname'] = $_POST["name"]; $_SESSION['sybr'] = $_POST["ybr"]; $_SESSION['semail'] = $_POST["email"]; $_SESSION['sach']= $_POST["ach"]; ?> this part at starting then at the middle m doing data validation and then if all the inputs are correct then i m submitting the data . before the $sql query above i have written $name = $_SESSION['sname']; $ybr = $_SESSION['sybr']; $email = $_SESSION['semail']; $ach = $_SESSION['sach']; – Sankalp Nigam Commented Aug 16, 2015 at 14:47
 |  Show 2 more ments

3 Answers 3

Reset to default 1

Use is set isset($_POST['submit']) to check whether user submits the form or not

    <?php

    include 'SQLIDB.php';   

    if(isset($_POST['submit'])){

        $name=$_POST['name'];
        $email=$_POST['email'];
        $ybr=$_POST['ybr'];
        $ach=$_POST['ach'];

    $sql="INSERT INTO members VALUES ('$name', '$email', '$ybr', '$ach')";

    if(!mysqli_query($con,$sql)) 
    {
        die('Error: ' . mysqli_error($con));
    }

    else
    {

        echo '<script language="javascript">';
        echo 'alert("Successfully Registered"); location.href="finalmem.php"';
        echo '</script>';
    }  

    }

?>

    <form action="" method="post">

        <input type="text" name="name">
        <input type="text" name="email">
        <input type="text" name="ybr">
        <input type="text" name="ach">

        <input type="submit" name="submit" value="Submit">

    </form>

You need to know two things, one of them it's to confirm if your data it's saved succefully.

For this you can play with

mysql_affected_rows() 

and this function will return the number of rows affected by your query. If zero, it was not inserted. If >= 1, it was.

So:

if ( mysql_affected_rows() >= 1 ){ /* inserted! now do something... */ }

If you are using an auto-incrementing column for row ID, you can use mysql_insert_id() as well:

if ( mysql_insert_id() > 0 ) { /* inserted! now do something... */ }

Then you can work with jQuery UI for show a dialog like this:

[https://jqueryui./dialog/][1]

You need tot load the .css and .js files to run jQuery and inside your code put this:

  <script>
  $(function() {
    $( "#dialog" ).dialog();
  });
  </script>

And this in your view:

<div id="dialog" title="Basic dialog">
  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

Normally this it's super ugly to do for me, because the best way it's doing this by AJAX.

You can try like this

<?php session_start();
//Include database detail here
if(isset($_POST['name'])){
    $name = mysqli_real_escape_string($_POST["name"]);
    $ybr = mysqli_real_escape_string($_POST["ybr"]);
    email = mysqli_real_escape_string($_POST["email"]);
    $ach = mysqli_real_escape_string($_POST["ach"]);

    //Do your data validation here

    $_SESSION['sname'] = $name;
    $_SESSION['sybr'] = $ybr;
    $_SESSION['semail'] = email;
    $_SESSION['sach']= $ach;

    $sql="INSERT INTO members VALUES ('$name', '$email', '$ybr', '$ach')";
    if(!mysqli_query($con,$sql)) {
        die('Error: ' . mysqli_error($con));
    } else {
        echo '<script language="javascript">';
        echo 'alert("Successfully Registered"); location.href="finalmem.php"';
        echo '</script>';
    }
}
?>
发布评论

评论列表(0)

  1. 暂无评论