显示产品描述的成本以适用于每一行

时间:2014-11-18 16:28:58

标签: php jquery mysql

我是初学者,我正试图从MYSQL数据库中获取成本来处理每一行。我创建了两列费用来展示我的尝试。到目前为止,我可以获得成本,但我只能使用class = "<?php echo $line_item; ?>"让它在最后一行工作我也尝试使用"class = results"显示正确的成本,但它显示在每一行。我知道这看起来像一团糟但是非常感谢任何帮助

<?php
include '****';
$order_num = $_REQUEST['order_n'];

$result = mysql_query("SELECT * from items where order_n=$order_num"); ?>

<script src="javascripts/jquery.js" type="text/javascript"></script>
<script src="javascripts/jquery.dataTables.js" type="text/javascript"></script>

<table id='datatables' class='display' cellspacing='1'  table width = '1200'  border = '5'' div >

 <thead>
 <tr>                 
      <th><div align='left'><span class='style7'>Line</span></div></th> 
      <th><div align='left'><span class='style7'>Order #</span></div></th>
      <th><div align='left'><span class='style7'>Choose Product</span></div></th>           
      <th><div align='left'><span class='style7'>cost</span></div></th>
      <th><div align='left'><span class='style7'>cost</span></div></th>      
  </tr>
  </thead>

  <?php
   while($row = mysql_fetch_assoc($result)){
    $line_item=$row['line_item']; ?>  
    <td><span class="text"><?=$row['line_item']?></td>  
    <td><span class="text"><?=$row['order_n']?></td>
    <td>
        <form action="">
            <select name="Description"  id="<?php echo $line_item; ?>" c
     class = "descrip_1"
                <option value="">Select a Item:</option>
                <option value="2x8-08 SYP #2">2x8-08 SYP #2</option>
                <option value="3/4 Galvanized Nut">3/4 Galvanized Nut</option>
            </select>
        </form>
    </td>
    <td width=60px><label class="results"></label></td>
    <td width=60px><label class="<?php echo $line_item; ?>"></label></td>    
</tr><?php
            } ?>
</tbody>
</table>
</div>
</body>
</html>

<script type="text/javascript">

 $(function() {
  function_1();
   $('.descrip_1').on('change', function() {
    function_2(this);
    });
 });

 function function_2(elem) {
 var $container = $(elem).parent().parent();
 var descrip_2= $container.find('.descrip_1').val();

   $.ajax({
            type: 'POST',
            data: ({p : descrip_2}),  
            url: 'cost.php',
            success: function(data) {
            // Using "results will get cost on every row,
            // but the cost is the same on each row.
            // Should be different depending on line number
            $('.results').html(data);
            //Using php echo $line_item will work only one line.
            $('.<?php echo $line_item; ?>').html(data);             }
       });
  }

 function function_1(){
    }

  </script>

PHP cost.php文件获取费用

 <?php
 include("*****");

 $name=$_POST['p'];

 $sql = mysql_query("SELECT * FROM types WHERE Description = '".$name."'");
 $row = mysql_fetch_array($sql);

echo "<table border='0' cellspacing='0'>
<th><font color='black'>".$row['Cost']."</th>
</table>"; ?>

2 个答案:

答案 0 :(得分:0)

您不需要使用变量类名。

只需使用<td width=60px><label class="line_item"></label></td>,然后

$container.find(".line_item").html(data);

在你的jquery成功。

如果这不是您想要的,请告诉我,您的问题有点令人困惑(您希望所有行都更新还是只更改了行?这应该只更改已更改的行)。

修改

好的,请使用$(elem).closest("tr").find(".line_item").html(data);

这是一个与相应的html松散耦合的解决方案。我错误地认为$ container变量是一个容器。

答案 1 :(得分:0)

Josh DM回答了我的问题,现在有效了。这就是我做的事情

HTML

    <td width=60px><label class="line_item"></label></td>

的javascript

<script type="text/javascript">

  $(function() {
   function_1();


    $('.descrip_1').on('change', function() {

       function_2(this);

       });  
      });

      function function_2(elem) {

// This will give the tr of the Element Which was changed



     var $container = $(elem).parent().parent();


   var descrip_2= $container.find('.descrip_1').val();


 $.ajax({
            type: 'POST',
            data: ({p : descrip_2}),    
            url: 'listercust.php',
            success: function(data) {



 $(elem).closest("tr").find(".line_item").html(data);


 }


    });

     }

   function function_1(){     

   }

  </script>