如何在HTML页面中显示单独的PHP文件的结果?

时间:2017-06-28 04:11:49

标签: php html mysql

我需要在HTML表单页面中显示总和。代码的一部分在我的PHP文件中给出:

<?php 
  while($row = mysqli_fetch_array($search_result)):?>
    <?php
      $sum=$sum+$row['value']; 
    ?>
    <tr>
      <td><?php echo $row['id'];?></td>
      <td><?php echo $row['time'];?></td>
      <td><?php echo $row['value'];?></td>
    </tr>
<?php endwhile;?>

在我的HTML表单中,我有一个total按钮。当我点击它时,我需要显示总和值。我怎么能这样做?

<form action="php_html_table_data_filter3.php" method="post">
  From:<input type="text" name="valueToSearch1"><br><br>
  To:<input type="text" name="valueToSearch2"><br><br>
  <input type="submit" name="search" value="Filter"><br><br>
  <button type="button">Total</button><br><br>
</form>

4 个答案:

答案 0 :(得分:0)

我假设您想要显示总数,如果它是正确的,请按照以下方法

 $sum = 0;

     while($row = mysqli_fetch_array($search_result)):?>

       $sum+= $row['value'];

    <?php endwhile;?>

    echo $sum;

如果要在不重新加载页面的情况下显示结果。你必须在ajax请求中编码。

答案 1 :(得分:0)

我们在html页面中使用ajax来访问php页面中的内容:

HTML:
<html>
<script src="jquery.min.js"></script> // script download
<form action="php_html_table_data_filter3.php" method="post">
  From:<input type="text" name="valueToSearch1"><br><br>
  To:<input type="text" name="valueToSearch2"><br><br>
  <input type="submit" name="search" value="Filter"><br><br>
  <button type="button" id="total">Total</button><br><br>
  Your sum:<span id="sum"></span>
</form>
</html>
<script>
$( "#total" ).click(function() {
    $.ajax({
        url : "phptest.php",
        type: "POST",
        success: function(data)
        {
           $('#sum').html(data);
        }
    });
});
</script>

PHP: here your php code
<?php 
echo "10";
?>

答案 2 :(得分:0)

您需要使用PHP SESSIONs,并使用您在POST请求中发送的信息。基本上,两个按钮都需要是提交类型,以便它们都将请求发送到进程(process.php)。然后,您可以使用会话来设置结果。

以这种方式做到:

<强>的index.php

<?php session_start(); ?>
<form action="process.php" method="post">
    From:<input type="text" name="valueToSearch1"><br><br>
    To:<input type="text" name="valueToSearch2"><br><br>
    <input type="submit" name="search" value="Filter"><br><br>
    <button name="total_btn" type="submit">Total</button>
</form>

<div id="results">
    <?= (!empty($_SESSION["table"])) ? $_SESSION["table"] : "" ?>
    <br>
    <?= (!empty($_SESSION["sum"])) ? "Sum: " . $_SESSION["sum"] : "" ?>
</div>
<?php
session_destroy();

<强> process.php

<?php
session_start();

$sum = 0;
$table = "<table>";
while ($row = mysqli_fetch_array($search_result)) {
    $sum += $row['value'];
    $table .= "<tr>
                  <td>{$row['id']}</td>
                  <td>{$row['time']}</td>
                  <td>{$row['value']}</td>
               </tr>";
}
$table .= "</table>";

if (isset($_POST['total_btn'])) {
    $_SESSION["sum"] = $sum;
}
$_SESSION["table"] = $table;
header('Location: index.php');

答案 3 :(得分:-1)

如果我理解你的问题,那么你需要Ajax调用。根据您的偏好进行GET或POST。因为您的HTML代码将与PHP代码分开,基本上PHP代码将响应Ajax调用并让您退出。

修改后的代码 - Test.html

<form action="php_html_table_data_filter3.php" method="post">
        From:<input type="text" name="valueToSearch1"><br><br>
        To:<input type="text" name="valueToSearch2"><br><br>
        <input type="submit" name="search" value="Filter"><br><br>
        <button type="button" id="gettotal">Total</button><br><br>

    </form>

<script>

$("#gettotal").click(function(e) {
    e.preventDefault();
    $.ajax({
    type: 'GET',//based on your choice or requirement
    url: 'calculate.php',
    success: function(response) {
       //here u decide where to show output value

         alert(response);

    },
    error: function(result) {
            alert('error');
        }
    });
});

</script>

<强> calculate.php

<?php 
$sum = 0;
  while($row = mysqli_fetch_array($search_result))
  {
   //calculate your total and send back as JSON obj.
      $sum=$sum+$row['value']; 
  }
 echo json_encode($sum);
?>