如何从 html 输入表单中使用 php 查找时差
Posted
技术标签:
【中文标题】如何从 html 输入表单中使用 php 查找时差【英文标题】:How to find time difference using php from html input form 【发布时间】:2020-04-17 19:19:43 【问题描述】:我有三个页面,第一页(html)请求用户输入两个日期以获取日期。 第二页 (php) 将显示符合条件的结果和供用户选择的选择按钮, 第三页(php)将显示用户从第二页选择的信息。 现在我可以将信息从 SQL 传递到第三页,但我无法获取第一页到最后一页的数据信息以显示日差,因此我可以通过乘以费率 * 日差来计算总成本。
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<span class="form-label">Drop off</span>
<input class="form-section" type="date" name="date1" required>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<span class="form-label">Pick up</span>
<input class="form-section" type="date" name="date2" required>
</div>
</div>
</div>
<div class="row">
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$severname = "localhost";
$username = "root";
$password = "";
$dbname = "dbn";
//$conn = mysqli_connect('xx', 'xx', 'xx','xx');
$conn = mysqli_connect('xx', 'xx', 'xx','xx');
//check connection
if (mysqli_connect_errno())
echo nl2br("Failed to connect to MySQL: ". mysqli_connect_error() . "\n");
else
echo nl2br("");
$city = mysqli_real_escape_string($conn, $_POST['city']);
$state = mysqli_real_escape_string($conn, $_POST['state']);
//$date1 = mysqli_real_escape_string($conn, $_POST['date1']);
//$date2 = mysqli_real_escape_string($conn,$_POST['date2']);
$pet = mysqli_real_escape_string($conn, $_POST['pet']);
//var_dump($date2);
$sql = "SELECT CONCAT(s.lname, ' ', s.fname) AS fullName, s.pic, s.ratings, s.rate, s.phone, s.address FROM Pet_Sitter AS s WHERE s.city='".$city."' AND s.state='".$state."' AND s.pet_type='".$pet."' "
;
//echo $sql."<br/>";
$result = mysqli_query($conn,$sql);
if (!$result)
printf("Error: %s\n", mysqli_error($conn));
exit();
if ($result=mysqli_query($conn,$sql))
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("");
//var_dump($result);
$num_rows = mysqli_num_rows($result);
?> <div class="container"> <?php
if ($result->num_rows > 0)
while ($row = $result->fetch_assoc())
$rate = $row['rate'];
$fullName = $row['fullName'];
//var_dump($row);
// echo "<tr><td>" . $row['fullName'] . " </td><td>" . $row['ratings'] . " </td><td>" . $row['rate'] . " </td></tr>";
// echo '<div class="listing-item">';
echo '<div class="listing-item">';
echo '<article class="geodir-category-listing fl-wrap">';
echo '<div class="geodir-category-img">';
echo '<img src="../images/all/1.jpg" >';
echo '<div class="overlay"></div>';
echo '<div class="list-post-counter"><span>' . '$'. $row['rate'] . '</span></div>';
echo '</div>';
echo '<div class="geodir-category-content fl-wrap">';
echo '<a class="listing-geodir-category" href="payment.php?rate='.$rate.'&fullname='.$fullName.'">Select</a>';
echo '<h3><a href="../index.html">' . $row['fullName'] . '</a></h3>';
echo '<p>PET-SITTER INFORMATION</p>';
echo '<div class="geodir-category-options fl-wrap">';
echo '<span>' . 'Reviews ' . $row['ratings'] .'</span>';
echo '<div class="geodir-category-location">'. $row['address'] . " · " . $row['phone'] .
'</div>';
echo '</div>';
echo '</div>';
echo '</article>';
echo '</div>';
?>
<?php
else
echo "0 results";
mysqli_close($conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Work</title>
<!-- Bootstrap -->
<link type="text/css" rel="stylesheet" href="css/bootstrap-grid.min.css" />
<link type="text/css" rel="stylesheet" href="css/style.css" />
<link rel="shortcut icon" href="images/pet-logo.png">
</head>
<body>
<!-- Menu Bar -->
<div class="menu">
<div class="container-fluid my-container">
<div class="row my-row">
<div class="col-1">
<div class="menu-logo">
<a href="index.html"><img src="images/pet-logo.png" ></a>
</div>
</div>
<div class="nav-col col-7"></div>
<div class="nav-col col-2">
<div class="dropdown">
<button class="dropbtn"><a href="index.html">Home</a></button>
<div class="dropdown-content">
<a href="about.html">About Us</a>
<a href="contacts.html">Contact</a>
</div>
</div>
</div>
<div class="nav-col col-1"><a href="login.html">Login</a></div>
<div class="nav-col col-1"><a href="register.html">Sign Up</a></div>
</div>
</div>
</div>
<!-- End of Menu -->
<div id="booking" class="section">
<div class="section-center">
<div class="container">
<div class="row">
<div class="col-4">
<div class="booking-form">
<form method="POST">
<h1>Confirm Order</H1>
<div class="booking-form">
<span class="form-label"><?php
$fullName = isset($_GET['fullname']) ? $_GET['fullname']: '';
echo 'Sitter: <br>' . $fullName;
?></span>
</br>
</br>
<div class="form-group">
<span class="form-label"><?php
$rate = isset($_GET['rate'])? $_GET['rate']:'';
echo 'Total Amount: <br>$'. $rate;
?></span>
</div>
</div>
<br>
<br>
<div class="form-btn">
<a class="submit-btn" href = "https://www.tutorialspoint.com/php/php_sessions.htm">Confirm and Pay</a>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
【问题讨论】:
【参考方案1】:为了发送您的数据,您希望将它们放在一个表单中,并给它一个指向您要处理数据的端点的操作。
<form action="/the/route/to/your/phpfile.php" method="post">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<span class="form-label">Drop off</span>
<input class="form-section" type="date" name="date1" required>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<span class="form-label">Pick up</span>
<input class="form-section" type="date" name="date2" required>
</div>
</div>
</div>
<div class="row">
<button type="submit">Click here</button>
</form>
在目的地之后,您可以通过从 $_POST 数组中检索数据来获得所需的数据,就像您已经使用它一样。您也可以有一个提交按钮,但如果我是对的,您也可以按 Enter 键提交。
顺便说一句:如果你想用日期计算很多,你可能想在你的项目中实现 Carbon。 https://carbon.nesbot.com/docs/
【讨论】:
以上是关于如何从 html 输入表单中使用 php 查找时差的主要内容,如果未能解决你的问题,请参考以下文章
如何使用户无法使用 PHP 在表单中输入原始 HTML 代码? [复制]
如何确定从我的 html 表单获取的输入值的数据类型,以便我可以使用它们在准备好的语句中绑定参数