-3

    define('DB_PASS' ,'');
    define('DB_NAME', 'codexworld');
    $con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
    if(isset($_POST['submit'])){
    $username=$_POST['username'];
    $email=$_POST['email'];
    $subject=$_POST['subject'];
    $message=$_POST['message'];
    $q=mysqli_query($con,"insert into demo (username,email,subject,message)values('$username','$email','$subject','$message')");
    if ($q){
    echo 'Not Inserted';
    } else {
    echo 'Inserted Successfully';
    header('Location: ib.php');
    }
    }
    ?>
    <!DOCTYPE html>
    <html>
       <head>
          <title>Simple Contact form in PHP & MySQL</title>
          <!-- Latest compiled and minified CSS -->
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
       </head>
       <body>
          <div class="container">
             <div class="row">
                <form action="ib.php" class="col-md-6 col-md-offset-3" method="post">
                   <h2>Contact Us</h2>
                   <div class="form-group">
                      <label for="exampleInputEmail1">Name</label>
                      <input type="text" name="username" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Full Name" required="">
                   </div>
                   <div class="form-group">
                      <label for="exampleInputEmail1">Email address</label>
                      <input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email" required="">
                   </div>
                   <div class="form-group">
                      <label for="exampleInputPassword1">Subject</label>
                      <input type="text" name="subject" class="form-control" id="exampleInputPassword1" placeholder="Enter Your Subject" required="">
                   </div>
                   <textarea class="form-control"  name="message" rows="3" placeholder="Enter Your Query Here" required=""></textarea>
                   <button type="submit" name="submit"  class="btn btn-default">Submit</button>
                </form>
             </div>
          </div>
       </body>
    </html>

the above code connects to database and saves form data in database and it is supposed to redirect the form data gets stored in data base but it doesn't redirect. How can i solve this?Save the form data and redirect

Rajan Sharma
  • 2,211
  • 3
  • 21
  • 33
Zlatan
  • 13
  • 8

1 Answers1

3

You need to exit after the header redirect so the html doesn't keep loading.

header("Location: ib.php");
exit;

It's also best to not output anything before the header() .. eg; don't echo 'Inserted Successfully' in your production code.

John.M
  • 335
  • 1
  • 10
  • I dont know how to thank you it has worked – Zlatan Jul 09 '19 at 13:34
  • Nice .. also you should pay attention to the possibility of sql injection as your code stands at the moment. My preference is using prepared statements with PDO but you should look into what's available with mysqli. Cheers. – John.M Jul 09 '19 at 13:39