I want to check the variables of a form. here I want "a" and "b" to be numbers betwen 5 and 99. if that is true I want to automaticly redirect to the next page, if not to remain to this page.
Let me explain by showing the piece of code i made: (i am sorry but i spent 15 minutes to understand how to write code here and i don't understand so i will use spaces when necesary)
<input type="text" name="a"><br>
<input type="text" name="b"><br>
<input type="submit" value="GO!">
<?php
if (is_numeric($_POST["a"]) && is_numeric($_POST["b"]) &&
$_POST["a"]<100 && $_POST["a"]>4 && $_POST["b"]<100 && $_POST["b"]>4) {
echo "corect";
//here i want to go to the next page like registration_complete.php
}
else {
//here i want to remain to this page and show the errors to the user
}
?>
I am sorry for this probably simple question but i find only stupid answers on google.
First you must check it before you echo something. Then use header("Location: mylogin_page.php");
.
What the point to echo something when you have chance that user will not see it? Then use header BEFORE html output.
Because you are already outputting content to the screen, you cannot use the header('Location: mypage.php')
. If you can't or don't want to do this, an alternative would be to use a javascript redirect:
echo "<script>document.location.replace = 'mypage.php';</script>";
<?php
if (is_numeric($_POST["a"]) && is_numeric($_POST["b"]) && $_POST["a"]<100 && $_POST["a"]>4 && $_POST["b"]<100 && $_POST["b"]>4)
{
header('location:correctpage.php');
}
else
{
foreach($POST as $key=>$post)
{
if(empty($post))
{
$error [] ="$key is empty required";//do your custom error handling this is just a demo
}
print_r($error);
//here i want to remain to this page and show the errors to the user
}
<input type="text" name="a"><br/>
<input type="text" name="b"><br/>
<input type="submit" value="GO!">
<?php
if (is_numeric($_POST["a"]) && is_numeric($_POST["b"]) &&
$_POST["a"]<100 && $_POST["a"]>4 && $_POST["b"]<100 && $_POST["b"]>4) {
//echo "corect"; //why 'echo' then redirect?
echo "<script>";
echo "top.location = 'registration_complete.php';";
echo "</script>";
exit();
}
else {
//here i want to remain to this page and show the errors to the user
//(if you want so, just stay executing this page)
}
?>
First of all you can't use header
after you send any html code to the browser, so your php code should be in another file so you should have:
first-step.php
which contains:
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?php
if(isset($_SESSSION['error'])) {
echo $_SESSION['error'];
unset($_SESSION['error']);
}
?>
<form action="actions.php?step=1" method="POST">
<input type="text" name="a"><br>
<input type="text" name="b"><br>
<input type="submit" value="GO!">
</form>
</body>
</html>
actions.php
<?php
session_start();
switch($_GET['step']) {
case 1:
if(is_numeric($_POST["a"]) && is_numeric($_POST["b"]) && $_POST["a"]<100 && $_POST["a"]>4 && $_POST["b"]<100 && $_POST["b"]>4)
{
header('Location: second-step.php');
exit;
}
else
{
$_SESSION['error'] = 'my error';
header('Location: first-step.php');
}
break;
}
?>