Home > Software design >  Opening A New Page After Form Successfully Submitted Using Php
Opening A New Page After Form Successfully Submitted Using Php

Time:05-16

hosting-file-format

I control form elements using php.If they are not valid i print an error message below them.My problem is when every element is true in the form ,the page is not redirected the destination page i want,it shows an empty logintest.php(inside php folder) and not redirecting it. Here my index.php code(it is login page actually)

<form action="../php/logintest.php" method="post">
                    <div >
                        <input type="email" name="email"  id="name" placeholder="Username-mail">
                            <?php if(isset($nameError)) { ?>                
                                <?php echo "<span style='color:red;'>$nameError</span>"; ?>                     
                            <?php } ?>
                    </div>   
                    <div >                            
                        <input type="password" name="password"  id="parola" placeholder="Password">
                            <?php if(isset($parolaError)) {?>
                                <?php echo "<span style='color:red;'>$parolaError</span>"; ?>   
                            <?php } ?>
                    </div>
                    <div >
                        <input type="checkbox"  id="accept" name="checker">
                            <p class=ctrlButton>You have to accept the terms.</p>
                            <?php if(isset($checkBoxError)) {?>
                                <?php echo "<span style='color:red;'>$checkBoxError</span>"; ?> 
                            <?php } ?>
                      </div>
                    <div >
                        <input type="submit" name="submit" value="Enter" id="submit" >                     
                    </div>    
                  
                </form>

And my logintest.php code is..

<?php     
       $nameError="";
       $parolaError="";
       $checkBoxError="";
       if(isset($_POST['checker'])){
            if(isset($_POST['email']) && isset($_POST['password']) ){
                if($_POST['password']=="123" && $_POST['email']=="[email protected]"){
                    //true input
                    
                   // window.open('https://github.com/', '_blank');
                    header("location:https://www.google.com");

                }
                if($_POST['password']!="b211210381"){
                            $parolaError='Parola is wrong';
                         //   header('Location: index.php'); 
                }
                if($_POST['email']!="[email protected]" ){
                        $nameError='Wrong email try again';
                       // header('Location: index.php');
                }
            }
            else{
                if($_POST['password']==""){
                    $parolaError='Enter a parola';
                }
                if($_POST['email']==""){
                    $nameError='Enter a userName';
                
                }
            }
        }
        else {           
              $checkBoxError='Please accept the terms';              
           }
        include('../index.php');

        ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL ^ E_NOTICE);
      

?>

CodePudding user response:

I believe you cannot set the header location once its already been set see accepted answer here : How to fix "Headers already sent" error in PHP

I am not sure how much hate I will get for offering this solution but this does the trick for me: ( 0 is time in seconds so you can add a delay)

echo '<meta http-equiv="refresh" content="0; URL=https://www.google.com">';
  • Related