我正在使用php。我有一个来自并在同一页面上提交此表单。我的代码如下:
<form method="post">
<input type = "text" name="u_name">
.....
<input type = "submit" name = "submit" value="Submit">
</form>
<?php
$u_name = $_POST["u_name"];
//insert query here
?>
这里我将数据发布到同一页面。现在我想创建弹出窗口,就像成功插入数据后成功插入记录一样。那么我应该做什么代码呢?
答案 0 :(得分:0)
使用javascript警告: -
<?php
$u_name = $_POST["u_name"];
//insert query here
echo '<script language="javascript">';
echo 'alert("record is successfully inserted")';
echo '</script>';
?>
答案 1 :(得分:-1)
首先:您不能使用“from”标记提交表单,我认为您的意思是“表单”。
对于您的问题:
1)通过jQuery ajax
发布数据2)从服务器接收响应(从PHP返回HTTP响应)
3)根据响应显示弹出窗口(通过提醒功能或某些js libs,如SweetAlert)
说话很便宜,请告诉我代码
客户端:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>title</title>
<link rel="stylesheet" type="text/css" href="path/to/sweetalert.css"/>
<script src="path/to/jquery.min.js"></script>
<script src="path/to/sweetalert.min.js"></script>
</head>
<body>
<form method="post" id="myform">
<input type = "text" name="u_name">
<input type = "submit" name = "submit" value="Submit">
</form>
</body>
<script>
$(function(){
$('#myform').submit(function(e){
e.preventDefault();
var u_name = $('input[name="u_name"]').val();
$.ajax({
type: "POST",
url: 'http://your.url',
data: {
u_name:u_name
},
dataType: "JSON",
success: function(res) {
//alert(res.msg);
sweetAlert("popup window", res.msg, "info");
}
});
});
});
</script>
</html>
服务器:
<?php
define('SUCCESS_CODE', 1);
define('SUCCESS_MSG', 'Success!');
$name = isset($_POST['u_name']) ? $_POST['u_name'] : '';
$res = array(
'code' => SUCCESS_CODE,
'msg' => SUCCESS_MSG,
'data' => $name
);
echo json_encode($res);
exit();
?>