php - AJAX call not working dynamically? -
i have following script:
$(document).ready(function(){ $('#additem').click(function(){ $.ajax({ type: 'post', url: 'php/additem.php', data: {itemname: $('#itemname').val()}, data: {pricetotal: $('#price').val()}, data: {description: $('#description').val()}, success: function(data) { can call separate ajax query here? want repull 2 tables data resulting itemadd seems dynamic. } }); }); });
here html:
<div class="col-sm-4 col-md-4"> <div class="content-boxes style-two top-column clearfix animated flipiny" style="opacity: 1;"> <div class="content-boxes-text"> <form action="php/additem.php" method="post" class="form-inline pull-right"> <h4>'.$row['itemname'].'</h4><input id="itemname" type="hidden" name="itemname" value="'.$row['itemname'].'"> <h3>$'.$price.'</h3><input id="price" type="hidden" name="pricetotal" value="'.$price.'"> <img src="../wholesale/img/sourdough.jpg" class="img-reponsive"> <p>'.$row['description'].'</p><input id="description" type="hidden" name="description" value="'.$row['description'].'"> <div class="form-group"> <label class="sr-only" for="exampleinputamount">qty</label> <div class="input-group"> <input type="number" name="qty" class="form-control" id="qtyitem" placeholder="how many?"> </div> </div> <button type="submit" id="additem" class="btn btn-primary">add</button> </form> </div> <!-- //.content-boxes-text --> </div> <!-- //.content-boxes --> </div>
the problem when submit form page reloads normal query. page should not refresh , item adding should happen dynamically.
i having trouble idea: initial loading pulls data 2 tables, best recall initial ajax query after have added item?
please give me reasoning write can learn this. thank all.
session_start(); include('db_config.php'); $date = date("y-m-d"); $itemname = $_post['itemname']; $description = $_post['description']; $qty = $_post['qty']; $price = $_post['pricetotal'] * $qty; $id = $_session['customer_id']; $sql = "insert orders (deliverydate, customerid, itemname, qty, price) values (?, ?, ?, ?, ?)"; $stmt = $conn->prepare($sql); $stmt->execute(array($date, $id, $itemname, $qty, $price)); header('location: ../order.php');
js script
<script type="text/javascript"> $(document).ready(function(){ $('.form-inline').on("submit",function(event){ event.preventdefault(); $.ajax({ type: 'post', url: 'php/additem.php', data: { itemname: $('#itemname').val(), pricetotal: $('#price').val(), description: $('#description').val(), qty: $('#qtyitem').val() }, success: function(data) { alert(data); } }); }); }); </script>
additem button submit button performs default operation
to avoid page reload
use : event.preventdefault();
change code
$(document).ready(function(){ $('.form-inline').on("submit",function(event){ event.preventdefault(); $.ajax({ type: 'post', url: 'php/additem.php', data: { itemname: $('#itemname').val(), pricetotal: $('#price').val(), description: $('#description').val() }, success: function(data) { // perform action } }); }); });
Comments
Post a Comment