I have two .php files as such:
test1.php
Sample
Sample
<form action="<?php $_PHP_SELF ?>" method="post"> Please enter a number <input type="number" name="userNumber"><br> <input type="submit"> </form> </body> </html>
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if (isset($_POST['submitted'])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
else {
?>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submitted">
</form>
<?php
}
?>
</body>
</html>
set the variables up before the form as placeholders and check for isset
<?php
$user_number = '';
if(isset($_POST["userNumber"])) {
$user_number = $_POST["userNumber"];
}
?>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submit">
</form>
<p>You have chosen: <?php echo $user_number ?></p>
Just use isset()
for submit button, note that i added name="submit"
to your button since it was missing
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST['submit'])
{
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
} else {
?>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submit">
</form>
<?php } ?>
</body>
</html>
Pretty simple actually:
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST) && isset($_POST['userNumber'])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
?>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body>
</html>
Your test1.php
will need to look like this
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="test1.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
<?php
if(isset($_POST["userNumber"])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
?>
</body>
</html>