简体   繁体   English

在MySQL中成功插入数据后如何显示警告框?

[英]How to show alert box after successful data insertion in mysql?

I want to show JavaScript alert after successful data insertion in MySQL. 我想在MySQL中成功插入数据后显示JavaScript警报。 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! 我已经编写了这段代码,但是每次我打开此页面时,它都会显示JavaScript警报,并且当我单击JavaScript Alert的OK时,它会将我重定向到finalmem.php,而无需执行从用户那里获取值的操作!

$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. 提前致谢。

Use is set isset($_POST['submit']) to check whether user submits the form or not 使用set isset($_POST['submit'])来检查用户是否提交表单

    <?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. 如果> = 1,则为。

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: 如果您使用自动递增的列作为行ID,则还可以使用mysql_insert_id():

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

Then you can work with jQuery UI for show a dialog like this: 然后,您可以使用jQuery UI来显示如下对话框:

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

You need tot load the .css and .js files to run jQuery and inside your code put this: 您需要加载.css和.js文件以运行jQuery,并在代码中放入以下代码:

  <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. 通常,这对我来说是非常丑陋的,因为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>';
    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在将数据成功插入数据库后,我想为客户端显示消息。那么如何在javascript中使用alert()消息实现此功能? - I want to display message for client after successful insertion of data to database..so how to implement this using alert() message in javascript? procces成功后添加警报框 - Add alert box after procces is successful 如何在javascript中显示警报框 - how to show an alert box In javascript 如果提交表单成功,如何显示警报以及如何验证用户输入 - How to show an alert if submitting form was successful and how to validate the user inputs 如何在jquery中显示带复选框的警告框 - How to show alert box with checkbox in jquery 如何在javascript中显示警告框和innerHTML - How to make alert box and innerHTML show in javascript 如何在 JavaScript 中显示 + 和 - 登录警告框 - How to show + and - sign in alert box in JavaScript 如何在警报框中显示波斯字符? - how show persian characters in alert box? 关闭易看的弹出框,并使用联系表单7将成功的消息显示为警报 - close easy fancy box pop and show successful message as alert using contact form 7 如何在使用 react-native-image-picker 选择图像后显示警报框 - How to show alert Box after picking an image using react-native-image-picker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM