Cannot locate problem with .php Contact page - php

I am sure this will end up being a silly question, but hear me out please.
I am a novice developer and i'm building a website with html, css (w/ Bootstrap) and since the website has to have a contact form page, i am using php there. Originally when I was developing the pages of the website including the front-end of the Contact page. all my pages were .html. Now, following a tutorial online, i have added the php to the contact.html to have the contact from content sent to an email of my choosing, i have changed the file name from contact.html to contact.php and i have placed ALL the website files into MAMP's htdocs folder in my C drive in order to test. However, since switching to contact.php, i cannot view the page! Instead i receive an http error 500. Note that all the .html files render as they should.
No doubt somewhere along the line i have made a rudimentary mistake, but any assistance in clearing this problem is very much appreciated.
<!DOCTYPE html>
<?php
$message_sent = 'false';
if(isset($_POST['email']) && $_POST['email'] != ''){
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){
$userName = $_POST['name'];
$userEmail = $_POST['email'];
$message = $_POST['message'];
$messageMark = $_POST['mark'];
$to = "redacted_email#outlook.com";
$body = "";
$body .+ "From: ".$userName. "\r\n";
$body .= "Email: ".userEmail. "\r\n";
$body .= "Message: ".$message. "\r\n";
$body .= "Marketing: ".$messageMark. "\r\n";
mail($to, $body);
$message_sent = true;
}
}
?>
<html lang="en">
<main>
<?php
if($message_sent):
?>
<h2>Thank you, we shall be in touch soon.</h2>
<?php
else:
?>
<section id="contact">
<div class="container-lg">
<div class="">
<h1 class="title">Contact Us</h1>
<hr>
<div class="row justify-content-center my-5">
<div class="col-lg-6">
<form class="form" name="form" autocomplete="off" method="POST" action="contactus.php">
<label class="form-label" for="name" style="color: white;">Name:</label>
<input class="form-control mb-2" id="name" type="text" name="name" required>
<label class="form-label mt-3" for="email" style="color: white;">Email: <span class="tooltip" data-tooltip="Please enter your email address">?</span></label>
<input class="form-control" id="email" type="email" placeholder="e.g. name:example.com" name="email" required>
<div class="form-floating mb-5 mt-5">
<textarea id="query" class="form-control" name="message" style="height: 250px; overflow-y: visible;"></textarea>
<label for="query" class="textlab">Type your message here: <span class="tooltip" data-tooltip="Type your message here">?</span></label>
</div>
<div class="form-floating mb-5 mt-5">
<textarea id="query" class="form-control" name="mark" style="height: 100px;"></textarea>
<label for="query" class="textlab">How did you hear about us?:<span class="tooltip" data-tooltip="Just a few words on how you found our website and heard about our club">?</span></label>
</div>
<p>
<!--Instert recaptcha here? -->
</p>
<div class="mb-4 text text-center">
<button type="submit" id="conbtn" class="btn btn-light btn-lg btn-block">Send Now!</button>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
</main
For the sake of clarity, here is the entirety of the code from my contact.php page which is producing an HTTP ERROR 500 response through MAMP:
<!DOCTYPE html>
<?php
$message_sent = false;
if(isset($_POST['email']) && $_POST['email'] != ''){
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){
$userName = $_POST['name'];
$userEmail = $_POST['email'];
$message = $_POST['message'];
$messageMark = $_POST['mark'];
$to = "troy_a_w_easson#outlook.com";
$body = "";
$body .= "From: ".$userName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Message: ".$message. "\r\n";
$body .= "Marketing: ".$messageMark. "\r\n";
mail($to, $subject, $body):
$message_sent = true;
}
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/x-icon" href="images/LMRCupsc-removebg2.png">
<link
href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js">
</script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Beau+Rivage&display=swap"
rel="stylesheet">
<link href='//fonts.googleapis.com/css?family=Signika+Negative:300,400,600'
rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="CSS/contact.css">
<title>Largs Model Boat Club | Contact Us</title>
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-dark">
<div class="container-fluid">
<h1>Largs Model Boat Club</h1>
<a class="navbar-brand" href="index.html"><img src="images/LMRCupsc-
removebg2.png" alt="Largs Model Boat Club Logo" style="width:200px;"></a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="gallery.html">Gallery</a>
</li>
<li class="nav-item">
<a class="nav-link" href="history.html">History</a>
</li>
<li class="nav-item">
<a class="nav-link" href="news.html">News & Events</a>
</li>
<li class="nav-item">
<a class="nav-link" href="aboutlargs.html">About Largs</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="contactus.php">Contact
Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
</ul>
</div>
</nav>
</header>
<main>
<?php
if($message_sent):
?>
<h2>Thank you, we shall be in touch soon.</h2>
<?php
else:
?>
<section id="contact">
<div class="container-lg">
<div class="">
<h1 class="title">Contact Us</h1>
<hr>
<div class="row justify-content-center my-5">
<div class="col-lg-6">
<form class="form" name="form" autocomplete="off" method="POST"
action="contactus.php">
<label class="form-label" for="name" style="color:
white;">Name:</label>
<input class="form-control mb-2" id="name" type="text"
name="name" required>
<label class="form-label mt-3" for="email" style="color:
white;">Email Address: <span class="tooltip" data-tooltip="Please enter your
email address">?</span></label>
<input class="form-control" id="email" type="email"
placeholder="e.g. name:example.com" name="email" required>
<label class="form-label" for="subject" style="color:
white;">Subject:</label>
<input class="form-control mb-2" id="subject" type="text"
name="subject">
<div class="form-floating mb-5 mt-5">
<textarea id="query" class="form-control" name="message"
style="height: 250px; overflow-y: visible;"></textarea>
<label for="query" class="textlab">Type your message
here: <span class="tooltip" data-tooltip="Type your message here">?</span>
</label>
</div>
<div class="form-floating mb-5 mt-5">
<textarea id="query" class="form-control" name="mark"
style="height: 100px;"></textarea>
<label for="query" class="textlab">How did you hear
about us?:<span class="tooltip" data-tooltip="Just a few words on how you
found our website and heard about our club">?</span></label>
</div>
<p>
<!--Instert recaptcha here? -->
</p>
<div class="mb-4 text text-center">
<button type="submit" id="conbtn" class="btn btn-light
btn-lg btn-block">Send Now!</button>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
<a href="https://en.wikipedia.org/wiki/Longship"><img
src="images/vikinggif.gif" id="vikinganim" alt="row, row, row your boat"
style="width: 300px; margin-left: 425px; margin-right: auto;"></a>
</main>
<div class="bg-image"></div>
<footer>
<p id="foot_statement">Largs Model Boat Club © 2022 | Designed & Developed
by EassonWeb.com</p><br>
<a href="https://en.wikipedia.org/wiki/International_maritime_signal_flags"><img
src="images/LMBC_alphapng.png" id="LMBC_flags" style="width: 400px;"></a>
<div class="container-fluid">
<ul class="nav navbar-expand-sm" id="footer-menu">
<li class="nav-item">
<a class="nav-link" href="gallery.html">Gallery</a>
</li>
<li class="nav-item">
<a class="nav-link" href="history.html">History</a>
</li>
<li class="nav-item">
<a class="nav-link" href="news.html">News & Events</a>
</li>
<li class="nav-item">
<a class="nav-link" href="aboutlargs.html">About Largs</a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
</ul>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</body>
</html>

