Código
<?php require_once('../Models/product.php'); $product = new product(); session_start(); if (isset($_SESSION['login']) && $_SESSION['login']=='YES') {?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <link rel="stylesheet" href="../Resources/css/bootstrap.min.css"> <link rel="stylesheet" href="../Resources/css/main.css"> </head> <body> <!--Barra de Navegacion--> <nav class="navbar navbar-default frm-nav"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"> </button> </div> <ul class="nav navbar-nav navbar-right"> <li class="dropdrown"> <ul class="dropdown-menu" role="menu"> </ul> </ul> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <ul class="dropdown-menu" role="menu"> </ul> </li> </ul> <ul class="nav navbar-nav navbar-right"> </ul> </nav> <div class="col-md-3"> <ul class="nav nav-pills nav-stacked"> </ul> </div> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModallabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> </div> <div class="modal-body"> <form class="form-horizontal" id="formCliente"> <div class="form-group"> <div class="col-xs-4"> <input type="text" class="form-control" value="<?php echo date('y-m-d'); ?>" disabled="disabled"/> </div> </div> <div class="form-group"> <div class="col-xs-2 frm-code"> <input type="text" id="code" name="code" class="form-control" onkeyup="validacion('code');" aria-describedby="inputSuccess2Status"> </div> </div> <div class="form-group"> <div class="col-xs-4 frm-product"> <select class="form-control" id="product" name="product" onchange="validacion('product');"> </select> </div> </div> <div class="form-group"> <div class="col-xs-3 frm-price"> <input type="text" class="form-control" id="price" name="price" onkeyup="validacion('price');"> </div> </div> <div class="form-group"> <div class="col-xs-4"> <select class="form-control" id="stock" name="stock" onchange="validacion('stock')";> </select> </div> </div> </form> </div> <div class="form-group"> <div class="alert alert-success text-center" style="display:none;" id="success"> </div> <div class="alert alert-danger text-center" style="display:none;" id="error"> </div> </div> <div class="modal-footer"> </div> </div> </div> </div> <div class="container"> <div class="tab-content nav col-md-8 frm-grid"> <div class="form-group col-xs-3 frm-search"> <div class="form-group has-feedback"> <input type="text" class="form-control" id="inputValidation" placeholder="Search"/> </div> </div> <table class="table table-hover table-resposive" id="return_product"> <thead> <tr> </tr> </thead> <tbody id="records"> <?php $product->show_product(); ?> </tbody> </table> </div> </div> <script> function cerrar() { $.ajax({ url:'../Controllers/actions.php', type:'POST', data:"boton=cerrar" }).done(function(resp){ location.href = '../Views/' }); } function add_product(){ var code = $('#code').val(); var product = $('#product').val(); var price = $('#price').val(); var stock = $('#stock').val(); var valCheck = verificar(); if(valCheck == true){ $.ajax({ url: '../Controllers/actions.php', type: 'POST', data: 'code='+code+'&product='+product+'&price='+price+'&stock='+stock+'&boton=add_product' }).done(function(ans){ if(ans == 'success'){ $('#code').val(""); $('#product').val('reset_product'); $('#price').val(""); $('#stock').val('rest_stock'); $('#success').show().delay(2000).fadeOut(); }else{ alert(ans); } }) } else { } } function delete(id){ $.ajax({ url:'../Controllers/product.php', type:'POST', data:'code='+id+'&boton=delete' }).done(function(ans){ alert(ans); }); } $(document).ready(function(){ $('#add').click(function(){ $.ajax({ url: '../Controllers/actions.php', type: 'POST', data: 'boton=show_product' }).done(function(data){ $('#records').html(data); }) }); }); </script> </body> </html> <?php } else { header("location: ./"); } ?>
Código
<?php class product{ private $conexion; public function __construct(){ require_once('conexion.php'); $this->conexion = new conexion(); $this->conexion->conectar(); } function add($price,$stock,$code,$product){ $sql1 ="INSERT INTO product (price,stock,code,product) VALUES('$price','$stock','$code','$product')"; if($this->conexion->conexion->query($sql1)){ return true; } else{ return false; } $this->conexion->cerrar(); } function delete($code){ $sql = "DELETE FROM product WHERE id_product='$code'"; if($this->conexion->conexion->query($sql)){ return true; } else{ return false; } $this->conexion->cerrar(); } function show_product(){ $sql = "SELECT * FROM product"; $record = $this->conexion->conexion->query($sql); $item =0; if(!$this->conexion->conexion->query($sql)){ } if($record->num_rows>0){ while($show = $record->fetch_array(MYSQLI_ASSOC)){ $item += 1; echo '<tr> <td>'.$item.'</td> <td>'.$show["code"].'</td> <td>'.$show["product"].'</td> <td>'.$show["price"].'</td> <td>'.$show["stock"].'</td> <td><button class="btn btn-danger" onclick="delete();"><span class="glyphicon glyphicon-trash"></span></button></td> </tr>'; } //this is for counting number of page } else{ } } } ?>