我想使用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,它一定是一样吗?当我在我的Ajax中放置#addJoint2
时,发生了错误(当我点击该字段时,将出现警报)。
我的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);
答案 0 :(得分:1)
将输入类型放在表单中,并将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'];
收集完这样的所有字段数据后,将其插入数据库。