[英]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.