我试图通过Anchor链接点击一个MySQL表数据库从Bootstrap Modal发送一些数据,但是我没有让它工作。
这是调用模态的PHP: -
<a href='#' data-toggle='modal' data-target='#add-post' data-userid=" . $_SESSION['user_id'] . " data-username=" . $username . " data-email=" . $email . ">Add a post</a>
这是Bootstrap Window模式: -
<div class="modal fade" id="add-post" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<form class="form-horizontal" role="form" name="add_post">
<input type="hidden" name="username" value="<?php echo $username; ?>">
<input type="hidden" name="email" value="<?php echo $email; ?>">
<div class="form-group">
<label class="col-sm-3 control-label" for="gebruikersnaam" style="text-align: left;">Username</label>
<div class="col-sm-9 control-label" style="text-align: left;"><?php echo $username; ?></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="email" style="text-align: left;">E-mail</label>
<div class="col-sm-9 control-label" style="text-align: left;"><?php echo $email; ?></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="post" style="text-align: left;">Post</label>
<div class="col-sm-9 control-label" style="text-align: left;"><textarea name="post" rows="5" cols="70"></textarea></div>
</div>
</form>
</div>
<div class="modal-footer">
<input class="btn btn-success" type="submit" value="Add post" id="submit" />
</div>
</div>
这是使用process.php发送数据的JQuery AJAX语法
<script>
$(document).ready(function () {
$("input#submit").click(function(e){
$.ajax({
type: "POST",
url: "process.php", //
data: {
'username':username,
'email':email
},
success: function(msg){
//alert("ok");
$('#add-post').modal('hide');
},
error: function(){
alert("Something went wrong!");
}
});
});
});
</script>
process.php是:
<?php
if ($_POST['post']) {
$sql = "INSERT INTO posts (username, email, post , date) VALUES ('". htmlspecialchars($username, ENT_QUOTES) . "', '". htmlspecialchars($email, ENT_QUOTES) . "', '" . htmlspecialchars($_POST['post'], ENT_QUOTES) . "', NOW())";
$res = mysqli_query($con, $sql) or die(mysql_error());
}
?>
知道出了什么问题吗?
答案 0 :(得分:2)
尝试这样的脚本
$(document).ready(function () {
$("input#submit").click(function(e){
$.ajax({
type: "POST",
url: "process.php", //
data: {
'username':$('input[name=username]').val(),
'email':$('input[name=email]').val()
},
success: function(msg){
alert("ok");
$('#add-post').modal('hide');
},
error: function(){
alert("Something went wrong!");
}
});
});
});