Привет, чтобы сделать ту же кнопку для вставки и обновления
config.php
<?php
$conn = mysqli_connect('localhost', 'root', ", 'parth');
?>
records.php
<?php include('config.php'); ?> <?php if(isset($_POST['submit'])) { $name = $_POST['name']; $email = $_POST['mail']; $gen = $_POST['gender']; $age = $_POST['age']; //$hob =implode(",",$_POST['hobbies']); $hob = implode(",",$_POST['hobbies']); $pass = $_POST['pass']; $cpass = $_POST['cpass']; $query = mysqli_query($conn, "INSERT INTO form(name, email, gender, age, hobbies, pass, cpass)VALUES('".$name."', '".$email."', '".$gen."', '".$age."', '".$hob."', '".$pass."', '".$cpass."') "); if($query) { echo "Insert"; } else { echo "Fail"; } } if(isset($_POST['uid'])) { $id = $_POST['uid']; $name = $_POST['name']; $email = $_POST['mail']; $gen = $_POST['gender']; $age = $_POST['age']; //$hob =implode(",",$_POST['hobbies']); //$hob = implode(",",$_POST['hobbies']); $hob = implode(",",$_POST['hobbies']); $pass = $_POST['pass']; $cpass = $_POST['cpass']; $query = mysqli_query($conn, "UPDATE form SET name = '".$name."', email = '".$email."', gender = '".$gen."', age = '".$age."', hobbies = '".$hob."', pass = '".$pass."', cpass = '".$cpass."' WHERE my_id = '".$id."' "); if($query) { echo "Update"; } else { echo "Fail"; } } $id = $_GET['id']; $query = mysqli_query($conn, "DELETE FROM form WHERE my_id = '".$id."'"); header("Location:view.php"); ?>
update.php
<?php include('config.php'); ?> <!DOCTYPE html> <html> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $('#update').click(function(){ var u = $('#uid').val(); var nm = $('#name').val(); var em = $('#mail').val(); // var gen = $('#gender').val(); var gen = $("input[name='gender']:checked").val(); var ag = $('#age').val(); //var hb = $('#hob').val(); var hb = []; $.each($("input[name='hobbies']:checked"), function(){ hb.push($(this).val()); }); // alert("My favourite sports are: " + hb.join(", ")); var p = $('#pass').val(); var cp = $('#cpass').val(); $('.error').remove(); var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; var validEmail = reg.test(em); if(u.length < 1){ $('#uid').after('<span class="error">Update Id is required</span>'); return false; } else if(nm.length < 1){ $('#name').after('<span class="error">This field is required</span>'); return false; } else if(em.length < 1){ $('#mail').after('<span class="error">This field is required</span>'); return false; } else if(!validEmail){ $('#mail').after('<span class="error">Enter a valid email</span>'); return false; } if(!gen){ $('.gender').after('<span class="error">This field is required</span>'); return false; } else if(ag.length == ""){ $('#age').after('<span class="error">This field is required</span>'); return false; } else if(hb.length < 1){ $('.hob').after('<span class="error">This field is required</span>'); return false; } else if(p.length < 1){ $('#pass').after('<span class="error">This field is required</span>'); return false; } else if(cp.length < 1){ $('#cpass').after('<span class="error">This field is required</span>'); return false; } else if (p != cp) { $('#cpass').after('<span class="error">Password not match.</span>'); return false; } else console.log("Starting ajax"); $.ajax({ url: "./records.php", type: "post", data: { uid:u, name:nm, mail:em, gender:gen, age:ag, hobbies:hb, pass:p, cpass:cp }, success:function(data) { alert('done'); } }); }); }); </script> </head> <body> <?php if(isset($_GET['id'])) { $id = $_GET['id']; $query = mysqli_query($conn, "SELECT * FROM form WHERE my_id = '".$id."' "); $q = mysqli_fetch_array($query); } ?> <a href="view.php">View</a> <form> <table> <tr> <td>Name</td> <td><input type="text" name="name" id="name" value="<?= $q['name']; ?>"></td> </tr> <tr> <td>Email</td> <td><input type="text" name="mail" id="mail" value="<?= $q['email']; ?>"></td> </tr> <tr> <td>Gender</td> <td>Male<input type="radio" name="gender" id="gender" class="gender" value="male" <?php if($q['gender']=="male"){ echo "checked";}?>> Female<input type="radio" name="gender" id="gender1" class="gender" value="female" <?php if($q['gender']=="female"){ echo "checked";}?>></td> </tr> <tr> <td>Age </td> <td> <select name="age" id= "age" value="<?= $q['age']; ?>"> <option value="<?= $q['age']; ?>"><?= $q['age']; ?></option> <?php for ($i=1; $i<=100; $i++) { ?> <option><?php echo $i;?></option> <?php } ?> </select> </td> </tr> <tr> <td>Hobbies</td> <td> <?php $hobby=explode(",",$q['hobbies']); ?> <input type="checkbox" name="hobbies" value="chess"<?php if(in_array("chess",$hobby)) { ?> checked="checked" <?php } ?> class="hob" id="hob">Chess <input type="checkbox" name="hobbies" value="cricket" <?php if(in_array("cricket",$hobby)) { ?> checked="checked" <?php } ?> class="hob" id="hob1">Cricket <input type="checkbox" name="hobbies" value="football" <?php if(in_array("football",$hobby)) { ?> checked="checked" <?php } ?> class="hob" id="hob2">Football <input type="checkbox" name="hobbies" value="hockey" <?php if(in_array("hockey",$hobby)) { ?> checked="checked" <?php } ?> class="hob" id="hob3">Hockey </td> </tr> <tr> <td>Password</td> <td><input type="password" name="pass" id="pass" value="<?= $q['pass']; ?>"></td> </tr> <tr> <td>Confirm Password</td> <td><input type="password" name="cpass" id="cpass" value="<?= $q['cpass']; ?>"></td> </tr> <tr> <td><input type="hidden" name="uid" id="uid" value="<?= $id ?>"></td> <!-- <td><input type="submit" name="update" id="update" value="update"></td> --> <td><button type="button" name="update" id="update">Update</button></td> <!-- <?php // if(isset($_GET['id']) > 1) { ?> <input type = "submit" class = "btn btn-primary" style="width:49%" value = "Save" name = "submit"> <?php // } else { ?> <input type = "submit" class = "btn btn-primary" style="width:49%" value = "Update" name = "submit"> <?php } ?> --> </tr> </table> </form> </body> </html>
index.php
<?php include('config.php'); ?> <!DOCTYPE html> <html> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $('#submit').click(function(){ var s = $('#submit').val(); var nm = $('#name').val(); var em = $('#mail').val(); // var gen = $('#gender').val(); var gen = $("input[name='gender']:checked").val(); var ag = $('#age').val(); //var hb = $('#hob').val(); var hb = []; $.each($("input[name='hobbies']:checked"), function(){ hb.push($(this).val()); }); // alert("My favourite sports are: " + hb.join(", ")); var p = $('#pass').val(); var cp = $('#cpass').val(); $('.error').remove(); var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; var validEmail = reg.test(em); if(nm.length < 1){ $('#name').after('<span class="error">This field is required</span>'); return false; } else if(em.length < 1){ $('#mail').after('<span class="error">This field is required</span>'); return false; } else if(!validEmail){ $('#mail').after('<span class="error">Enter a valid email</span>'); return false; } if(!gen){ $('.gender').after('<span class="error">This field is required</span>'); return false; } else if(ag.length == ""){ $('#age').after('<span class="error">This field is required</span>'); return false; } else if(hb.length < 1){ $('.hob').after('<span class="error">This field is required</span>'); return false; } else if(p.length < 1){ $('#pass').after('<span class="error">This field is required</span>'); return false; } else if(cp.length < 1){ $('#cpass').after('<span class="error">This field is required</span>'); return false; } else if (p != cp) { $('#cpass').after('<span class="error">Password not match.</span>'); return false; } else console.log("Starting ajax"); $.ajax({ url: "./records.php", type: "post", data: { submit:s, name:nm, mail:em, gender:gen, age:ag, hobbies:hb, pass:p, cpass:cp }, success:function(data) { alert('done'); } }); }); }); </script> </head> <body> <a href="view.php">View</a> <?php if(isset($_GET['id'])) { $id = $_GET['id']; $query = mysqli_query($conn, "SELECT FROM form WHERE my_id = '".$id."' "); $q = mysqli_fetch_array($query); } ?> <form> <table> <tr> <td>Name</td> <td><input type="text" name="name" id="name"></td> </tr> <tr> <td>Email</td> <td><input type="text" name="mail" id="mail"></td> </tr> <tr> <td>Gender</td> <td>Male<input type="radio" name="gender" class="gender" id="gender" value="male"> Female<input type="radio" name="gender" class="gender" id="gender1" value="female"></td> </tr> <tr> <td>Age </td> <td> <select name="age" id= "age"> <option value="">selet age</option> <?php for ($i=1; $i<=100; $i++) { ?> <option><?php echo $i;?></option> <?php } ?> </select> </td> </tr> <tr> <td>Hobbies</td> <td> <input type="checkbox" name="hobbies" class="hob" value="chess" id="hob">Chess <input type="checkbox" name="hobbies" class="hob" value="cricket" id="hob1">Cricket <input type="checkbox" name="hobbies" class="hob" value="football" id="hob2">Football <input type="checkbox" name="hobbies" class="hob" value="hockey" id="hob3">Hockey </td> </tr> <tr> <td>Password</td> <td><input type="password" name="pass" id="pass"></td> </tr> <tr> <td>Confirm Password</td> <td><input type="password" name="cpass" id="cpass"></td> </tr> <tr> <td></td> <!-- <td><input type="submit" name="submit" id="submit"></td> --> <td><button type="button" name="submit" id="submit">Submit</button></td> <!-- <?php // if(isset($_GET['id']) > 1) { ?> <input type = "submit" class = "btn btn-primary" style="width:49%" value = "Save" name = "submit"> <?php // } else { ?> <input type = "submit" class = "btn btn-primary" style="width:49%" value = "Update" name = "submit"> <?php } ?> --> </tr> </table> </form> </body> </html>
add.html
<!DOCTYPE html> <html> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $('#submit').click(function(){ var s = $('#submit').val(); var nm = $('#name').val(); var ml = $('#mail').val(); var ps = $('#pass').val(); $('.error').remove(); var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; var validEmail = reg.test(ml); if(nm.length < 1) { $('#name').after('<span class="error">This field is required</span>'); } else if(ml.length < 1) { $('#mail').after('<span class="error">This field is required</span>'); } else if(!validEmail) { $('#mail').after('<span class="error">Enter a valid email</span>'); } else if(ps.length < 1) { $('#pass').after('<span class="error">This field is required</span>'); } else { $.ajax({ url: "./index.php", type:"post", data: { submit:s, name:nm, email:ml, pass:ps }, success:function(data) { alert('done'); } }); } }); }); </script> </head> <body> <a href="view.php">view</a> <form> <table> <tr> <td>Name</td> <td><input type="text" name="name" id="name"></td> </tr> <tr> <td>Email</td> <td><input type="text" name="mail" id="mail"></td> </tr> <tr> <td>Password</td> <td><input type="text" name="pass" id="pass"></td> </tr> <tr> <td></td> <td><button type="button" name="submit" id="submit" class="btn">Submit</button></td> </tr> </table> </form> </body> </html>
view.php
<?php include('config.php'); ?> <!DOCTYPE html> <html> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $('.del').click(function(){ var d = $(this).data('id'); if(confirm('Are you sure you want to delete ?')) { if(d== '') { alert('This user cant be deleted'); return false; } else { $.ajax({ url: "./records.php?id="+d, type: "post", data: { del:d }, success:function(data) { $('body').html(data); } }); } } }); }); </script> </head> <body> <a href="index.php">Add</a> <?php $query = mysqli_query($conn, "SELECT * FROM form"); ?> <table> <tr> <td>Name</td> <td>Email</td> <td>Gender</td> <td>Age</td> <td>Hobbies</td> <td>Password</td> <td>Confitm Password</td> <td>Action</td> </tr> <?php while($q = mysqli_fetch_array($query)) { ?> <tr> <td><?= $q['name'] ?></td> <td><?= $q['email'] ?></td> <td><?= $q['gender'] ?></td> <td><?= $q['age'] ?></td> <td><?= $q['hobbies'] ?></td> <td><?= $q['pass'] ?></td> <td><?= $q['cpass'] ?></td> <td><a href="update.php?id=<?= $q['my_id']; ?>">Update</a> | <a href="#!" class="del" data-id="<?= $q['my_id']; ?>">Delete</a></td> </tr> <?php } ?> </table> </body> </html>
How to make same button for insert and update and make single form with php jquery ajax
Что я уже пробовал:
Как сделать одну и ту же кнопку для вставки и обновления и сделать единую форму с php jquery ajax