Javascript 我必须从数据库中输出我的数据,还必须复制表单中的所有输入,将它们发送给我自己,包括实时乘以重量*PC

Javascript 我必须从数据库中输出我的数据,还必须复制表单中的所有输入,将它们发送给我自己,包括实时乘以重量*PC,javascript,php,html,Javascript,Php,Html,我有一些代码,我必须从客户端输入我的联系人数据,并从MySQL数据库输出数据,该数据库在表中创建新行 此外,代码中还必须包含其他函数,但我不知道如何处理这个问题。我必须实时计算元素块的数量x它们的重量,以向客户显示订单的总重量。 问题是,我不知道如何让客户在更改中片段的值时看到它 最后,表单和表格中的数据必须通过电子邮件发送给员工 PHP代码: <html> <head> <meta charset="utf-8" />

我有一些代码,我必须从客户端输入我的联系人数据,并从MySQL数据库输出数据,该数据库在表中创建新行

此外,代码中还必须包含其他函数,但我不知道如何处理这个问题。我必须实时计算元素块的数量x它们的重量,以向客户显示订单的总重量。

问题是,我不知道如何让客户在更改
片段的值时看到它

最后,表单和表格中的数据必须通过电子邮件发送给员工

PHP代码:

 <html>
  <head>
    <meta charset="utf-8" />
    <meta name="calculator" content="width=device-width, initial-scale=1"/>
        <title>Test</title>
        <link rel="stylesheet" href="style.css" >
  </head>

    <body>
        <!-- including calc.php file which contains all variables -->
        <?php include 'calc.php'; ?>
        <!-- Printing the form -->
    <form method="post" action="sendform.php"><br>
    
     <center>
     <h1>TEST</h1>
        <input id="Name" type="text" placeholder="<?= $form['FIRMA']; ?>" class="form-contact" required><br>
         <input id="Adres" type="text" placeholder="<?= $form['ADRES']; ?>" class="form-contact" required><br>
          <input id="Email" type="email" placeholder="<?= $form['EMAIL']; ?>" class="form-contact" required><br>
           <input id="Country" type="text" placeholder="<?= $form['COUNTRY']; ?>" class="form-contact" value="" size="1" pattern="[0-9]{2}" maxlength="2" required>
            <input id="Phone" type="text" placeholder="<?= $form['PHONE']; ?>" class="form-contact" pattern="[0-9]{9}" maxlength="9" required>
            <br>
            
        <!-- Printing out the table -->
        
  <table class="table-responsive" border="1">
    <thead>
        <tr>
            <th><?= $form['PRODUCTS']; ?></th>
            <th><?= $form['CATALOG']; ?></th>
            <th><?= $form['DESC']; ?></th>
            <th><?= $form['WEIGHT']; ?></th>
            <th><?= $form['TWEIGHT']; ?></th>
        <br>
        </tr>

    </thead>
      <tbody>
      
      <?php
            # Database connection
                $dbhost = "localhost";
                $dbuser = "root";
                $dbpass = "1234";
                $dbname = "data";

                $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
                if ($connection->connect_error) {
                    die("Connection failed: " . $connection->connect_error);
                }
                #echo "Connection successfull!";
                
                # Questions for Database
                $sql = 'SELECT * FROM `elements`';
                $result = mysqli_query($connection, $sql);
                
                    if (mysqli_num_rows($result) > 0) {
                        //output data each row
                        while($row = mysqli_fetch_assoc($result)) {
                            echo "<tr><td>".'<input class="pcs-input" min=0 maxlength="3" value=0 size="2px" style="background-color: white;" name="inputs[]">';
                            echo "</td><td>Nr. "  . $row["pcode"] . "</td><td> " . $row["fullname"]. "</td><td> " . $row["weight"] . "kg</td><td><div id='sumWeight'></div></td></tr>";
                            
                        }
                        } else {
                        echo "0 Results";
                    }

                mysqli_close($connection);
      ?>

      </tbody>
    </table>
        <!-- Counting all needed atributes -->
            <div id="totalWeight">totalWeight</div>
        <!-- Submit btn -->
        <br><button name="submit" type="submit" value="Send" class="form-button">Send</button>
        </center>
    </form>
        
    </body>
    
  </html>