Instead .+ you have to use .= in this line: $body .+ "From: ".$userName. "\r\n";

UPD: the main issue here is mail function.
Look at the docs: https://www.php.net/manual/en/function.mail.php
It requires 3 arguements:
string $to,
string $subject,
string $message
But in your code only 2 argements are passed: mail($to, $body);
typos:
You can look at highlighted code and see that $body .= "Email: ".userEmail. "\r\n"; looks different rather than it's neighbors.
userEmail lacks it's $ to be a variable :)
$message_sent = 'false'; => $message_sent = false;
$body .+ "From: ".$userName. "\r\n"; => $body .= "From: ".$userName. "\r\n";

Related

New Contact Form Not Working URL was not found on this server

I'm fairly new and was wondering if anyone could help. I've just uploaded all website files via filezilla to a web hosting company and the contact form isn't working. I've tried looking into this but I can seem to find out why. Are you able to have a look at my code and see if I'm perhaps missing something.
The Contact Form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="description" content="Contact Protekt Insurance Brokers. For more information and questions you have">
<meta name="keywords" content="insurance companies, cheap car insurance, insurance brokers, insurance, personal insurance, business insurance, home insurance, life insurance, insurance agency, car insurance, insurance quotes, cheap insurance, liability insurance">
<meta name="author" content="Vaughan Testa">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css" integrity="sha256-h20CPZ0QyXlBuAw7A+KluUYx/3pK+c7lYEpqLTlxjYQ=" crossorigin="anonymous" />
<link rel="stylesheet" href="css/utilities.css" />
<link rel="stylesheet" href="css/style.css" />
<title>Contact Protekt | Protekt Insurance Brokers</title>
</head>
<body id="home">
<header class="hero blog">
<div id="navbar" class="navbar top">
<h1 class="logo">
<span class="text-primary">Protekt </span>Insurance Brokers</h1>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Documentation</li>
<li>Contact</li>
</ul>
</nav>
</div>
<div class="content">
<h1 class="text-primary">Contact Us By Email</h1>
</div>
</header>
<main>
<section id="contact" class="contact flex-columns">
<div class="row">
<div class="column">
<div class="column-1">
<img src="images/blogger-336371_1920.jpg" alt="" />
</div>
</div>
<div class="column">
<div class="column-2 bg-light">
<h2>Contact Us By Email</h2>
<form action="process.php" method="POST" id="contact_form" class="callback-form">
<div class="form-control">
<label for="name"></label>
<input type="text" name="name" id="name" placeholder="Enter name" />
</div>
<div class="form-control">
<label for="email"></label>
<input type="email" name="email" id="email" placeholder="Enter email" />
</div>
<div class="form-control">
<label for="phone"></label>
<input type="text" name="phone" id="phone" placeholder="Enter phone" />
</div>
<input type="submit" value="Send" id="submit" class="btn" />
</form>
</div>
</div>
</div>
</section>
<section id="about" class="icons bg-primary">
<h2 class="about-h2">Contact Details</h2>
<div class="flex-items">
<div class="flex-item1">
<i class="fas fa-phone fa-2x"></i>
<div>
<h3>Contact Info</h3>
<p>
Telephone: (09) 475 6602 <br>International Callers: +649 475 6602
</p>
</div>
</div>
<div class="flex-item2">
<i class=" fas fa-envelope fa-2x "></i>
<div>
<h3>Email Info</h3>
<p>
E-mail: enquiry#protekt.co.nz
</p>
</div>
</div>
<div class="flex-item3">
<i class="fas fa-address-card fa-2x"></i>
<div>
<h3>Office Location</h3>
<p>
103 Glenvar Road, <br>Torbay, Auckland 0630 <br>New Zealand <br><br>Postal Address<br>PO Box 35962 <br>Browns Bay <br>Auckland 0753 <br>New Zealand
</p>
</div>
</div>
</div>
</section>
<section id="boxes">
<div class="container-boxes">
<div class="box">
<img src="images/steadfast.gif" alt="">
</div>
<div class="box">
<img src="images/ANZ.gif" alt="">
</div>
<div class="box">
<img src="images/IBANZ.gif" alt="">
</div>
</div>
</section>
</main>
<footer class="footer bg-dark">
<div class="social">
<i class="fab fa-facebook fa-2x"></i>
<i class="fab fa-twitter fa-2x"></i>
<i class="fab fa-linkedin fa-2x"></i>
</div>
<p>Copyright © 2020 - Designed By: Vaughan Testa</p>
</footer>
<script>
const navbar = document.getElementById('navbar');
let scrolled = false;
window.onscroll = function() {
if (window.pageYOffset > 100) {
navbar.classList.remove('top');
if (!scrolled) {
navbar.style.transform = 'translateY(-70px)';
}
setTimeout(function() {
navbar.style.transform = 'translateY(0)';
scrolled = true;
}, 200);
} else {
navbar.classList.add('top');
scrolled = false;
}
};
</script>
</body>
</html>
My PHP Code
<?php
if(!$_POST) exit;
$email = $_POST['email'];
error_reporting(error_reporting() & ~E_DEPRECATED);
//$error[] = preg_match('/\b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."#"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');
$your_email = "enquiry#protekt.co.nz";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
if(#mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
}
?>

