javascript - Using Ajax to insert data to mysql -
i want insert data mysql using ajax. however, not able save database. can suggest mistakes is?
the html , ajax code below:
<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 submit url type: 'post', //or data: $('#addjoint2 form').serialize(), success: function (data) { alert('joint 2 added'); window.location.reload(); } }); }); </script>
i not sure id, must same? when put #addjoint2
in ajax, error occurred (when click on field, alert appear).
my handle.php:
<?php session_start(); require_once 'db/dbfunction.php'; $con = open_connection(); function addemployementdetails3($con, $occupationjoint2){ $query2 = "insert employementdetails(occupation) values('$occupationjoint2')"; $insertresult2 = mysqli_query($con, $query2); if($insertresult2){ echo " applicant detail added !<br />"; echo "<a href='index.php'>back home</a>"; } else { echo " error !"; echo "{$query2}"; //header('location: post.php'); } } if (isset($_post['occupationjoint2'])){ $occupationjoint2 = $_post['occupationjoint2']; addemployementdetails3($con, $occupationjoint2); } close_connection($con);
put input type inside form , give id form
<form id="addjoint2"> <input type="text" id="occupationjoint2" name="occupationjoint2" type="text" value="<?php echo $occupationjoint2; ?>"> </form> <input type="button" id="btn" value="submit">
and use following jquery value form fields.
<script> $('#btn').click(function () { $.ajax({ url: "handle.php", //this submit url type: 'post', //or data: $('#addjoint2').serialize(), success: function (data) { alert('joint 2 added'); window.location.reload(); } }); }); </script>
later can extract data in target page
$value1=$_post['occupationjoint2'];
after collecting field data this, insert database.
Comments
Post a Comment