0

Everything works fine. but the issue is the when I add a new contact. the page doesn't go back to index and if I go back to index myself the new contact is added. I checked my code using some checking software and it says I have a error on my PHP line.

<?php
require_once"connection.php";
?>
<!DOCTYPE html>
<html>
  <head>
    <?php include"includes/head.inc"; ?>
    <script>tinymce.init({selector:'textarea'});</script>
  </head>
  <body>
    <div class="wrapper">
      <!-- header section -->
      <div class="header">
        <div class="headerContent"><h1>CONTACT LIST</h1></div>
      </div>
      <!-- content section -->
      <div class="content">
        <div><h1>Create New Contact</h1></div>
        <hr>
        <div class="contact">
          <div class="contact_insert">
            <form action="insert_contact.php" method="post">
              <table style="float:left" width="50%">
                <tr>
                    <td>Name:</td>
                    <td><input type="text" name="name" placeholder="name"  size="40%"></td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td><input type="text" name="email" placeholder="email" size="40%"></td>
                </tr>
                <tr>
                    <td>Department:</td>
                    <td><select name ="department" </td>
                </tr>
                <tr>
                    <td>Extension Number:</td>
                    <td><input type="text" name="extension" placeholder="extension" size="40%"></td>
                </tr>
                <tr>
                    <td>Cellphone:</td>
                    <td><input type="text" name="cellphone" placeholder="cellphone" size="40%"></td>
                </tr>
              </table>
              <div class="clear"></div>
              <input class="insert_contact_button" type="submit" name="submit" value="Add Contact">
              <a href="index.php"><input class="cancel_contact_button" type="button" value="Cancel"></a>
            </form>
          </div>
          <div class="clear"></div>
        </div>

  </body>
</html>
<?php
< ---IT SAYS THE ERROR IS HERE
if (isset($_POST['submit'])) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $department = $_POST['department'];
  $extension = $_POST['extension'];
  $cellphone = $_POST['cellphone'];
  $insert_contact = "insert into contacts (name, email, department, extension, cellphone) values ('$name', '$email', '$department', '$extension', '$cellphone')";
  $sql_insert_contact = $conn->query($insert_contact);
  $to = "manie@titan-networks.co.za";
  $from = "ExtensionList@alpinemotors.co.za";
  $subject = "New Staff Added To Extension List";
  $message = "New Staff: " . "\n\n" . "Name : " . $name . " " . "\n\n" . "Email: " . $email . " " . "\n\n" . "Department: " . $department . " " . "\n\n" . "Extension: " . $extension . " " . "\n\n" . "Cellphone: " . $
      $headers = "From:" . $from;
  mail($to, $subject, $message, $headers);
  if ($sql_insert_contact == true) {
    header("Location: index.php");
  }
}
?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
5ilent
  • 5
  • 2

3 Answers3

1

You cannot use Header function after your html. Take your code to the top, before you render anything.

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called.

<?php
    header("Location: index.php") //This will be succeed
?>
<html>
//some html
</html>
<?php
    header("Location: index.php") //This will fail
?>
DDRamone
  • 1,078
  • 11
  • 18
  • could also use output buffering, http://stackoverflow.com/questions/3111179/how-do-headers-work-with-output-buffering-in-php – Alex Andrei Jul 19 '16 at 10:59
1

Try adding ob_start(); right the line below the first opening php tag in the top. In your case try change the top to:

<?php
ob_start();
require_once "connection.php";
?>

You should also considering adding ob_end_flush(); in the very bottom of your file

0

try this

      if (isset($sql_insert_contact)) {
         header("Location: index.php");
       }

or

if ($sql_insert_contact) {
      header("Location: index.php");
      }
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45