getting error Cannot modify header information - headers already [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 2 years ago.
I am creating a project on the school management system using PHP and I am stuck as it's giving me an error: Cannot modify header information - headers already sent.
If the user forgot to fill any field and click on the submit button,the server should return a message like fill this field but instead it's giving this error and I don't know why.
this is the error I am getting
this is my form
This is my home page
<?php
session_start();
require_once 'header.php';
if(!isset($_SESSION['name']))
{
die("Access Denied");
}
if(isset($_POST['logout']))
{
header('Location: logout.php');
return;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css"
integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght#400;500&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght#300;400;500&display=swap" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js#2.8.0"></script>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="../management/dashboard/css/admin.css">
<link rel="stylesheet" href="../management/student/css/addstudent.css">
<link rel="stylesheet" href="../management/student/css/allstudent.css">
<link rel="stylesheet" href="../management/student/css/studentdetail.css">
</head>
<body>
<header>
<nav>
<div class="logo">
<div class="img">
<img src="./images/logo3.png" alt="">
</div>
</div>
<ul class="nav-links">
<li id="n-1">
<div class="admin">
<p>Konark Shivam</p>
<i class="fas fa-user-circle"></i>
</div>
</li>
<li id="n-2">
<i class="fas fa-envelope"></i>
</li>
<li id="n-3">
<i class="fas fa-bell"></i>
</li>
<li id="n-4">
<i class="fas fa-sign-out-alt"></i>
</li>
</ul>
<div class="burger" id="burger">
<i class="fas fa-bars"></i>
</div>
</nav>
</header>
<!-- for logout -->
<section>
<div class="logout-box" id="logout-box">
<div class="logout-content">
<p>Do you want to logout</p>
<form method="post">
<div class="logout-button">
<input type="submit" name="logout" value="Logout">
<input type="submit" name="cancel" value="Cancel" id="logout-cancel" onclick="return false">
</div>
</form>
</div>
</div>
</section>
<menu>
<div class="left-menu" id="left-menu">
<ul>
<li class="menu-items" id="menu-item-1">
Dashboard
<span class="arrow">></span>
</li >
<div class="drop-menu" id="drop-menu-1">
<ul>
<li class="drop-items">
<div class="name"></div>
<a id="d-i-admin" href="home.php?page=dashboard/admin">> Admin</a>
</li>
<li class="drop-items">
<div class="name"></div>
> Teacher
</li>
<li class="drop-items">
<div class="name"></div>
> Students
</li>
<li class="drop-items">
<div class="name"></div>
> Parents
</li>
</ul>
</div>
<li class="menu-items" id="menu-item-2">
<span>Student</span>
<span class="arrow">></span>
</li>
<div class="drop-menu" id="drop-menu-2">
<ul>
<li class="drop-items">
<div class="name"></div>
> All Students
</li>
<li class="drop-items">
<div class="name"></div>
> Student Details
</li>
<li class="drop-items">
<div class="name"></div>
> Admission Form
</li>
<li class="drop-items">
<div class="name"></div>
> Student Promotion
</li>
</ul>
</div>
<li class="menu-items" id="menu-item3">
<span>Teacher</span>
<span class="arrow">></span>
</li>
<div class="drop-menu" id="drop-menu-3">
<ul>
<li class="drop-items">
<div class="name"></div>
> Admin
</li>
<li class="drop-items">
<div class="name"></div>
> Teacher
</li>
<li class="drop-items">
<div class="name"></div>
> Students
</li>
<li class="drop-items">
<div class="name"></div>
> Parents
</li>
</ul>
</div>
<li class="menu-items">
<span>Parents</span>
<span class="arrow">></span>
</li>
<li class="menu-items">
<span>Library</span>
<span class="arrow">></span>
</li>
<li class="menu-items">
<span>Subjects</span>
<span class="arrow">></span>
</li>
<li class="menu-items">
<span>Class Routines</span>
<span class="arrow">></span>
</li>
<li class="menu-items">
<span>Attendence</span>
<span class="arrow">></span>
</li>
<li class="menu-items">
<span>Exams</span>
<span class="arrow">></span>
</li>
<li class="menu-items">
<span>Transport</span>
<span class="arrow">></span>
</li>
<li class="menu-items">
<span>Hostel</span>
<span class="arrow">></span>
</li>
<li class="menu-items">
<span>Notice</span>
<span class="arrow">></span>
</li>
<li class="menu-items">
<span>Message</span>
<span class="arrow">></span>
</li>
<li class="menu-items">
<span>Accounts</span>
<span class="arrow">></span>
</li>
</ul>
</div>
</menu>
<main>
<div class="presentation">
<div class="my-container" id="container">
<?php
if(isset($_GET['page']))
{
$page=$_GET['page'];
include_once $page.'.php';
}
else{
include_once 'dashboard/admin.php';
}
?>
</div>
</div>
</main>
<script src="script.js"></script>
</body>
</html>
this is my addstudent page
<?php
ob_start();
if(!isset($_SESSION['name']))
{
die("Access Denied");
}
require_once 'pdo.php';
if(isset($_POST['submit']))
{
if(isset($_POST['f_name']) && isset($_POST['l_name']) && isset($_POST['gender']) && isset($_POST['dob']) && isset($_POST['blood'])
&& isset($_POST['religion']) && isset($_POST['email']) && isset($_POST['address']) && isset($_POST['phone']) )
{
if(strlen($_POST['f_name'])<1)
{
$_SESSION['error']='Please enter first name';
header('Location:home.php?page=student/addstudent');
return;
}
if(strlen($_POST['l_name'])<1)
{
$_SESSION['error']='Please enter last name';
header('Location:addstudent.php');
return;
}
if(strlen($_POST['gender'])<1)
{
$_SESSION['error']='Please enter gender';
header('Location:addstudent.php');
return;
}
if(strlen($_POST['dob'])<1)
{
$_SESSION['error']='Please select date';
header('Location:addstudent.php');
return;
}
if(strlen($_POST['blood'])<1)
{
$_SESSION['error']='Please enter blood group';
header('Location:addstudent.php');
return;
}
if(strlen($_POST['religion'])<1)
{
$_SESSION['error']='Please enter religion';
header('Location:addstudent.php');
return;
}
if(strlen($_POST['email'])<1)
{
$_SESSION['error']='Please enter email';
header('Location:addstudent.php');
return;
}
else if(!strpos($_POST['email'],"#")>0)
{
$_SESSION['error']='Please enter valid email';
header('Location:addstudent.php');
return;
}
if(strlen($_POST['address'])<1)
{
$_SESSION['error']='Please Address';
header('Location:addstudent.php');
return;
}
if(is_numeric($_POST['phone'])!=1)
{
$_SESSION['error']='Please enter valid phone no';
header('Location:addstudent.php');
return;
}
else
{
$stmpt=$pdo->prepare('INSERT INTO student_detail (f_name ,l_name ,gender, dob, blood_grp, religion, email, phone, address)
VALUES ( :fn , :ln , :sx , :dob , :bld , :re , :em , :phno , :add )');
$stmpt->execute(array(
':fn'=>$_POST['f_name'],
':ln'=>$_POST['l_name'],
':sx'=>$_POST['gender'],
':dob'=>$_POST['dob'],
':bld'=>$_POST['blood'],
':re'=>$_POST['religion'],
':em'=>$_POST['email'],
':phno'=>$_POST['phone'],
':add'=>$_POST['address']
));
$_SESSION['success']='Profile added';
header("Location: addstudent.php");
return;
}
}
}
ob_end_flush();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="../student/css/addstudent.css">
</head>
<body>
<div class="container-student-3">
<div class="page-info">
<h3>Student Form</h3>
<p>Home>Student Admit form</p>
</div>
<div class="server-message">
<?php
if(isset($_SESSION['error']))
{
echo '<p style="color:red";>'.$_SESSION['error'].'</p>';
unset($_SESSION['error']);
}
if(isset($_SESSION['success']))
{
echo '<p style="color:green";>'.$_SESSION['success'].'</p>';
unset($_SESSION['success']);
}
?>
</div>
<div class="student-form-wrap">
<div class="student-form-content">
<h3>Add New Student</h3>
<form method="post">
<div class="form-inputs">
<div class="col-xl-3 col-lg-6 col-12 form-group">
<label>First Name *</label>
<input type="text" placeholder="First Name" name="f_name" class="form-control">
</div>
<div class="col-xl-3 col-lg-6 col-12 form-group">
<label>Last Name *</label>
<input type="text" placeholder="Last Name" name="l_name" class="form-control">
</div>
<div class="col-xl-3 col-lg-6 col-12 form-group">
<label>Gender *</label>
<input type="text" placeholder="Gender" name="gender" class="form-control">
</div>
<div class="col-xl-3 col-lg-6 col-12 form-group">
<label>Date Of Birth *</label>
<input type="date" placeholder="Date of Birth" name="dob" class="form-control">
</div>
<div class="col-xl-3 col-lg-6 col-12 form-group">
<label>Blood Group *</label>
<input type="text" placeholder="Blood Group" name="blood" class="form-control">
</div>
<div class="col-xl-3 col-lg-6 col-12 form-group">
<label>Religion *</label>
<input type="text" placeholder="Religion" name="religion" class="form-control">
</div>
<div class="col-xl-3 col-lg-6 col-12 form-group">
<label>E-Mail *</label>
<input type="text" placeholder="E-male" name="email" class="form-control">
</div>
<div class="col-xl-3 col-lg-6 col-12 form-group">
<label>Address *</label>
<input type="text" placeholder="Address" name="address" class="form-control">
</div>
<div class="col-xl-3 col-lg-6 col-12 form-group">
<label>Phone *</label>
<input type="text" placeholder="Phone No" name="phone" class="form-control">
</div>
<div class="col-12 form-group mg-t-8">
<input type="submit" value="submit" name="submit">
<input type="reset" value="Reset" id="reset">
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
This error is displayed when the "header" method is called more than once in an execution scope.
Let's see there, in your code there are several "if" conditions, without the "else if" treatment and somehow a "header" may be being executed more than once.
I would recommend doing the form treatments in a "Controller" or in another file, if you prefer. But as the top friend commented, you can test by replacing "return" with "exit".
Remember that "header" must be called before any real output is sent, either by normal HTML tags, blank lines in a file, or by PHP.

error in redirect to other page using php

In my project im getting such kind of error when im trying to redirect to another page when the person submit the contact form then it should redirect to some other page but im getting such kind of error. the error what im getting is this
warning: Cannot modify header information - headers already sent by
(output started at C:\xampp\htdocs\grade\header.php:89) in
C:\xampp\htdocs\grade\contact.php on line 64
header.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>SAMSON SOLAR ENERGY</title>
<!-- Meta tag Keywords -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<meta name="keywords" content="Grade Responsive web template, Bootstrap Web Templates, Flat Web Templates, Android Compatible web template,
Smartphone Compatible web template, free webdesigns for Nokia, Samsung, LG, SonyEricsson, Motorola web design" />
<script type="application/x-javascript">
addEventListener("load", function () {
setTimeout(hideURLbar, 0);
}, false);
function hideURLbar() {
window.scrollTo(0, 1);
}
</script>
<!--// Meta tag Keywords -->
<!-- css files -->
<link rel="stylesheet" href="css/bootstrap.css"> <!-- Bootstrap-Core-CSS -->
<link rel="stylesheet" href="css/style.css" type="text/css" media="all" /> <!-- Style-CSS -->
<link rel="stylesheet" href="css/fontawesome-all.css"> <!-- Font-Awesome-Icons-CSS -->
<!-- //css files -->
<!----->
<link rel="stylesheet" href="css/flexslider.css" type="text/css" media="all" /><!-- for testimonials -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!--web font-->
<link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:200,200i,300,300i,400,400i,600,600i,700,700i,900,900i&subset=cyrillic,cyrillic-ext,greek,greek-ext,latin-ext,vietnamese" rel="stylesheet">
<!--//web font-->
</head>
<body>
<!-- header -->
<header>
<div class="container">
<nav class="navbar navbar-expand-lg navbar-light">
<a class="navbar-brand" href="index.html">
<span class="fa fa-strikethrough"></span> SAMSON SOLAR
</a>
<button class="navbar-toggler ml-md-auto" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mx-auto text-center">
<li class="nav-item active mr-lg-3">
<a class="nav-link" href="index.php">Home<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item mr-lg-3">
<a class="nav-link" href="about.php">about</a>
</li>
<li class="nav-item mr-lg-3">
<a class="nav-link" href="gallery.php">Gallery</a>
</li>
<li class="nav-item dropdown mr-lg-3">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Products</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="domestic.php">Domestic</a>
<a class="dropdown-item" href="commercial.php">Commercial</a>
<a class="dropdown-item" href="other.php">Other Products</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="contact.php">contact</a>
</li>
</ul>
<div class="buttons">
<p><i class="fas mr-1 fa-phone"></i> +91 99161 67190</p>
</div>
</div>
</nav>
</div>
<div class="icon-bar">
<i class="fa fa-facebook"></i>
<i class="fa fa-twitter"></i>
<i class="fa fa-google"></i>
<i class="fa fa-linkedin"></i>
<i class="fa fa-youtube"></i>
</div>
</header>
<!-- //header -->
contact.php
<?php include 'header.php'; ?>
<!-- banner -->
<section class="inner-banner">
<div class="banner-layer">
<h1 class="text-center">Contact Page</h1>
</div>
</section>
<!-- //banner -->
<?php
include_once 'connect.php';
$nameErr = $emailErr = $contactErr = "";
$name = $email = $contact = $subject = "";
if (isset($_POST['submit']))
{
//Name
if (empty($_POST["name"])) {
$nameErr = "Please enter your Name";
}
else{
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Sorry Only letters are allowed";
}
}
//E-mail ID
if (empty($_POST["email"])) {
$emailErr = "Please enter your E-mail ID";
}
else{
$email = test_input($_POST["email"]);
// check if the entered Email-id is valid or not
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Please enter a valid E-mail ID";
}
}
//Contact Number
if (empty($_POST["phone"])) {
$contactErr = "Please enter your Contact Number";
}
else{
$contact = test_input($_POST["phone"]);
// check weather the contact number is valid or not
if (!preg_match("/^[0-9]+$/",$contact)) {
$contactErr = "Please enter Your 10 Digit mobile Number";
}
}
//Reason For contacting
if (!empty($_POST["subject"])) {
$subject = test_input($_POST["subject"]);
}
if($nameErr == "" && $emailErr == "" && $contactErr == ""){
$sql = "INSERT INTO contact(`ID`,`Name`, `Email`, `Contact_no` , `Reason`)
VALUES ('','".$name."', '".$email."', '".$contact."' , '".$subject."')";
if (mysqli_query($mysqli, $sql)) {
header("location:contact_success.php");
exit();
}
else{
echo "Error: " . $sql . "<br>" . mysqli_error($mysqli);
}
mysqli_close($mysqli);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<!-- contact -->
<section class="contact py-5">
<div class="container">
<h2 class="heading mb-lg-5 mb-4">Contact Us</h2>
<div class="row contact-grids w3-agile-grid">
<div class="row col-md-6 col-sm-6 contact-grid1 w3-agile-grid">
<div class="col-3 text-center">
<i class="fa fa-commenting-o" aria-hidden="true"></i>
</div>
<div class="col-9 p-0">
<h4>Get In Touch</h4><br>
<p><b>Mobile:</b> +91 9916167190</p>
<p><b>E-mail:</b> abpaple#gmail.com</p>
</div>
</div>
<div class="row col-md-6 col-sm-6 mt-sm-0 mt-4 contact-grid1 w3-agile-grid">
<div class="col-3 text-center">
<i class="fa fa-map-marker" aria-hidden="true"></i>
</div>
<div class="col-9 p-0">
<h4>Visit Our Location</h4><br>
<p><b>SAMSON SOLAR ENERGY</b></p>
<p>#02, K.E.Board Complex, Navalgund Road, Shivaji Circle</p>
<p>DHARWAD-580008, KARNATAKA, INDIA</p>
</div>
</div>
</div><br><br>
<div class="row">
<div class="col-lg-7 col-md-7 col-sm-12 col-xs-12">
<div class="gmap_canvas">
<iframe width="100%" height="475" id="gmap_canvas" src="https://maps.google.com/maps?q=SAMSON%20SOLAR%20ENERGY%20%20%2302%2C%20K.E.Board%20Complex%2C%20Navalgund%20Road%2C%20Shivaji%20Circle%20%20DHARWAD-580008%2C%20KARNATAKA%2C%20INDIA&t=&z=13&ie=UTF8&iwloc=&output=embed" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>
</div>
</div>
<div class="col-lg-5 col-md-5 col-sm-12 col-xs-12">
<h2 style="margin-bottom:15px">Write To Us</h2>
<form action="" method="post">
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter Name" name="name" value="<?php if (isset($name)) echo $name; ?>">
<span style="color:red;"> <?php echo $nameErr;?></span>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Enter E-mail" name="email" value="<?php if (isset($email)) echo $email; ?>">
<span style="color:red;"> <?php echo $emailErr;?></span>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter Contact Number" name="phone" value="<?php if (isset($contact)) echo $contact; ?>">
<span style="color:red;"> <?php echo $contactErr;?></span>
</div>
<div class="form-group">
<textarea class="form-control" rows="4" placeholder="Enter Reason For Contacting" name="subject"><?php if (isset($subject)) echo $subject; ?></textarea>
</div>
<button type="submit" class="btn btn-primary pull-right" name="submit">Submit</button>
</form>
</div>
</div>
</div>
</section>
<!-- //contact -->
<?php include 'footer.php'; ?>
please can any one help me to solve my issue
There are lots of reason for the error: Cannot modify header information - headers already sent below are some of them:
print and echo statements will terminate the opportunity to send HTTP headers
<html> before any header() call.
Whitespace before <?php
Whitespace after ?>
Preceding error messages can also be a cause of this error.
If You want to check all reasons in detail then here is referenced stackoveflow answer
But for you the reason of this error is that, you have included Raw sections prior
to <?php code, you can not include html before php code.
In your code you are including header.php and <section> which is html, before php code therefor you are getting this error, so for solving it change your
contact.php file as below
<?php
include_once 'connect.php';
$nameErr = $emailErr = $contactErr = "";
$name = $email = $contact = $subject = "";
if (isset($_POST['submit']))
{
//Name
if (empty($_POST["name"])) {
$nameErr = "Please enter your Name";
}
else{
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Sorry Only letters are allowed";
}
}
//E-mail ID
if (empty($_POST["email"])) {
$emailErr = "Please enter your E-mail ID";
}
else{
$email = test_input($_POST["email"]);
// check if the entered Email-id is valid or not
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Please enter a valid E-mail ID";
}
}
//Contact Number
if (empty($_POST["phone"])) {
$contactErr = "Please enter your Contact Number";
}
else{
$contact = test_input($_POST["phone"]);
// check weather the contact number is valid or not
if (!preg_match("/^[0-9]+$/",$contact)) {
$contactErr = "Please enter Your 10 Digit mobile Number";
}
}
//Reason For contacting
if (!empty($_POST["subject"])) {
$subject = test_input($_POST["subject"]);
}
if($nameErr == "" && $emailErr == "" && $contactErr == ""){
$sql = "INSERT INTO contact(`ID`,`Name`, `Email`, `Contact_no` , `Reason`)
VALUES ('','".$name."', '".$email."', '".$contact."' , '".$subject."')";
if (mysqli_query($mysqli, $sql)) {
header("location:contact_success.php");
exit();
}
else{
echo "Error: " . $sql . "<br>" . mysqli_error($mysqli);
}
mysqli_close($mysqli);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php include 'header.php'; ?>
<!-- banner -->
<section class="inner-banner">
<div class="banner-layer">
<h1 class="text-center">Contact Page</h1>
</div>
</section>
<!-- //banner -->
<!-- contact -->
<section class="contact py-5">
<div class="container">
<h2 class="heading mb-lg-5 mb-4">Contact Us</h2>
<div class="row contact-grids w3-agile-grid">
<div class="row col-md-6 col-sm-6 contact-grid1 w3-agile-grid">
<div class="col-3 text-center">
<i class="fa fa-commenting-o" aria-hidden="true"></i>
</div>
<div class="col-9 p-0">
<h4>Get In Touch</h4><br>
<p><b>Mobile:</b> +91 9916167190</p>
<p><b>E-mail:</b> abpaple#gmail.com</p>
</div>
</div>
<div class="row col-md-6 col-sm-6 mt-sm-0 mt-4 contact-grid1 w3-agile-grid">
<div class="col-3 text-center">
<i class="fa fa-map-marker" aria-hidden="true"></i>
</div>
<div class="col-9 p-0">
<h4>Visit Our Location</h4><br>
<p><b>SAMSON SOLAR ENERGY</b></p>
<p>#02, K.E.Board Complex, Navalgund Road, Shivaji Circle</p>
<p>DHARWAD-580008, KARNATAKA, INDIA</p>
</div>
</div>
</div><br><br>
<div class="row">
<div class="col-lg-7 col-md-7 col-sm-12 col-xs-12">
<div class="gmap_canvas">
<iframe width="100%" height="475" id="gmap_canvas" src="https://maps.google.com/maps?q=SAMSON%20SOLAR%20ENERGY%20%20%2302%2C%20K.E.Board%20Complex%2C%20Navalgund%20Road%2C%20Shivaji%20Circle%20%20DHARWAD-580008%2C%20KARNATAKA%2C%20INDIA&t=&z=13&ie=UTF8&iwloc=&output=embed" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>
</div>
</div>
<div class="col-lg-5 col-md-5 col-sm-12 col-xs-12">
<h2 style="margin-bottom:15px">Write To Us</h2>
<form action="" method="post">
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter Name" name="name" value="<?php if (isset($name)) echo $name; ?>">
<span style="color:red;"> <?php echo $nameErr;?></span>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Enter E-mail" name="email" value="<?php if (isset($email)) echo $email; ?>">
<span style="color:red;"> <?php echo $emailErr;?></span>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter Contact Number" name="phone" value="<?php if (isset($contact)) echo $contact; ?>">
<span style="color:red;"> <?php echo $contactErr;?></span>
</div>
<div class="form-group">
<textarea class="form-control" rows="4" placeholder="Enter Reason For Contacting" name="subject"><?php if (isset($subject)) echo $subject; ?></textarea>
</div>
<button type="submit" class="btn btn-primary pull-right" name="submit">Submit</button>
</form>
</div>
</div>
</div>
</section>
<!-- //contact -->
<?php include 'footer.php'; ?>
And if problem remains the same after changing file as above then try to separate html file contact.php and make new php process file like contact_process.php and add php processing code only in contact_process.php.
Instead of
if (mysqli_query($mysqli, $sql)) {
header("location:contact_success.php");
exit();
}
You could do this:
if (mysqli_query($mysqli, $sql)) {
echo "<script>window.location.href = 'contact_success.php';</script>";
exit();
}
May not be conventional but it should work.

I am trying to run a mailing form in php. This is the HTML with Bootstrap 3.0

I am doing a contact page for a site that uses framework Twitter Bootstrap 3.0. This is the HTML.
<form action="mail.php" name="contactform" method="post" id="contact-form">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="name"> Name</label>
<input id="name" class="form-control" type="text" name="name" placeholder="Enter you name" value="" required/>
</div>
<div class="form-group">
<label for="email">Email Address</label>
<div id="input_email" class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
<input id="email" class="form-control" type="email" name="email" placeholder="Enter your email" value="" required />
</div>
</div>
<div class="form-group">
<label for="subject"> Subject</label>
<input id="subject" class="form-control" type="text" name="subject" placeholder="Subject"/>
</div>
</div>
<div class="col-md-6">
<div id="input_message" class="col-md-12">
<textarea id="comments" class="form-control" name="message" rows="6" placeholder="Your Suggestion ..." value="" required ></textarea>
</div>
</div>
<!-- Submit Button -->
<div class="col-md-12">
<input class="btn btn-skin pull-right" type="submit" value="submit" id="submit">
</div>
</div>
</form>
This is the PHP:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content=" Contact Form Validation.">
<meta name="author" content="Julian Vanegas">
<title>Mailing</title>
<!--Favicon-->
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<!-- Bridge -->
<link href="bridge-contactphp.css.css" rel="stylesheet" type="text/css">
</head>
<body id="page-top" data-spy="scroll" data-target=".navbar-custom">
<!-- Preloader -->
<div id="preloader">
<div id="load"></div>
</div>
<!--Navbar-->
<nav class="navbar navbar-custom navbar-fixed-top top-nav-collapse" role="navigation">
<div class="container">
<div class="navbar-header page-scroll">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-main-collapse">
<i class="fa fa-bars"></i>
</button>
<a class="navbar-brand" href="#page-top">
<img src="img/logo-svg.svg"class="logo"></img>
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-right navbar-main-collapse">
<ul class="nav navbar-nav">
<li>Home</li>
<li>About us</li>
<li>Services</li>
<li class="active">Contact</li>
<li>Sitemap</li>
<li class="dropdown">
More<b class="caret"></b>
<ul class="dropdown-menu">
<li>Globalnode Acad</li>
<li>Community</li>
<li></li>
</ul>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<?php
$form_htmlString = "";
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['subject']) || empty($_POST['message'])){
$form_htmlString = <<<EOD
<section id=\"intro\" class=\"intro\"><div class=\"gallery-intro\"><h2> <span class=\"text_color\"> <span>fadfasfads</span> </h2><h4></h4></div><div class=\"page-scroll\"><i class=\"fa fa-angle-double-down animated\"></i></div></section><div class=\"container\"> <div class=\"row\"> <div class=\"col-xs-12 col-sm-12 col-md-12 text-center padding-20\"><div class=\"wow bounceInUp\" data-wow-delay=\"0.2s\"> <div class=\"team boxed-grey\"> <div class=\"inner\"><h5 alt=\"Internal-link List\"> Check the information you typed in. </h5> <p class=\"subtitle\"> There is one or more mistakes. </p></div></div></div></div></div></div>
EOD;
}else{
$sendTo = "jumisingo#hotmail.es";
$subject ="Mensaje de contacto";
$title = "Alguien se ha contactado con usted";
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$comments = $_POST["message"];
$message = "\nAlguien se ha contactado con usted:"."\nNombre: ".$name."\nEmail: ".$email."\nAsunto: ".$subject."\nComentarios: ".$comments;
mail($sendTo, $subject, $message);
$form_htmlString = "<section id=\"intro\" class=\"intro\"><div class=\"gallery-intro\"><h2> <span class=\"text_color\"> <span>fadfasfads</span> </h2><h4></h4></div><div class=\"page-scroll\"><i class=\"fa fa-angle-double-down animated\"></i></div></section><div class=\"container\"> <div class=\"row\"> <div class=\"col-xs-12 col-sm-12 col-md-12 text-center padding-20\"><div class=\"wow bounceInUp\" data-wow-delay=\"0.2s\"> <div class=\"team boxed-grey\"> <div class=\"inner\"><h5 alt=\"Internal-link List\"> Check the information you typed in. </h5> <p class=\"subtitle\"> There is one or more mistakes. </p></div></div></div></div></div></div>";
echo $form_htmlString;
}
?>
<footer id="sitemap"><div class="container"><div class="row"><div class="col-md-12 col-lg-12"><div class="wow shake" data-wow-delay="0.4s"><div class="page-scroll marginbot-30 pull-right"><i class="fa fa-angle-double-up animated"></i></div></div><p>©Copyright 2015 - Global Node Inc. All rights reserved.</p></div></div></div></footer>
</body>
When I run the if, it should evaluate the data entered in the HTML through the ifs, and echo the html depending on the break, yet it echoes part of the php too. I have searched for solutions and I saw heredoc works, but in this case it didn't. Any clue?
"This is what the server replies." Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /home2/ina2015/public_html/gna/contact.php on line 76"
The error is in this block of code for your heredoc.
<?php
$form_htmlString = "";
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['subject']) || empty($_POST['message'])){
$form_htmlString = <<<EOD
<section id=\"intro\" class=\"intro\"><div class=\"gallery-intro\"><h2> <span class=\"text_color\"> <span>fadfasfads</span> </h2><h4></h4></div><div class=\"page-scroll\"><i class=\"fa fa-angle-double-down animated\"></i></div></section><div class=\"container\"> <div class=\"row\"> <div class=\"col-xs-12 col-sm-12 col-md-12 text-center padding-20\"><div class=\"wow bounceInUp\" data-wow-delay=\"0.2s\"> <div class=\"team boxed-grey\"> <div class=\"inner\"><h5 alt=\"Internal-link List\"> Check the information you typed in. </h5> <p class=\"subtitle\"> There is one or more mistakes. </p></div></div></div></div></div></div>
EOD;
^^^^^^^^^^^^^^ It contains spaces. There should not be any.
There should not be anything before or after your closing identifier.
Modify it to be as: (no spaces before or after EOD;)
<?php
$form_htmlString = "";
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['subject']) || empty($_POST['message'])){
$form_htmlString = <<<EOD
<section id=\"intro\" class=\"intro\"><div class=\"gallery-intro\"><h2> <span class=\"text_color\"> <span>fadfasfads</span> </h2><h4></h4></div><div class=\"page-scroll\"><i class=\"fa fa-angle-double-down animated\"></i></div></section><div class=\"container\"> <div class=\"row\"> <div class=\"col-xs-12 col-sm-12 col-md-12 text-center padding-20\"><div class=\"wow bounceInUp\" data-wow-delay=\"0.2s\"> <div class=\"team boxed-grey\"> <div class=\"inner\"><h5 alt=\"Internal-link List\"> Check the information you typed in. </h5> <p class=\"subtitle\"> There is one or more mistakes. </p></div></div></div></div></div></div>
EOD;
https://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
From the manual:
It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline.

Email form not working (Php noobie)

I just started learning php and my first project was to make a working contact form. I have typed up what I could, but for some reason there appears to be some sort of errors that php is not telling me. I have looked for solutions to the issue on another web forum like phpacademy, and to no avail. I finally came to try out stack overflow, since I have heard great things about this forum. Any help is appreciated, and thanks in advance.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Contact</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/styles.css"/>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't websites if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php
if ($_POST['submit']){
if(!$_POST['firstname']){
$error='<br /> Please Enter your first name';
}
if(!$_POST['lastname']){
$error='<br /> Please Enter your last name';
}
if(!$_POST['email']){
$error='<br /> Please Enter your email address';
}
if(!$_POST['phone']){
$error='<br /> Please Enter your phone number';
}
if(!$_POST['message']){
$error='<br /> Please Enter your message you would like to send';
}
if($_POST['email']!="" and !filter_var($_POST['email'],
FILTER_VALIDATE_EMAIL)){
$error.='<br /> Please enter a valid email address';
}
if($error){
$result='<div class="alert alert-danger"><strong>One or more errors has occurred</strong>'.$error.'</div>';
}
else {
if (mail("casey#caseywoelfle.com", "Potential Client!", "FirstName: " .
$_POST['firstname'] . "
LastName ".$_POST['lastname'] ."
Email: " .$_POST['email'] . "
Phone: ".$_POST['phone'] . "
Comment: ".$_POST['comment'])) {
$result = '<div class="alert alert-success"><strong>Thank you!</strong>I\'ll be in touch within 2 business days.</div>';
} else {
$result = '<div class="alert alert-danger"> Sorry, there was an error sending your message. Please try again later.</div>';
}
}
}
?>
<nav class="navbar">
<div class="container-full">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand honey-sb" href="#">CW</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li><a class="nLink" href="index.html">Home</a></li>
<li><a class="nLink" href="about.html">About</a></li>
<li><a class="nLink" href="portfolio.html">Portfolio</a></li>
<li><a class="nLink" href="contact.php">Contact</a></li>
</ul>
</div>
</div>
</nav>
<div class="container-fluid">
<div class="row contact-sec">
<div class="col-md-12">
<form class="form-horizontal" method="post">
<div class="well">
<h1 class="text-center header">Contact Me</h1>
<h3 class="text-center">
Do you have a project you would like me to take a look at or maybe you have an idea?<br/>Either
way don’t hesitate to send me an email using the contact form or give me a call.
</h3>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-user"></i></span>
<div class="col-md-6">
<input id="InputFirstName" name="firstname" type="text" placeholder="First Name" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-user"></i></span>
<div class="col-md-6">
<input id="InputLastName" name="lastname" type="text" placeholder="Last Name" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-envelope"></i></span>
<div class="col-md-6">
<input id="InputEmail" name="email" type="text" placeholder="Email Address" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-earphone"></i></span>
<div class="col-md-6">
<input id="InputPhone" name="phone" type="text" placeholder="Phone" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-pencil"></i></span>
<div class="col-md-6">
<textarea class="form-control" id="InputMessage" name="message" placeholder="Enter your massage for us here. We will get back to you within 2 business days." rows="7"></textarea>
</div>
</div>
<div class="well">
<div class="form-group">
<div class="col-sm-4 col-sm-offset-4 text-center">
<button type="submit" id="submit" name="submit" class="btn btn-primary btn-lg">Submit</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<footer>
<div class="container-fluid">
<div class="row" id="f-nav">
<div class="col-xs-6 col-sm-3 text-center" id="f-contact">
<h2 class="footerbolder">Contact</h2>
<img src="images/ph.png" class="img-responsive" alt=""/>
</div>
<div class="col-xs-6 col-sm-3 text-center" id="f-about">
<h2 class="footerbolder">About Us</h2>
<img src="images/ph.png" class="img-responsive" alt=""/>
</div>
<div class="col-xs-6 col-sm-3 text-center" id="f-services">
<h2 class="footerbolder">Services</h2>
<img src="images/ph.png" class="img-responsive" alt=""/>
</div>
<div class="col-xs-6 col-sm-3 text-center" id="f-follow">
<h2 class="footerbolder">Follow Me</h2>
<img src="images/ph.png" class="img-responsive" alt=""/>
</div>
</div>
<div class="row">
<div class="hidden-xs col-sm-12" id="pp-tos">
<div class="center-block text-center">
<a class="" href="#">Terms of Service</a>
<a>|</a>
<a class="" href="#">Privacy Policy</a>
</div>
</div>
</div>
</div>
</footer>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script src="js/Fittext/jquery.fittext.js"></script>
</body>
</html>
#B-and-P is right, if all your error checking validates, then your error variable is undefined by the time you reach your
if($error)
statement. To fix this, simply declare your error variable at the top to be null like
$error = NULL;
that way when you check for it, it is defined and if there is no errors, your script will send your mail.
There is no action attribute in your form tag. Example:
<form class="form-horizontal" method="post" action="send_form_email.php">
cut the PHP script from your code and put it in a separate file. Save as send_form_email.php
http://askguyson.com/simple-form-to-email-php-contact-form/

Categories