试验

试验

总的来说,您的代码有很多问题,可以概括为

  • 无效的HTML(例如重复的ID、错误写入的
    input
    元素、未关闭的表行、未关闭的数据属性)
  • 过于复杂的HTML
  • 键入错误或大小写错误(例如,
    W
    而不是
    W
  • JavaScript中的CSS选择器不正确
  • 缺少初始默认数据(例如,如果任何一个输入框中没有数字,计算将失败,因为尝试将空白值添加到另一个数字会导致
    NaN
    (不是数字)
  • 页面加载时未运行计算,因此在用户更改某些内容之前,数据将丢失(如果用户希望在更改任何值之前查看当前权重,这是没有用的)
此客户端演示演示了让PHP生成所需的HTML,以及执行计算和显示结果所需的正确JavaScript/jQuery代码:

$(函数(){
$(“.pcs输入”).change(calculateForm);//设置事件处理程序
函数calculateForm(){
var总权重=0;
$(“.pcs输入”)。每个(函数(){
var pcs=parseInt($(this.val());
如果(pcs<0){
pcs=0;
} 
否则{
var-weight=parseFloat($(this).data('weight');
var sumWeight=pcs*重量;
$(this).toFixed(“tr”).find('.sum-weight2').html(sumWeight.toFixed(2)+'kg');
总重量+=总重量;
};
$('totalWeight').html(totalWeight.toFixed(2)+'kg');
})
};
calculateForm();//在页面第一次加载时也调用它一次,以设置初始数据
});
表格
{
边界塌陷:塌陷;
}
运输署
{
边框:实心1px黑色;
填充物:5px;
}

第14536号
测试1
3公斤
23431号
测试2
15公斤
第33125号
测试3
4公斤
总重量:
分辨率:




非常感谢您的帮助,我已经将代码注入到应该插入的位置,但是我得到了错误:
uncaughtreferenceerror:$未在索引中定义。php:42
转到
$(function(){
这意味着您没有在页面中包含jQuery。一旦您解决了这个问题,我的代码应该可以工作。如果它对您有帮助,请将其标记为已接受,而不是重新发布您自己的版本。请参阅(以及)-谢谢。是的,现在它起作用了,非常感谢你,因为你,我学到了很多东西。请注意安全。评论不用于长时间的讨论;这段对话已经结束。
    <script type="text/javascript">
    document.addEventListener(function()
    input.OnChange = calculateForm();
    function calculateForm() {
        var totalWeight = 0;
        $(".pcs-input").each(function () {
            var pcs = parseInt($(this).val());
            if (pcs < 0) {
                pcs = 0;
            } else {
                var weight = parseFloat($(this).data('weight'));
                var sumWeight = pcs * weight;
                totalWeight += sumWeight;
            }
        });
       document.write(totalWeight.toFixed(2) + ' kg');
      }
    )};
    </script>
 <?php
            # Database connection
                $dbhost = "localhost";
                $dbuser = "root";
                $dbpass = "1234";
                $dbname = "data";

                $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
                if ($connection->connect_error) {
                    die("Connection failed: " . $connection->connect_error);
                }
                #echo "Connection successfull!";
                
                # Questions for Database
                $sql = 'SELECT * FROM `elements`';
                $result = mysqli_query($connection, $sql);
                
                    if (mysqli_num_rows($result) > 0) {
                        //output data each row
                        while($row = mysqli_fetch_assoc($result)) {
                            echo "<tr><td>"."<input type='number' class='pcs-input' id='inputs' data-weight=".$row["weight"];
                            echo "></input>";
                            # echo "<tr><td>".$row["id"].'';
                            echo "</td><td>Nr. "  . $row["pcode"] . "</td><td> " . $row["fullname"]. "</td><td> " . $row["weight"] . "kg</td><td><div id='sum-Weight2'></div></td></tr>";
                            
                        }
                      } else {
                        echo "0 Results";
                    }
        #Declaring JavaScript Code inside PHP
                mysqli_close($connection);
      ?>

      </tbody>
    </table>
    <script type="text/javascript">
    function calculateForm() {
        var totalWeight = 0;
        $(".pcs-input").each(function () {
            var pcs = parseInt($(this).val());
            if (pcs < 0) {
                pcs = 0;
            } else {
                var weight = parseFloat($(this).data('weight'));
                var sumWeight = pcs * weight;
                $(this).parent().parent().find('.sum-weight2').html(sumWeight.toFixed(2) + ' kg');
                totalWeight += sumWeight;
            };
       $('.totalGewicht').html(totalWeight.toFixed(2) + ' kg');
      }
    )};
    </script>
        <!-- Counting all needed atributes -->
            <div id="totalWeight"></div>
        <!-- Submit btn -->
        <br><button name="submit" type="submit" value="Send" class="form-button">Send</button>
        </center>
    </form>
    <body>
        <!-- including calc.php file which contains all variables -->
        <?php include 'calc.php'; ?>
        <!-- Printing the form -->
    <form method="post" action="sendform.php"><br>
    
     <center>
        <input id="Name" type="text" placeholder="<?= $form['FIRMA']; ?>" class="form-contact" required><br>
         <input id="Adres" type="text" placeholder="<?= $form['ADRES']; ?>" class="form-contact" required><br>
          <input id="Email" type="email" placeholder="Email" class="form-contact" required><br>
           <input id="Country" type="text" placeholder="49" class="form-contact" value="" size="1" pattern="[0-9]{2}" maxlength="2" required>
            <input id="Phone" type="text" placeholder="Tel" class="form-contact" pattern="[0-9]{9}" maxlength="9" required>
            <br>
            
        <!-- Printing out the table -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <table class=".table-responsive">
    <thead>
        <tr>
            <th><?= $form['PRODUCTS']; ?></th>
            <th><?= $form['CATALOG']; ?></th>
            <th><?= $form['DESC']; ?></th>
            <th><?= $form['WEIGHT']; ?></th>
            <th><?= $form['TWEIGHT']; ?></th>
        <br>
        </tr>

    </thead>
      <tbody>
      
      <?php
            # Database connection
                $dbhost = "localhost";
                $dbuser = "root";
                $dbpass = "1234";
                $dbname = "data";

                $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
                if ($connection->connect_error) {
                    die("Connection failed: " . $connection->connect_error);
                }
                #echo "Connection successfull!";
                
                # Questions for Database
                $sql = 'SELECT * FROM `elements`';
                $result = mysqli_query($connection, $sql);
                
                    if (mysqli_num_rows($result) > 0) {
                        //output data each row
                        while($row = mysqli_fetch_assoc($result)) {
                            echo "<tr><td>"."<input type='number' class='pcs-input' data-weight='".$row["weight"]."' value='0' min='0' /></td>";
                            echo "<td>Nr. ".$row["pcode"]."</td><td>".$row["fullname"]."</td><td>".$row["weight"]."kg</td><td class='sum-weight2'></td></tr>";
                        }
                      } else {
                        echo "0 Results";
                    }
        #Declaring JavaScript Code inside PHP
                mysqli_close($connection);
      ?>

      </tbody>
    </table>
    <script type="text/javascript">
    $(function() {
  $(".pcs-input").change(calculateForm); //set up the event handler

  function calculateForm() {
    var totalWeight = 0;
    
    $(".pcs-input").each(function() {
      var pcs = parseInt($(this).val());
      
      if (pcs < 0) {
        pcs = 0;
      } 
      else {
        var weight = parseFloat($(this).data('weight'));
        var sumWeight = pcs * weight;
        $(this).closest("tr").find('.sum-weight2').html(sumWeight.toFixed(2) + ' kg');
        totalWeight += sumWeight;
      };
      $('#totalWeight').html(totalWeight.toFixed(2) + ' kg');
    })
  };
  
  calculateForm(); //call it once when the page first loads too, to set up the initial data
});
    </script>
        <!-- Counting all needed atributes -->
            Total Weight: <span id="totalWeight"></span>
        <!-- Submit btn -->
        <br><button name="submit" type="submit" value="Send" class="form-button">Send</button>
        </center>
    </form>
        
    </body>