How can I display results from a separate PHP file in an HTML page?

I need to display the amount on the HTML form page itself. Some of the code is below in my PHP file:

<?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;?>

      

In my HTML form, I have a button total

. When I click on this, I need to display the value of the sum. How can i do this?

<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>

      

+3


source to share


4 answers


Since I assume you want to show the total, if it is correct then follow below method

 $sum = 0;

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

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

    <?php endwhile;?>

    echo $sum;

      



or

if you want to show the result without reloading the page. you need to program the ajax request.

0


source


We are using ajax on the html page to access content from the php page:



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

      

0


source


You need to use PHP SESSIONs and play with the information you send in the POST request. Basically, both buttons should be of type submit in order for both of them to send the request to the process (process.php). Then you can use sessions to determine your results.

Do it like this:

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');

      

0


source


if I understood your problem correctly then you need an Ajax call. GET or POST based on your preference. Since your HTML code will be decoupled from the php code, basically the php code will respond to the Ajax call and issue it.

Modified Test.html code

<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);
?>

      

-1


source







All Articles