javascript - bootstrap modals put values in database
问题描述
I have a modal with an input
to add a new category and at the same time to check if this value is already in my database and show me a message.
the main code is this Javascript
$(document).ready(function() {
$("#savebutton").click(function() {
var name_category = $("#new_category").val();
if (name_category != "") {
alert("has values");
$.post("action.php", {
suggestion: name_location
}, function(data, status) {
$("#test").html(data);
});
} else {
alert("give category");
}
});
});
and the PHP code is
$connection = sqlsrv_connect($server, $cinfo);
if( $connection === false )
{
echo "Error (sqlsrv_connect): " . print_r(sqlsrv_errors(), true);
}
if(isset($_POST['suggestion']))
{
$query = "SELECT jacat_title FROM JobAdCategories WHERE jacat_title='".$_POST['suggestion']."'";
$statement=sqlsrv_query($connection,$query);
if(sqlsrv_num_rows($statement) > 0) {
echo"Yes";
} else {
echo"No";
}
}
sqlsrv_close($connection);
解决方案
尝试在您的 ajax 中使用 async false 。
$.post("action.php", {
suggestion: name_location,
type: 'POST',
async: false //blocks window close
}
希望它可以帮助你
推荐阅读
- python - Tensorflow 1.14+:docker 映像无法按预期使用 docker-compose
- javascript - 如何在更大的 HTML 文件中显示(或嵌套)一个单独的 HTML 文件/段的内容?
- elasticsearch - 我可以将咆哮位图用于 lucence 倒排索引吗
- sas - SAS:避免终端问题和丢失卡
- html - 输入字段“日期”在 iOS Safari 中的间距未知
- sympy - SymPy三角函数的周期性
- c# - 使用扭矩稳定气垫船刚体直立
- angular - Angular/RXJS - 跟踪陷入重试模式的 HTTP 请求
- c# - 如何使用 Visual Studio 使用 C# 连接到 SQL Server 实例
- excel - Power Query 单元格中的字符少于源单元格