如何添加更多的行表ajax

时间:2019-05-06 22:23:29

标签: php ajax

这是我所拥有的代码,用一个按钮可以搜索记录并将调用发送到文件ajax.php。这里的问题是我无法添加更多记录。我只能添加1,而我想添加更多作为购物车

<?php
      $page_title = 'Agregar venta';
      require_once('includes/load.php');
      // Checkin What level user has permission to view this page
       page_require_level(3);
    ?>
    <?php

      if(isset($_POST['add_sale'])){
        $req_fields = array('s_id','quantity','price','total', 'date' );
        validate_fields($req_fields);
            if(empty($errors)){
              $p_id      = $db->escape((int)$_POST['s_id']);
              $s_qty     = $db->escape((int)$_POST['quantity']);
              $s_total   = $db->escape($_POST['total']);
              $date      = $db->escape($_POST['date']);
              $s_date    = make_date();

              $sql  = "INSERT INTO sales (";
              $sql .= " product_id,qty,price,date";
              $sql .= ") VALUES (";
              $sql .= "'{$p_id}','{$s_qty}','{$s_total}','{$s_date}'";
              $sql .= ")";

                    if($db->query($sql)){
                      update_product_qty($s_qty,$p_id);
                      $session->msg('s',"Venta agregada ");
                      redirect('add_sale.php', false);
                    } else {
                      $session->msg('d','Lo siento, registro falló.');
                      redirect('add_sale.php', false);
                    }

            } else {
               $session->msg("d", $errors);
               redirect('add_sale.php',false);
            }

      }

    ?>
    <?php include_once('layouts/header.php'); ?>
    <div class="row">
      <div class="col-md-6">
        <?php echo display_msg($msg); ?>
        <form method="post" action="ajax.php" autocomplete="off" id="sug-form">
            <div class="form-group">
              <div class="input-group">
                <span class="input-group-btn">
                  <button type="submit" class="btn btn-primary">Búsqueda</button>
                </span>
                <input type="text" id="sug_input" class="form-control" name="title"  placeholder="Buscar por el nombre del producto">
             </div>
             <div id="result" class="list-group"></div>
            </div>
        </form>
      </div>
    </div>
    <div class="row">

      <div class="col-md-12">
        <div class="panel panel-default">
          <div class="panel-heading clearfix">
            <strong>
              <span class="glyphicon glyphicon-th"></span>
              <span>Editar venta</span>
           </strong>
          </div>
          <div class="panel-body">
            <form method="post" action="add_sale.php">
             <table class="table table-bordered">
               <thead>
                <th> Producto </th>
                <th> Precio </th>
                <th> Cantidad </th>
                <th> Total </th>
                <th> Agregado</th>
                <th> Acciones</th>
               </thead>
                 <tbody  id="product_info"> </tbody>
             </table>

            <button type="submit" name="add_sale" class="btn btn-primary">Generar Venta</button>
           </form>
          </div>
        </div>
      </div>

    </div>

    <?php include_once('layouts/footer.php'); ?>

这是ajax所在的位置,我想做的是我想继续添加而不删除已经拥有的记录。

Ajax.php

<?php
  require_once('includes/load.php');
  if (!$session->isUserLoggedIn(true)) { redirect('index.php', false);}
?>

<?php
 // Auto suggetion
    $html = '';
   if(isset($_POST['product_name']) && strlen($_POST['product_name']))
   {
     $products = find_product_by_title($_POST['product_name']);
     if($products){
        foreach ($products as $product):
           $html .= "<li class=\"list-group-item\">";
           $html .= $product['name'];
           $html .= "</li>";
         endforeach;
      } else {

        $html .= '<li onClick=\"fill(\''.addslashes().'\')\" class=\"list-group-item\">';
        $html .= 'No encontrado';
        $html .= "</li>";

      }

      echo json_encode($html);
   }
 ?>

 <?php
 // find all product
  if(isset($_POST['p_name']) && strlen($_POST['p_name']))
  {
    $qnt=1;
    $product_title = remove_junk($db->escape($_POST['p_name']));
    if($results = find_all_product_info_by_title($product_title)){
      $qnt++;
        foreach ($results as $result) {
          for($i = 0; $i < $qnt; $i++){
          $html .= "<tr>";
          $html .= "<td id=\"s_name\">".$result['name']."</td>";
          $html .= "<input type=\"hidden\" name=\"s_id\" value=\"{$result['id']}\">";
          $html  .= "<td>";
          $html  .= "<input type=\"text\" class=\"form-control\" name=\"price\" value=\"{$result['sale_price']}\">";
          $html  .= "</td>";
          $html .= "<td id=\"s_qty\">";
          $html .= "<input type=\"text\" class=\"form-control\" name=\"quantity\" value=\"1\">";
          $html  .= "</td>";
          $html  .= "<td>";
          $html  .= "<input type=\"text\" class=\"form-control\" name=\"total\" value=\"{$result['sale_price']}\">";
          $html  .= "</td>";
          $html  .= "<td>";
          $html  .= "<input type=\"date\" class=\"form-control datePicker\" name=\"date\" data-date data-date-format=\"yyyy-mm-dd\">";
          $html  .= "</td>";
          $html  .= "<td>";
          $html  .= "</td>";
          $html  .= "</tr>";
            }
        }
    } else {
        $html ='<tr><td>El producto no se encuentra registrado en la base de datos</td></tr>';
    }

    echo json_encode($html);
  }
 ?>

0 个答案:

没有答案