因此,正如您在下面的代码中所看到的,我想执行一个“ if”语句来检查查询是否正确运行。如果是,它将存储在变量$output
中,作为脚本的一部分,该存储什么?立即被ajax脚本中包含的其余通知脚本读取。
基本上,“ if”语句仅用于格式化通知类型,例如成功,错误等。
我正在使用overhang.js作为通知。
<?php $con=mysqli_connect('localhost', 'root', '', 'teste');
if(!$con) {
die('Connection not Establish');
}
if(isset($_POST['data'])) {
$output='';
$texto=$_POST['texto'];
$data_nasc=$_POST['data'];
$sql="INSERT INTO teste(texto, data) VALUES ('$texto', '$data_nasc')";
$result=mysqli_query($con, $sql);
if($result) {
$buscar=mysqli_query($con, "SELECT * FROM teste ORDER BY id DESC");
$linha=mysqli_fetch_array($buscar);
$output ='$("body").overhang({
type: "success",
message: "Yeiii",
closeConfirm: true
});';
echo $output;
}
else {
$output.='$("body").overhang({
type: "error",
message: "Whoops! Something went wrong!",
closeConfirm: true
});';
echo $output;
}
}
?>
`
<?php
$con=mysqli_connect('localhost','root','','teste');
if(!$con)
{
die('Connection not Establish');
}
$sql="SELECT * from teste";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_array($result);
?>
<!Doctype html>
<html>
<head>
<title>How to show data on bootstrap modal using ajax with php</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<meta charset="UTF-8" />
<meta name="author" content="Paul Krishnamurthy" />
<link rel="icon" href="demo/logo.ico" />
<link rel="stylesheet" type="text/css" href="demo/style/index.css" />
<link rel="stylesheet" type="text/css" href="dist/overhang.min.css" />
<link rel="stylesheet" type="text/css" href="demo/style/prism.css" />
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" />
<script type="text/javascript" src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="dist/overhang.min.js"></script>
<script type="text/javascript" src="demo/js/prism.js"></script>
<script type="text/javascript" src="demo/js/index.js"></script>
</head>
<body>
<div class="container">
<h1 align="center">How to show data on bootstrap modal using ajax with php</h1>
<button style="margin-top:10%;" class="submit" id="">Click to show data on bootstrap modal</button>
<input type="text" name="texto">
<input type="date" name="data_nasc">
<script type="text/javascript">
$(document).on('click', '.submit', function() {
$texto = $("input[name=texto]").val();
$data = $("input[name=data_nasc]").val();
$.ajax({
url: "show_data.php",
method: "post",
data: {
texto: $texto,
data: $data
},
success: function(data) {
<?php echo $output;?>
}
});
});
</script>
</body>
</html>
答案 0 :(得分:1)
您仅应使用ajax从服务器端代码返回true或false 并在ajax成功方法中处理其余的javascript。
服务器端代码:
<?php
$con=mysqli_connect('localhost', 'root', '', 'teste');
if(!$con) {
die('Connection not Establish');
}
if(isset($_POST['data'])) {
$output='';
$texto=$_POST['texto'];
$data_nasc=$_POST['data'];
$sql="INSERT INTO teste(texto, data) VALUES ('$texto', '$data_nasc')";
$result=mysqli_query($con, $sql);
if($result) {
$buscar=mysqli_query($con, "SELECT * FROM teste ORDER BY id DESC");
$linha=mysqli_fetch_array($buscar);
$output = TRUE;
}
else {
$output = FALSE;
}
echo $output;
}
?>
客户端代码:
<?php
$con=mysqli_connect('localhost','root','','teste');
if(!$con)
{
die('Connection not Establish');
}
$sql="SELECT * from teste";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_array($result);
?>
<!Doctype html>
<html>
<head>
<title>How to show data on bootstrap modal using ajax with php</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<meta charset="UTF-8" />
<meta name="author" content="Paul Krishnamurthy" />
<link rel="icon" href="demo/logo.ico" />
<link rel="stylesheet" type="text/css" href="demo/style/index.css" />
<link rel="stylesheet" type="text/css" href="dist/overhang.min.css" />
<link rel="stylesheet" type="text/css" href="demo/style/prism.css" />
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" />
<script type="text/javascript" src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="dist/overhang.min.js"></script>
<script type="text/javascript" src="demo/js/prism.js"></script>
<script type="text/javascript" src="demo/js/index.js"></script>
</head>
<body>
<div class="container">
<h1 align="center">How to show data on bootstrap modal using ajax with php</h1>
<button style="margin-top:10%;" class="submit" id="">Click to show data on bootstrap modal</button>
<input type="text" name="texto">
<input type="date" name="data_nasc">
<script type="text/javascript">
$(document).on('click', '.submit', function() {
$texto = $("input[name=texto]").val();
$data = $("input[name=data_nasc]").val();
$.ajax({
url: "show_data.php",
method: "post",
data: {
texto: $texto,
data: $data
},
success: function(data) {
if(data == true){
// success
$("body").overhang({
type: "success",
message: "Yeiii",
closeConfirm: true
});
}else{
// your false script 2019-07-08 11:10:33
$("body").overhang({
type: "error",
message: "Whoops! Something went wrong!",
closeConfirm: true
});
}
<?php echo $output;?>
}
});
});
</script>
</body>
</html>
答案 1 :(得分:0)
使用mysqli_error()
方法检查查询是否有错误。
在您当前的代码中,建议您在 PHP 端使用JSON作为返回类型。要在js中提取数据,请使用JSON.parse(data)
。我不建议在js中使用<?php echo $output;?>
。
if (mysqli_error($buscar))
{
// an error eoccurred
}