简体   繁体   中英

Simple ordering system

I am trying to do a simple ordering system.in which the services are already in the database but the name if the person/s who did the service is/are manually entered. i am able to successfully save the order but the name of the person attended the costumer but i am having trouble if i do have multiple services in one appointment it is only saving the first name that i entered the name after that is not saving into my database. Here is the function that i am working out

function ConfirmOrder(){
  var orders = {}; 


  if($('.food').length>0){
    var checkbox = 0;

    $('tr.item').each(function(){
      var order_id = $(this).attr('id');
      var quantity1 = $(this).find('.quantity').text();
      var name = $('#name').val();


      orders[order_id] = quantity1;

      })

   console.log(orders);


      if ($('#senior-checkbox').prop('checked')) {
        checkbox = 1;
      }
      else{
        checkbox = 0;
      }

   }

  else{
    alert("No orders taken");

  }
    $.ajax({
      url: "confirm_order.php",
      type: "POST",
      async: true,
      cache: false,
      data: {orders:orders,checkbox:checkbox,name},

      success: function(html){

     }
   })

This how i select the services and entered the name

<?php
include 'dbconnection.php';
$menu_id = $_POST['menu_id'];
$query = mysql_query("SELECT * FROM menu WHERE menu_id=$menu_id");
echo "SELECT * FROM menu WHERE menu_id=$menu_id";

while ($row = mysql_fetch_array($query)){

?>
<tr id="<?php echo $row['menu_id'];?>" class="item">
    <td class="food noprint" id="<?php echo $row['menu_id'];?>">
        <input name="checkbox[]" type="checkbox" value="<?php echo $row['menu_id'];?>">

    </td>
    <td><?php echo $row['dish'];?></td>
    <td class="food_price_column" id="<?php echo $row['menu_id'];?>">


<span>
    <span class="btn btn-success noprint" onclick="minusQuantity(<?php echo $row['menu_id'];?>, <?php echo $row['price']; ?>)"><i class="fa fa-minus"></i></span>

&nbsp;&nbsp;&nbsp;&nbsp; <span class="quantity order-quantity<?php echo $row['menu_id'];?>">1</span> 

    &nbsp;&nbsp;&nbsp;&nbsp; <span class="btn btn-success noprint" onclick="addQuantity(<?php echo $row['menu_id'];?>, <?php echo $row['price']; ?>)"><i class="fa fa-plus"></i></span></span>

    </td>
    <td><?php echo $row['price'];?></td>
    <td class="price" id="<?php echo $row['menu_id'];?>"><?php echo $row['price'];?></td>
    <td><input type="text" name="name" id="name"></td>
    </td></tr>

<?php } ?>

This is how i save it the the database

<?php
include 'dbconnection.php';
session_start();

$user_id = $_SESSION['user_id'];

if(isset($_POST['orders'])){
    $checkbox = $_POST['checkbox'];
    $order_id = $_POST['orders'];
    $fullname= "";
    $last_ids = [];
    $name = $_POST['name'];



    //alert($name);

    //Query the cashier's name to  be inserted to sales table
    $user_query = mysql_query("SELECT full_name FROM account WHERE acct_id = $user_id");
    while($row = mysql_fetch_array($user_query)){
        $fullname = $row['full_name'];
    }

    //insert orders into orders table
        $insert = mysql_query("INSERT INTO sales VALUES ('','$fullname',now(),$checkbox)");

        if($insert){
            $last_id = mysql_insert_id();
        }
    foreach($order_id as $key => $value) { //$key = dish_id and $value = quantity


        $dish_query = mysql_query("SELECT dish FROM menu WHERE menu_id = $key");
        while($dish = mysql_fetch_array($dish_query)){

            $dish = $dish['dish'];
            /*$name = $_POST['name'];*/
            $order = mysql_query("INSERT INTO orders VALUES ('', '$dish','$value','$last_id','$name')");

            $query1 = mysql_query("INSERT into a_logs(id,name,activity,a_date) values(0,'$fullname','Added $dish',NOW())");


            $recent_order_id = mysql_insert_id();

        }       
    }
}

?>

here is the the part of code you need to check out:

    </td>
    <td><?php echo $row['price'];?></td>
    <td class="price" id="<?php echo $row['menu_id'];?>"><?php echo $row['price'];?></td>
    <td><input type="text" name="name" id="name"></td>
    </td>
    </tr>

as you are creating the input tag using while loop every tag will have same name and id. So when you fetch the data using id it will only give you only one value every time.

you could use following: to create array of values and access them

while(-->condition<--){
</td>
        <td><?php echo $row['price'];?></td>
        <td class="price" id="<?php echo $row['menu_id'];?>"><?php echo $row['price'];?></td>
        <td><input type="text" name="name[]" ></td>
        </td>
        </tr>
}

to access values using php:

$name = $_POST['name'];

foreach( $name as $v ) {
print $v;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM