jquery - Issues with submitting edited form using ajax and PHP -
when tried make update first_name , last_name database, empty form passed database result no information updated. please need help?
here work far.... index.php
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> </head> <body> <div class="container"> <div id="disp_data"></div> </div> </body> </html> <script> $(document).ready(function(){ function fetch_data() { $.ajax({ url:"select.php", method:"post", success:function(data){ $('#disp_data').html(data); } }); } fetch_data(); //edit $(document).on('click', '#edit', function(){ var id=$(this).data("id4"); //var first_name = $('#first_name').text(); //var last_name = $('#last_name').text(); var first_name = $('#first_name').val(); var last_name = $('#last_name').val(); if(id == '') { alert("id assigned automatically.."); return false; } if(first_name == '') { alert("enter first name"); return false; } if(last_name == '') { alert("enter last name"); return false; } if(confirm("are sure want edit this?")) { $.ajax({ url:"edit.php", method:"post", data:{id:id}, datatype:"text", success:function(data){ if(data==1) { alert('data edited ....!'); } fetch_data(); } }); } }); }); </script>
select.php
<?php $db = new pdo ( 'mysql:host=localhost;dbname=db_name;charset=utf8', 'root', // username '' // password ); $output = ''; $result = $db->prepare('select * empinfo order id desc'); $result->execute(array()); $count = $result->rowcount(); $output .= ' <div align="center"> <table border="1" bordercolor="#00cccc"> <tr> <th width="10%">id</th> <th width="40%">first name</th> <th width="40%">last name</th> </tr>'; if($count > 0) { while($row = $result->fetch()){ $output .= ' <tr> <td>'.$row["id"].'</td> <td class="first_name" first_name="'.$row["first_name"].'" contenteditable>'.$row["first_name"].'</td> <td class="last_name" first_name="'.$row["last_name"].'" contenteditable>'.$row["last_name"].'</td> <td><button type="button" name="edit" data-id4="'.$row["id"].'" id="edit">edit</button></td> </tr> '; } $output .= ' <tr> <td></td> <td id="first_name" contenteditable></td> <td id="last_name" contenteditable></td> </tr> '; } else { $output .= '<tr> <td colspan="4">data not found</td> </tr>'; } $output .= '</table> </div>'; echo $output; ?>
edit.php
<?php error_reporting(0); $db = new pdo ( 'mysql:host=localhost;dbname=chat;charset=utf8', 'root', // username '' // password ); $id = $_post["id"]; $fn = $_post["first_name"]; $ln = $_post["last_name"]; $update1 = $db->prepare(' update empinfo set first_name = :first_name,last_name=:last_name id= :id'); if($update1->execute(array( ':first_name' => $fn,':last_name' => $ln, ':id' => $id))){ echo 'data updated'; } ?>
i see in edit.php
trying firstname
, lastname
values withous sending them js part result no information in edit.php
suggest send them ajax
request can them :
if(confirm("are sure want edit this?")) { $.ajax({ url:"edit.php", method:"post", data:{"id":id,"first_name":first_name,"last_name":last_name}, datatype:"text", success:function(data){ if(data==1) { alert('data edited ....!'); }
so have sent desired values ajax request , can them in edit.php
typing $fn = $_post["first_name"];
hope helpful
Comments
Post a Comment