根据用户下拉选择PHP

时间:2016-10-05 17:04:29

标签: php jquery mysql ajax

伙计们我试图从两个表中加入数据。 我有一个选择选项下拉列表与面料名称,另一个有大小。 我想知道如何让我的下拉列表更改每个尺寸的每个面料的值,并乘以所选的面料数量。

这是我的布料表,tecido是Fabrics(这个表名是almofadas):

enter image description here

这是我的价格表,只是其中的一部分(此表名称为valores_almofadas): enter image description here

这是我的checkout.php用户界面: enter image description here

Lemme解释它更好......

首先,用户必须从下拉列表中选择一个结构名称,然后他必须选择大小,但它将取决于用户所处的状态 在sub.total中获得正确的值。

所有值都将根据用户选择的内容而改变。

用户会话包含用户所在的州。

icms_7适用于生活在我的州的用户,icms_12适用于局外人

因此,如果用户选择velvet大小的结构45cm并且他生活在我所居住的相同状态,则结构的值将为$ 39,60。< / p>

UI中的产品行是缓冲,用户从库中选择,这些行存储在会话中,这些会话位于foreach loop

这是我的cart.php(在这里我有产品会议)......

  //this part gets the cushion image from gallery.
  if(isset($_GET['add'])){
   $select = "SELECT * FROM gallery2 WHERE id=" .escape_string($_GET['add'])." ";
   $run_selection = mysqli_query($conn,$select);
   while($rows = mysqli_fetch_assoc($run_selection)){
   if($rows['id'] != $_SESSION['product_'.$_GET['add']]){
   $_SESSION['product_' . $_GET['add']]+=1;
        header('Location: ' . $_SERVER['HTTP_REFERER']);
        }else{
        header('Location: checkout.php');
        }
   }
   }

这是我在cart.php中的功能

function cart(){
    global $conn;

    $fabric_options = '';
    $query2 = "SELECT almofadas.A_id, almofadas.tecido, almofadas.id_price, valores_almofadas.id_price, valores_almofadas.icms_7, valores_almofadas.icms_12
    FROM almofadas 
    INNER JOIN valores_almofadas
    ON almofadas.id_price=valores_almofadas.id_price";
    $result = mysqli_query($conn,$query2);
    while($rows = mysqli_fetch_assoc($result)){
      $tecido=$rows['tecido'];
      $id_price=$rows['id_price'];
      $price=$rows['icms_7'];
      $fabric_options .= '<option value="'.$id_price.'">'.$tecido.'</option>';

    }

foreach ($_SESSION as $name => $value) {
     if($value > 0){
     if(substr($name, 0, 8 ) == "product_"){
     $length = strlen($name) -8;
     $item_id = substr($name,8 , $length);

     $query = "SELECT * 
               FROM gallery2 
               WHERE gallery2.id =".escape_string($item_id). "";
               $run_item = mysqli_query($conn,$query);
               while($rows = mysqli_fetch_assoc($run_item)){ 
                 $vari   = $rows['variante'];
                 $num    = $rows['title'];
                 $id     = $rows['id'];

  // these are my buttons                  
$btn_add ='<button type="button" class="btn btn-success actions plus" data-action="plus" product_id="'.$id.'"><i class="fa fa-plus fa-lg" aria-hidden="true" add_btn></i></button>';

$btn_remove ='<button type="button" class="btn btn-warning actions less" data-action="remove" product_id="'.$id.'"><i class="fa fa-minus fa-lg" aria-hidden="true" remove_btn></i></button>';

$btn_delete ='<button type="button" class="btn btn-default actions" data-action="delete" product_id="'.$id.'" onclick="deleteRow(this)"><i class="fa fa-times fa-lg" aria-hidden="true"></i></button>';

     if($rows['variante'] < 1){
        $vari="";
        }else{
        $vari = "-".$rows['variante'];
        }

  $product = '
   <tr>
   <td style="width:100px; "><img src="../'.$rows['image'].'" style="width:90%;border: 1px solid black;"></td>
   <td>'.$num.''.$vari.'</td>
   <td style="width:15%;">
     <select id="" class=" fabric select form-control selectpicker" required=""  >
    '. $fabric_options . '  
     </select>
    </td>

    <td>
     <select  id="size" class="select form-control selectpicker" required style="width:80%;" onchange="saveChoice()" >
       <option value="50">50x50</option>
       <option value="40">45x45</option>
     </select>
    </td>

    <td class="product'.$id.'">'.$value.'</td> // this is the amount of items for each row
    <td class="'.$id.'">R$:'.$price.'</td>
    <td>$ ' .$value * $price.'</td>
    <td> 
    '.$btn_add.' '.$btn_remove.' '.$btn_delete.'
    </td>
    </tr>';
           echo $product;  

             } 
          } 
       }
    }   
 }

这是我用来在btn上点击checkout.php进行更新更新的脚本

$('.actions').click(function(e){
  e.preventDefault();
  var action = $(this).attr('data-action'); //gets the button action
  var id = $(this).attr('product_id'); //id of the products
  console.log("triggered action " + action + " for product " + id); //debugging
  $.ajax({
    url: 'cart_functions.php',
    type : 'GET',
    data: {action:action,prod_id:id},
    dataType: 'json',
    success: function(data)
    {
      console.log("ajax call returned the following: " + JSON.stringify(data)); //debugging
      if (data.result == "success") {
        if (action == "plus" || action == "remove")
        {
          console.log("identified product td for " + id + ": " + $(".product" + id).length); //debugging
          $(".product" + id).text(data.val);//update the UI with the new total of each item
         //rest of the script

现在变得如此复杂,我在stackoverflow中使用ajax脚本提供了帮助。 那么我将如何根据用户选择的内容更改值?

0 个答案:

没有答案