使用Ajax将数据插入到mysql
问题描述:
我想使用Ajax将我的数据插入到mysql中。但是,我无法将其保存到数据库中。任何人都可以提出我的错误在哪里?使用Ajax将数据插入到mysql
的HTML和Ajax代码如下:
<div class="modal fade" id="addJoint2" role="dialog" action="Handle.php">
<table id="01">
<tr>
<th></th>
<th>Joint Applicant2</th>
</tr>
<tr>
<td>Occupation</td>
<td><input type="text" id="occupationJoint2" name="occupationJoint2" type="text" value="<?php echo $occupationJoint2; ?>"></td>
</tr>
</table>
<div class="modal-footer">
<button type="button" class="btn btn-success btn-lg" data-dismiss="modal" style="width: 100%;"><span class="glyphicon glyphicon-ok-sign"></span>Insert</button>
</div>
</div>
<script>
$('#addJoint2 button.btn.btn-warning.btn-lg').click(function (event) {
event.preventDefault();
$.ajax({
url: "Handle.php", //this is the submit URL
type: 'POST', //or GET
data: $('#addJoint2 form').serialize(),
success: function (data) {
alert('Joint 2 Added');
window.location.reload();
}
});
});
</script>
我不知道的ID,必须是一样吗?正如我将#addJoint2
放入我的Ajax时发生错误(当我点击该字段时,将出现警报)。
我handle.php:
<?php
session_start();
require_once 'db/dbfunction.php';
$con = open_connection();
function addemployementdetails3($con, $occupationJoint2){
$query2 = "insert into employementdetails(Occupation)
values('$occupationJoint2')";
$insertResult2 = mysqli_query($con, $query2);
if($insertResult2){
echo " Applicant Detail Added !<br />";
echo "<a href='index.php'>Back to Home</a>";
}
else {
echo " Error !";
echo "{$query2}";
//header('Location: post.php');
}
}
if (isset($_POST['occupationJoint2'])){
$occupationJoint2 = $_POST['occupationJoint2'];
addemployementdetails3($con, $occupationJoint2);
}
close_connection($con);
答
把输入类型的表单中,并给予ID的形式
<form id="addJoint2">
<input type="text" id="occupationJoint2" name="occupationJoint2" type="text" value="<?php echo $occupationJoint2; ?>">
</form>
<input type="button" id="btn" value="submit">
,并使用以下的jQuery从所有表单字段中获取价值。
<script>
$('#btn').click(function() {
$.ajax({
url: "Handle.php", //this is the submit URL
type: 'POST', //or GET
data: $('#addJoint2').serialize(),
success: function (data) {
alert('Joint 2 Added');
window.location.reload();
}
});
});
</script>
以后可以收集这样所有的现场数据后,提取这样的数据在目标页面
$value1=$_POST['occupationJoint2'];
,将其插入到数据库中。
帮助我们来帮助你..你有什么错误? ajax请求是否被执行? (在chrome或firefox下检查网络标签) – Superdrac
是你输入的一种形式吗?我没有看到$'('#addJoint2 form')。serialize()'工作的任何表单标签 – CumminUp07
数据不存储在mysql中 – xhinvis