php - 显示每条生产线的产品描述成本

标签 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>"; ?>

最佳答案

您不需要使用变量类名。

只需使用 <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 变量是一个容器。

关于php - 显示每条生产线的产品描述成本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26999176/

相关文章:

javascript - 如何使用 PHP 在网站中添加 Instagram feed?

javascript - 通过 OnClick 按钮插入 MySQL

php - 在 Codeigniter 中选择并加入同一个表

php - 我可以获得特定时区的时间戳吗?

php - 获取连字符第 n 次出现前后的文本

javascript - 如何在jquery中循环遍历ID

javascript - 在 jquery 中取消提交?

javascript - 用js处理mailchimp的ajax查询

MySQL 错误 1064 NOT NULL 默认值

PHP + MySql 带类别且无偏移的分页