Reputation: 79
I want the user to enter an animal and it go to the line below their previous input.
<!DOCTYPE html>
<html>
<head>
<title> Will Proj. 2</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1> Animal Form</h1>
<form action="" method="post">
Enter animal:<br>
<input type="text" name="animal"><br><br>
<input type="Submit" value="Add Animal">
</form>
<?php
function display() {
$animal = $_POST['animal'];
echo "$animal <br>";
}
if(isset($_POST['animal'])){
display();
}
?>
</body>
</html>
It currently only displays 1 line at a time and refreshes the line with new input.
Upvotes: 0
Views: 294
Reputation: 371
<!DOCTYPE html>
<html>
<head>
<title> Will Proj. 2</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1> Animal Form</h1>
<form action="" method="post">
Enter animal:<br>
<?php if(isset($_POST['animal'])){
foreach($_POST['animal'] as $animal){
echo '<input type="hidden" name="animal[]" value="'.$animal.'" >';
}
} ?>
<input type="text" name="animal[]"><br><br>
<input type="Submit" value="Add Animal">
</form>
<?php
function display() {
$animals = $_POST['animal'];
foreach($animals as $animal){
echo "$animal <br>";
}
}
if(isset($_POST['animal'])){
display();
}
?>
</body>
</html>
Upvotes: 0
Reputation: 572
<?php
session_start();
if (!isset($_SESSION['animals']) && $_POST['animal']) {
$_SESSION['animals'] = $_POST['animal'].'<br>';
} elseif (isset($_SESSION['animals']) && $_POST['animal']) {
$_SESSION['animals'] .= $_POST['animal'].'<br>';
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Will Proj. 2</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1> Animal Form</h1>
<form action="" method="post">
Enter animal:<br>
<input type="text" name="animal"><br><br>
<input type="Submit" value="Add Animal">
</form>
<?php if (!empty($_SESSION['animals']) { echo $_SESSION['animals']; } ?>
</body>
</html>
Upvotes: 1