我目前正在使用PHP脚本将记录添加到我的数据库中,这在一些调试后正常工作。当脚本运行时,它会打开一个带有查询输出的新页面。
我想在页面顶部的导航栏下,在执行查询的同一页面上显示查询输出。
HTML页面的相关代码:
<form name="registration" action="addKlant.php" target="_blank" method="post">
<h3><i class="fa fa-plus-circle"></i> Klant toevoegen</h3>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Naam</label>
<input type="text" name="klantNaam">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Soort klant</label>
<select class="" name="klantSoort">
<option value="">Bedrijf</option>
<option value="">Particulier</option>
</select>
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Telefoonnummer</label>
<input type="number" name="klantTel">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> E-mail adres</label>
<input type="email" name="klantEmail">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Straat & huisnr</label>
<input type="text" name="klantAdres">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Postcode</label>
<input type="text" name="klantPostcode">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Plaats</label>
<input type="text" name="klantPlaats">
</div>
<div class="form-input">
<input type="submit" name="submit" value="Klant toevoegen">
</div>
</form>
addKlant.php
<?php
require('db.php');
$stmt = $con->prepare('INSERT INTO `klanten` (klant_naam, klant_soort, klant_telefoon, klant_email, klant_straat, klant_postcode, klant_plaats) VALUES (?, ?, ?, ?, ?, ?, ?)');
if($stmt) {
$klant_naam = $_POST['klantNaam'];
$klant_soort = $_POST['klantSoort'];
$klant_telefoon = $_POST['klantTel'];
$klant_email = $_POST['klantEmail'];
$klant_adres = $_POST['klantAdres'];
$klant_postcode = $_POST['klantPostcode'];
$klant_plaats = $_POST['klantPlaats'];
$stmt->bind_param('ssissss', $klant_naam, $klant_soort, $klant_telefoon, $klant_email, $klant_adres, $klant_postcode, $klant_plaats);
$stmt->execute();
$con->close();
echo "Klant is toegevoegd, je kan deze pagina nu sluiten.";
}
?>
我在考虑使用像if($_POST)
这样的东西?但我不知道如何完全实现它。
如果页面刷新以添加输出并不重要,但是我想将它保存到PHP而不使用JS(当然,如果只有PHP可以使用)。
非常感谢任何提示。
编辑:在@SloanTrasher的帮助下编辑我的代码
<?php
if($_POST['submit'] == 'Klant toevoegen') {
require('db.php');
$stmt = $con->prepare('INSERT INTO `klanten` (klant_naam, klant_soort, klant_telefoon, klant_email, klant_straat, klant_postcode, klant_plaats) VALUES (?, ?, ?, ?, ?, ?, ?)');
if($stmt) {
$klant_naam = $_POST['klantNaam'];
$klant_soort = $_POST['klantSoort'];
$klant_telefoon = $_POST['klantTel'];
$klant_email = $_POST['klantEmail'];
$klant_adres = $_POST['klantAdres'];
$klant_postcode = $_POST['klantPostcode'];
$klant_plaats = $_POST['klantPlaats'];
$stmt->bind_param('ssissss', $klant_naam, $klant_soort, $klant_telefoon, $klant_email, $klant_adres, $klant_postcode, $klant_plaats);
$stmt->execute();
$con->close();
}
}
?>
<!-- I placed it above the form for simplification -->
<?php ($_POST['submit'] == 'Klant toevoegen') {echo "<p>Klant is toegevoegd.</p>";}?>
<form name="registration" action="addKlant.php" target="_blank" method="post">
<h3><i class="fa fa-plus-circle"></i> Klant toevoegen</h3>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Naam</label>
<input type="text" name="klantNaam">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Soort klant</label>
<select class="" name="klantSoort">
<option value="">Bedrijf</option>
<option value="">Particulier</option>
</select>
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Telefoonnummer</label>
<input type="number" name="klantTel">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> E-mail adres</label>
<input type="email" name="klantEmail">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Straat & huisnr</label>
<input type="text" name="klantAdres">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Postcode</label>
<input type="text" name="klantPostcode">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Plaats</label>
<input type="text" name="klantPlaats">
</div>
<div class="form-input">
<input type="submit" name="submit" value="Klant toevoegen">
</div>
</form>
编辑2:感兴趣的人的工作代码
<?php
include('auth.php');
if($_POST['submit'] == 'Klant toevoegen') {
require('db.php');
$stmt = $con->prepare('INSERT INTO `klanten` (klant_naam, klant_soort, klant_telefoon, klant_email, klant_straat, klant_postcode, klant_plaats) VALUES (?, ?, ?, ?, ?, ?, ?)');
if($stmt) {
$klant_naam = $_POST['klantNaam'];
$klant_soort = $_POST['klantSoort'];
$klant_telefoon = $_POST['klantTel'];
$klant_email = $_POST['klantEmail'];
$klant_adres = $_POST['klantAdres'];
$klant_postcode = $_POST['klantPostcode'];
$klant_plaats = $_POST['klantPlaats'];
$stmt->bind_param('ssissss', $klant_naam, $klant_soort, $klant_telefoon, $klant_email, $klant_adres, $klant_postcode, $klant_plaats);
$stmt->execute();
$con->close();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<!-- Charset, viewport en keywords -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Titel en favicon -->
<title></title>
<link rel="icon" href="./img/favicon.jpg">
<!-- Scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="./js/pageForward.js"></script>
<script type="text/javascript" src="./js/pageBack.js"></script>
<!-- Stylesheets en fonts -->
<link rel="stylesheet" href="./css/backend_styles.css">
<link href="https://fonts.googleapis.com/css?family=Work+Sans:200" rel="stylesheet"> <!-- font-family: 'Work Sans', sans-serif; -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet"> <!-- font-family: 'Open Sans', sans-serif; -->
<link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="top-links">
<p><a href="index.php"><i class="fa fa-home"></i></a><a href="geavanceerd.php"><i class="fa fa-cogs"></i></a><a onclick="goBack()"><i class="fa fa-arrow-left"></i></a><a onclick="goForward()"><i class="fa fa-arrow-right"></i></a> Backoffice | Ingelogt als: <?php echo $_SESSION['username']; ?> |<a href="logout.php"> Uitloggen</a></p>
</div>
<?php if($_POST['submit'] == 'Klant toevoegen') {echo "<p>Klant is toegevoegd.</p>";}?>
<div class="inner-container">
<div class="backend-form">
<form name="registration" method="post">
<h3><i class="fa fa-plus-circle"></i> Klant toevoegen</h3>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Naam</label>
<input type="text" name="klantNaam">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Soort klant</label>
<select class="" name="klantSoort">
<option value="Bedrijf">Bedrijf</option>
<option value="Particulier">Particulier</option>
</select>
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Telefoonnummer</label>
<input type="number" name="klantTel">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> E-mail adres</label>
<input type="email" name="klantEmail">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Straat & huisnr</label>
<input type="text" name="klantAdres">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Postcode</label>
<input type="text" name="klantPostcode">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Plaats</label>
<input type="text" name="klantPlaats">
</div>
<div class="form-input">
<input type="submit" name="submit" value="Klant toevoegen">
</div>
</form>
</div>
</div>
</div>
<footer>
<p>Copyright © 2018</p>
</footer>
</body>
</html>
答案 0 :(得分:1)
这是工作代码。很高兴你得到它的工作!
<?php
include('auth.php');
if($_POST['submit'] == 'Klant toevoegen') {
require('db.php');
$stmt = $con->prepare('INSERT INTO `klanten` (klant_naam, klant_soort, klant_telefoon, klant_email, klant_straat, klant_postcode, klant_plaats) VALUES (?, ?, ?, ?, ?, ?, ?)');
if($stmt) {
$klant_naam = $_POST['klantNaam'];
$klant_soort = $_POST['klantSoort'];
$klant_telefoon = $_POST['klantTel'];
$klant_email = $_POST['klantEmail'];
$klant_adres = $_POST['klantAdres'];
$klant_postcode = $_POST['klantPostcode'];
$klant_plaats = $_POST['klantPlaats'];
$stmt->bind_param('ssissss', $klant_naam, $klant_soort, $klant_telefoon, $klant_email, $klant_adres, $klant_postcode, $klant_plaats);
$stmt->execute();
$con->close();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<!-- Charset, viewport en keywords -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Titel en favicon -->
<title></title>
<link rel="icon" href="./img/favicon.jpg">
<!-- Scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="./js/pageForward.js"></script>
<script type="text/javascript" src="./js/pageBack.js"></script>
<!-- Stylesheets en fonts -->
<link rel="stylesheet" href="./css/backend_styles.css">
<link href="https://fonts.googleapis.com/css?family=Work+Sans:200" rel="stylesheet"> <!-- font-family: 'Work Sans', sans-serif; -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet"> <!-- font-family: 'Open Sans', sans-serif; -->
<link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="top-links">
<p><a href="index.php"><i class="fa fa-home"></i></a><a href="geavanceerd.php"><i class="fa fa-cogs"></i></a><a onclick="goBack()"><i class="fa fa-arrow-left"></i></a><a onclick="goForward()"><i class="fa fa-arrow-right"></i></a> Backoffice | Ingelogt als: <?php echo $_SESSION['username']; ?> |<a href="logout.php"> Uitloggen</a></p>
</div>
<?php if($_POST['submit'] == 'Klant toevoegen') {echo "<p>Klant is toegevoegd.</p>";}?>
<div class="inner-container">
<div class="backend-form">
<form name="registration" method="post">
<h3><i class="fa fa-plus-circle"></i> Klant toevoegen</h3>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Naam</label>
<input type="text" name="klantNaam">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Soort klant</label>
<select class="" name="klantSoort">
<option value="Bedrijf">Bedrijf</option>
<option value="Particulier">Particulier</option>
</select>
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Telefoonnummer</label>
<input type="number" name="klantTel">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> E-mail adres</label>
<input type="email" name="klantEmail">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Straat & huisnr</label>
<input type="text" name="klantAdres">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Postcode</label>
<input type="text" name="klantPostcode">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Plaats</label>
<input type="text" name="klantPlaats">
</div>
<div class="form-input">
<input type="submit" name="submit" value="Klant toevoegen">
</div>
</form>
</div>
</div>
</div>
<footer>
<p>Copyright © 2018</p>
</footer>
</body>
</html>
答案 1 :(得分:-2)
我假设HTML表单不是addKlant.php的一部分? 在这种情况下,一旦处理完表单,您可能希望重定向到包含HTML表单的页面,并使用某种变量传递查询字符串中的项目以显示查询已运行。
即
// ... after your form processing
$con->close();
header("Location: htmlpage.php?queryresult=ok");
exit;
然后在HTML表单页面中:
<?php if ($_GET['queryresult'] == 'ok') print "query ran okay"; ?>
我不知道查询的结果可能是什么样的 - 如果它在一个url中可编码,你可以传递整个响应,即
header("Location: htmlpage.php?queryresult=" . urlencode( $queryresultstring ));