如何在HTML页面中显示单独PHP文件的结果?
我需要在HTML表单页面本身中显示总和。我的PHP文件中给出了部分代码:如何在HTML页面中显示单独PHP文件的结果?,php,html,mysql,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>&l
<?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;?>
因为我假设你们想显示总计,若它是正确的,那个么遵循下面的方法
$sum = 0;
while($row = mysqli_fetch_array($search_result)):?>
$sum+= $row['value'];
<?php endwhile;?>
echo $sum;
$sum=0;
而($row=mysqli\u fetch\u数组($search\u result)):?>
$sum+=$row['value'];
echo$sum;
或
如果您希望在不重新加载页面的情况下显示结果。您必须在ajax请求中编写代码。我们在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";
?>
HTML:
//脚本下载
发件人:
致:
总计
你的总数:
$(“#总计”)。单击(函数(){
$.ajax({
url:“phptest.php”,
类型:“POST”,
成功:功能(数据)
{
$('#sum').html(数据);
}
});
});
PHP:这是您的PHP代码
您需要使用并处理POST请求中发送的信息。基本上,这两个按钮都需要是submit类型,以便它们都将请求发送到流程(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();
<?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');
<?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);
?>
发件人:
致:
全部的
如果我正确理解了您的问题,那么您需要Ajax调用。根据您的偏好获取或发布。在此基础上,您的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>
发件人:
致:
总计
$(“#gettotal”)。单击(函数(e){
e、 预防默认值();
$.ajax({
根据您的选择或要求键入:“GET”/
url:'calculate.php',
成功:功能(响应){
//在这里,你决定在哪里显示输出值
警报(响应);
},
错误:函数(结果){
警报(“错误”);
}
});
});
calculate.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();
<?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');
<?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);
?>
html和.php文件是分开的?@pAsh文件是分开的将值分配到隐藏字段。单击“总计”按钮后,从隐藏字段中获取值并显示所需的位置。另一种更安全的方法是使用AJAX。在服务器端存储sum。单击“总计”按钮后,进行ajax调用,并将ajax响应显示为一个总和。表行如何?你是怎么用的?它们也需要出现在结果中吗?但是我想将其显示在同一表单页上,然后您必须使用jquery编写一次单击ajax请求的代码。sum将显示在下一页中