I have 2 questions. The first one is probably really easy but I don't know how to do it. I made a code that lets you save a music artist and a single and after you save it you can open it with a but I want to open it with a button how do i do that.
And the second question is when you open the saved files I get 20 lines of things that are empty and 1 that is full (sorry for my bad english). How do I change that into normal lines so it only makes a line if needed. Here are my 2 codes:
FILE NAME SEB2.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<?php
if (!empty($_POST)) {
$artiest = $_POST["artiest"];
$single = $_POST["single"];
$fp = fopen("muziekcollectie.txt", "a+");
fputs($fp, $artiest."\r\n");
fputs($fp, $single."\r\n");
fclose($fp);
}
?>
</head>
<body>
<form name="woorden" method="post">
Artiest:<input type="text" name="artiest"><br>
Single:<input type="text" name="single"><br>
<input type="submit" name="Add" value="Add"><br><br>
<form>
<a href="SEB2.php"><!-- <input type="submit" name="Watch" value="Watch" action="SEB2.php"> -->TEST</a>
</body>
</html>
FILE NAME SEB2.php
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="utf-8">
<title>Het gastenboek lezen</title>
</head>
<body>
<?php
$bestandsnaam = "muziekcollectie.txt";
// aantal regels per bijdrage om in te lezen
// elke bijdrage bestaat uit 5 regels
$aantal_regels = 2;
// teller om bij te houden hoeveel regels
// zijn gelezen
$teller = 0;
// Aantal bijdragen wordt bijgehouden.
$aantal_bijdragen = 0;
// gastenboek openen om te lezen
if (file_exists($bestandsnaam)){
$fp = fopen($bestandsnaam, "r");
}
else{
echo "<h2>De muziekcollectie is nog leeg!</h2>
<a href= 'SEB1.php'>
Wees de eerste die erin schrijft!</a> ";
exit;
}
while (!feof($fp)){
$regel = fgets($fp);
if (!feof($fp)){
if ($teller % $aantal_regels == 0){
// kop afdrukken
$aantal_bijdragen++;
// echo "<hr>";
echo "<h3>Bijdrage: $aantal_bijdragen</h3>";
}
}
// regel afdrukken
echo "$regel <br>";
// echo "<hr>";
$teller++;
}
echo "<a href='SEB1.php'>Terug naar de
homepage</a>";
?>
</body>
</html>
答案 0 :(得分:0)
按钮链接不是很难
<button onclick="window.location.href='SEB2.php';"></button>
我注意到你没有关闭你的表单,所以当你测试它时可能会出现问题。
我不确定你对第二部分的意思,但我认为这只是因为你没有对你的帖子进行空检查,所以空输入也会被添加到文件中。如果这是问题,这将解决它:
if (!empty($_POST['artiest']) && !empty($_POST['single']))