I copied the code for a form to my website. It doesn't submit... Can someone see the problem?
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "danielko#intrahouse.co.il";
$email_subject = "פנייה מהאתר";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo " מלא את כל הפרטים בבקשה.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died(' אחד הפרטים חסר. מלא את כל הפרטים בבקשה');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "נא מלא פרטים נכונים";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'נא מלא פרטים נכונים.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" dir"=rtl">
<meta name="keywords" content="פורום בית חכם,בית חכם, חשמל חכם, בקרת חשמל, ניהול חשמל, חסכון בחשמל, אמצעי בקרת חשמל, כמה עולה בית חכם, מחירי חשמל חכם, מערכות בית חכם, תכנון בית חכם">
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="contact_style.css" type="text/css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery.uniform.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(e){
$("input:checkbox, input:radio").uniform();
$('#top').css('float','none');
$('.content_window').css('height','auto');
});
</script>
<title>IntraHouse - צור קשר</title>
</head>
<body>
<div class="background">
<div class="content_window">
<header>
<div class="languages">
English
עברית
</div>
<div id="app_theme" style="margin-top:3px" ></div>
<div id="top" style="margin-top:2px" style="float:none" >
<nav id="topmenu">
<ul>
<li id="home">דף הבית </li>
<li id="about">אודות </li>
<li id="prices">מחירים</li>
<li id="projects">פרוייקטים</li>
<li id="store">חנות</li>
<li id="contact_us">צור קשר</li>
</ul>
</nav>
</div>
</header>
<div class="contact" style="margin-top:10px">
<article dir="rtl">
<h1 style="margin-top:10px"> נשמע מעניין? השאירו פרטים ונחזור אליכם</h1>
<form>
<ul>
<li>
<label for="name"> שם: </label>
<input type="text" size="40" id="name" />
</li>
<li>
<label for="telephone"> טלפון: </label>
<input type="text" size="40" id="name" />
</li>
<li>
<label for="email"> כתובת אימייל: </label>
<input type="email" size="40" id="email" />
</li>
</li>
<li>
<label for="message">תוכן ההודעה:</label>
<textarea cols="50" rows="5" id="message"></textarea>
</li>
</ul>
<p>
<button type="reset" class="right"> אפס </button>
<button type="submit" class="action"> שלח </button>
</p>
</form>
</article>
</div>
</div>
</body>
</html>
<?php
}
?>
HTML appears at the end, as you can see. All buttons work and email # check work, but the form submmision just opens an empty page.
Your form has no method or action I believe this doesn't work in some browsers.
Change
<form>
To:
<form method="post" action="[the url of the page]">
The default method for a form is GET so if you don't specify it in the form tag, you would need $_GET instead of $_POST.
You also need an action attibute, so your tag should look like:
<form action="" method="post">
Then you would also need to add name attributes to all your form fields as that is what gets sent to the server, not the id.
Here are two crucial problems. The id of an HTML element needs to be unique, so here you have two elements with the same id:
<li>
<label for="name"> שם: </label>
<input type="text" size="40" id="name" />
</li>
<li>
<label for="telephone"> טלפון: </label>
<input type="text" size="40" id="name" />
</li>
This needs to change, and also each element needs a name attribute, or it will not appear in the POST array properly:
<li>
<label for="name"> שם: </label>
<input type="text" size="40" id="name" name="name" />
</li>
<li>
<label for="telephone"> טלפון: </label>
<input type="text" size="40" id="telephone" name="telephone" />
</li>
Etc. - all element must have a name and a unique id. In particular, your email field needs name="email" so that your if(isset($_POST['email'])) check at the beginning of the script will work. Also, the form needs the method POST in order for those elements to pass into the POST array. If you simply omit the action, the form will submit to the same page:
<form method="POST">
your <form> should have method="POST" attribute
like this :
<form method="POST">
You have errors :
<input type="email" size="40" id="email" />
it should be
<input type="text" size="40" id="email" name="email" />
See, you have $_POST['first_name'] in your code, so you must have <input type="text" name="first_name" /> , a name attribute is a must.
in order to get the value of an input field in php you have to use name attribute
Related
I have a form:
When you press submit the page redirect to another (I embedded the .php in a page) where the errors are shown like "Name and Last Name are not valid" but I want to make when you press Submit and the text is not valid the standard Form HTML to see like "You need to insert email" or "This field is required". I have a headache because I tried a lot of thigs but nothing seems to work.
/*
* Template Name: ThankyouPage
*/
//wp_header();
get_header();
if(is_single() || is_page())
include ('inc/page_header_transparent.php');
else
include ('inc/page_header.php');
?>
<head>
<link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri();?>/css/bootstrap.min.css">
<link href="<?php echo get_stylesheet_directory_uri();?>/css/pages.scss">
<link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri();?>/css/default_thank_you.css">
</head>
<style type="text/css">
body {
min-width: 300px;
font-family: 'Akkurat-Regular', sans-serif;
background-color: #fffffe;
color: #1a1a1a;
text-align: center;
word-wrap: break-word;
-webkit-font-smoothing: antialiased;
}
</style>
<?php
if(isset($_POST['email'])) {
$email_to = "";
$email_subject = "";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
$errors = array(
'nume' => false,
);
$values = array(
'nume' => isset($_GET['nume']) ? $_GET['nume'] : "",
);
if (isset($_POST['nume'])) {
if (!isset($_POST['nume']) || empty($_POST['nume'])) {
$errors['nume'] = true;
}
}
$first_name = $_POST['nume'];
$email_from = $_POST['email'];
$telephone = $_POST['telefon'];
$city = $_POST['localitate'];
$comments = $_POST['detalii'];
$date = $_POST['data'];
$specializare = $_POST['specializare'];
$medic = $_POST['medic']; // required
$agreement= $_POST['agree']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "<h2></h2>\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "<br>Name: ".clean_string($first_name)."\n";
$email_message .= "<br>Email: ".clean_string($email_from)."\n";
$email_message .= "<br>Telephone: ".clean_string($telephone)."\n";
$email_message .= "<br>City: ".clean_string($city)."\n";
$email_message .= "<br>Comments: ".clean_string($comments)."\n";
$email_message .= "<br>Date: ".clean_string($date)."\n";
$email_message .= "<br>Speciality: ".clean_string($specializare)."\n";
$email_message .= "<br>Medic: ".clean_string($medic)."\n";
$email_message .= "<br><h3>Contact him now!</h3>";
// create email headers
$headers = "From: contact#template.com\r\n".
"Content-type:text/html;charset=UTF-8" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link href='https://fonts.googleapis.com/css?family=Lato:300,400|Montserrat:700' rel='stylesheet' type='text/css'>
<style>
#import url(//cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.min.css);
#import url(//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css);
</style>
<script src="https://2-22-4-dot-lead-pages.appspot.com/static/lp918/min/jquery-1.9.1.min.js"></script>
<script src="https://2-22-4-dot-lead-pages.appspot.com/static/lp918/min/html5shiv.js"></script>
</head>
<body>
<header class="site-header" id="header">
<h1 class="site-header__title" data-lead-id="site-header-title">SUCCES!</h1>
</header>
<div class="main-content">
<i class="fa fa-check main-content__checkmark" id="checkmark"></i>
<p class="main-content__body" data-lead-id="main-content-body"></p>
</div>
</body>
</html>
<?php
}
?>
<?php
include ('inc/page_footer.php');
?>
<?php
get_footer();
?>
<form method="POST" id="contactForm" novalidate class="contact-form" action="/form">
<fieldset>
<legend><span class="number">1</span> Informatii Personale</legend>
<div class="row">
<div class="col-md-12 form-group">
<input type="text" class="form-control" name="nume" id="nume" placeholder="Nume Prenume *" required="" data-error="Completati numele dvs.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Email *" required="" data-error="Tastati o adresa de email valida">
<span class="error">* <?php echo $email_error;?></span>
</div>
<div class="col-md-6 form-group">
<input type="text" class="form-control" id="telefon" name="telefon" placeholder="Telefon *" required="" data-error="Tastati numarul corect de telefon">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<input type="text" class="form-control" id="localitate" name="localitate" placeholder="Localitatea *" required="" data-error="Completati localitatea dvs.">
<div class="help-block with-errors"></div>
</div>
</div>
</fieldset>
<fieldset>
<legend><span class="number">2</span> Ce va supara?</legend>
<div class="row">
<div class="col-md-12 form-group">
<textarea name="detalii" id="detalii" class="form-control" placeholder="detaliati aici ce va supara ....." required="" data-error="completati simtomatologia"></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label for="data">Cand doresti programarea?</label>
<input class="mb0 form-control" type="date" name="data" id="data" required="" data-error="Completati data">
</div>
<div class="col-md-6 form-group">
<label for="specializare">Specializare:</label>
<select id="specializare" class="form-control" name="specializare" required="" data-error="Selectati o optiune">
<option value="" selected="" disabled="">Selecteaza</option>
<?php if( have_rows('specializare') ): ?>
<?php while( have_rows('specializare') ): the_row();
$medic = get_sub_field('medic');
?>
<option value="<?php echo $medic;?>"><?php echo $medic;?></option>
<?php endwhile; ?>
<?php endif; ?>
</select>
</div>
</div>
</fieldset>
<fieldset>
<div class="row">
<div class="col-md-12">
<legend><span class="number">3</span>Medic</legend>
<div class="form-group">
<select id="medic" name="medic" class="form-control" required="" data-error="Selectati medic">
<option value="" selected="" disabled="">Selecteaza medic</option>
<?php if( have_rows('formular') ): ?>
<?php while( have_rows('formular') ): the_row();
$medic = get_sub_field('medic');
?>
<option value="<?php echo $medic;?>"><?php echo $medic;?></option>
<?php endwhile; ?>
<?php endif; ?>
</select>
</div>
<div class="form-group" id="medicf" style="display:none">
<select id="medic" name="medic" class="form-control" required="" data-error="Alege medicul">
<option value="" selected="" disabled="">Alege medicul</option>
</select>
</div>
<div class="help-block with-errors"></div>
</div>
</div>
</fieldset>
<div class="row m-t15">
<div class="col-md-12">
<label class="gdpr">
<input type="checkbox" name="agree" id="agree" required="" data-error="Bifeaza">
Am citit si sunt de acord cu Termeni si Conditii si Politica GDPR </label>
<button type="submit" name="submit" id="submit" class="font-weight-500 text-uppercase btn disabled" style=" border-radius: 8px; background: #5CDB94; pointer-events: all; cursor: pointer;">Trimite solicitarea</button>
<div class="clearfix"></div>
</div>
</div>
</form>
Very simple! First prevent default action of submit button then validate and finally trigger submit.
Use this after the end tag
<script>
$(document).ready(()=>{
$("#contactForm").submit(function (e) {
//e.preventDefault(); //uncomment if form still submits
//e.stopImmediatePropagation(); //uncomment if form still submits
//now validate fields
if($('#nume').val().lenght < 3){
$('#nume').next().text('name is too short');
return false;
}else if($('#email').val().length < 5 && !$('#email').val().includes('#')){
$('#email').next().text('invalid email');
return false;
}
//else if....
else{
//$("#contactForm").submit();
//or
return true;
}
});
});
</script>
Note : This is untested code.
I'm new to PHP and I've got issue in my contact form. When I press "Submit" in my form it skips (somewhere, I don't know where and why it happens) to other site, and conditions are not checked by the php code. What's more, you can type wrong answer in "human recognizer" and it will still send and email.
I was looking for som bad declarations or wrong syntax, but all seems to be good. I assume that also my contact.php responds properly if it sends an email (but without checking conditions).
I don't know if it's connected but my modal window in it also doesn't want to close (but on the other site the same code works fine, when there is other form withoud "action=contact.php" field).
My main head.php:
<!--HEAD-->
<head>
<title>X</title>
<!--META-->
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="SitePoint">
<!--CSS-->
<link id="theme" rel="stylesheet" href="css/light.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Font Awesome -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<!--END OF HEAD-->
<body>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script>
<script src="js/dropdown.js"></script>
<script src="js/scrolling-nav.js"></script>
<script src="js/theme-switch.js"></script>
<script src="js/nav-position.js"></script>
<nav id="mainNav">
<bar>
<i id="hamburger" class="fa fa-bars" aria-hidden="true"></i>
</bar>
<ul id="menu">
<li>Main</li>
<li>Generator</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
<section id = "main" >
<div class = "content">
<h1>Hello!</h1>
<p>:)</p>
</div></section>
<section id = "generator">
<div class = "content">
<h1>Generator</h1>
<form id="generator-form" ="form-horizontal" role="form" method="post" action="generator.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="idCardNumber" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="idCardNumber" name="idCardNumber" placeholder="Student ID Card Number" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div></section>
<section id = "about">
<div class = "content">
<h1>About</h1>
<p></p>
</div></section>
<section id="contact">
<div class="content">
<h1>Contact</h1>
<p><a class="btn btn-default btn-lg" href="#contact-form">Contact Us</a></p>
<p><iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d5122.204450340393!2d19.91387757798398!3d50.065647167696376!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x47165ba756b59b21%3A0xb20c8dba21b317d1!2sAkademia+G%C3%B3rniczo-Hutnicza+im.+Stanis%C5%82awa+Staszica+w+Krakowie!5e0!3m2!1spl!2spl!4v1511628584066" width="500rem" height="500rem" frameborder="0" style="border:0" allowfullscreen></iframe></p>
</div>
</section>
<footer>
<label class="switch">
<input type="checkbox" onchange=" switchTheme(this)">
<span class="slider"></span>
</label>
<p>Copyright©2017 for </p>
</footer>
<!--SIGN UP-->
<div id="contact-form" class="modal-window">
<div>
Close
<form action="contact.php">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<p class="text-danger"><?php echo $errName; ?></p>
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<p class="text-danger"><?php echo $errEmail; ?></p>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" rows="4" name="message" value="<?php echo htmlspecialchars($_POST['message']);?>"></textarea>
<p class="text-danger"><?php echo $errMessage; ?></p>
</div>
<div class="form-group">
<label for="human">1 + 1 = ?</label>
<input type="text" class="form-control" id="human" name="human" pattern=".{1,}" required title="At least 1 character required" placeholder="Your Answer">
<p class="text-danger"><?php echo $errHuman; ?></p>
</div>
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary btn-lg"></input>
<div class="form-group">
<?php echo $result; ?>
</div>
</form>
</div>
</div>
<!--END SIGN UP-->
<!--CONTACT FORM-->
<div id="contact-form" class="modal-window">
<a title="Close" class="modal-close">Close</a>
<form id="contactForm" role="form" method="post" action="contact.php">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" pattern=".{3,}" required title="At least 3 characters required" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" pattern=".{3,}" required title="At least 3 characters required" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" rows="4" pattern=".{3,}" required title="At least 3 characters required" name="message">
<?php echo htmlspecialchars($_POST['message']);?>
</textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
<div class="form-group">
<label for="human">1 + 1 = ?</label>
<input type="text" class="form-control" id="human" name="human" pattern=".{1,}" required title="At least 1 character required" placeholder="Your Answer">
<p class='text-danger'>$errHuman</p>
</div>
<input name="submit" type="submit" value="Send" class="btn btn-primary btn-lg">
<div class="form-group">
<?php echo $result; ?>
</div>
</form>
</div>
<!--CONTACT FORM-->
</body>
</html>
My contact.php code:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Generator Contact';
$to = 'kamykx#gmail.com';
$subject = 'Message from AGH Generator Form';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
//CHECK NAME
if (!$name || empty($name)) {
$errName = 'Please enter your name';
}
//CHECK EMAIL
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($email)) {
$errEmail = 'Please enter a valid email address';
}
//CHECK MESSAGE
if (!$message || empty($message)) {
$errMessage = 'Please enter your message';
}
//CHECK IF USER IS NOT A BOT
if ($human !== 2 || $human !=2) {
$errHuman = 'Please... proof that you are not a bot :>';
}
//SEND THE EMAIL IF THERE ARE NO EXISTING ERRORS
if (!empty($errName) && !empty($errEmail) && !empty($errMessage) && !empty($errHuman)) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! We will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
header("Location: home.php");
}
}
?>
EDIT:
I've done improvements in my php code (there was logical problem in line error fields are empty: was -> if(!empty($errName)) but should be -> if(empty($errName)).
But I've still got and issue. I've compressed the code in order to stay on the same page after contact form submit, but when we click the "submit" button nothing appears (no errors are displayed), page only refreshes and open the form again. What's wrong now? NEW CODE:
<!DOCTYPE HTML>
<html lang="en">
<!--HEAD-->
<head>
<title>AGH Application for entry with ECTS deficit generator</title>
<!--META-->
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="SitePoint">
<meta name="Description" content="It is simple PDF generator for sing with lack of ECTS for another term" />
<meta name="Keywords" content="ECTS, deficit, deficyt, Poland, Cracow, generator, application, form, pdf, AGH, UST, Akademia, Górniczko, Hutnicza, University, S cience, Technology, Polska, Kraków, " />
<!--CSS-->
<link id="theme" rel="stylesheet" href="css/dark.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Font Awesome -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<!--END OF HEAD-->
<body>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script>
<script src="js/dropdown.js"></script>
<script src="js/scrolling-nav.js"></script>
<script src="js/theme-switch.js"></script>
<script src="js/nav-position.js"></script>
<nav id="mainNav">
<bar>
<i id="hamburger" class="fa fa-bars" aria-hidden="true"></i>
</bar>
<ul id="menu">
<li>Main</li>
<li>Generator</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
<section id = "main" >
<div class = "content">
<h1>Hello!</h1>
<p>Welocome to The AGH Application for entry with ECTS deficit generator website. We hope that you use it just for fun :)</p>
</div></section>
<section id = "generator">
<div class = "content">
<h1>Generator</h1>
</div></section>
<section id = "about">
<div class = "content">
<h1>About</h1>
<p>This webapge was created as a project for the Web Technologies. The main reason why it exists is that very common among Students is that they want to apply for entry on another term with ECTS deficit. This site will help students and AGH employees by generating PDF application. We hope that everything at AGH will be fast and growing in the future. We want to make our students life BETTER! </p>
</div></section>
<section id="contact">
<div class="content">
<h1>Contact</h1>
<p><a class="btn btn-default btn-lg" href="#contact-form">Contact Us</a></p>
<?php echo $result; ?>
<p><iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d5122.204450340393!2d19.91387757798398!3d50.065647167696376!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x47165ba756b59b21%3A0xb20c8dba21b317d1!2sAkademia+G%C3%B3rniczo-Hutnicza+im.+Stanis%C5%82awa+Staszica+w+Krakowie!5e0!3m2!1spl!2spl!4v1511628584066" width="500rem" height="500rem" frameborder="0" style="border:0" allowfullscreen></iframe></p>
</div>
</section>
<footer>
<label class="switch">
<input type="checkbox" onchange=" switchTheme(this)">
<span class="slider"></span>
</label>
<p>Copyright©2017 Marcin Kamiński for AGH </p>
</footer>
<!--SIGN UP-->
<?php
if (isset($_POST["send"])) {
$name = $_POST['name']; //Getting variable from form
$email = $_POST['email']; //Getting variable from form
$message = $_POST['message']; //Getting variable from form
$human = intval($_POST['human']); //Getting variable from form
$from = 'Generator Contact'; //Set sender
$to = 'kamykx#gmail.com'; //Where to send an email
$subject = 'Message from AGH Generator Form'; //Set the subject of email
$errName = $errEmail = $errMessage = $errHuman = ''; //Values of errors
$body ="From: $name\n E-Mail: $email\n Message:\n $message"; //Body of email
//CHECK NAME
if (empty($name)) {
$errName = 'Please enter your name';
}
//CHECK EMAIL
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || empty($email)) {
$errEmail = 'Please enter a valid email address';
}
//CHECK MESSAGE
if (empty($message)) {
$errMessage = 'Please enter your message';
}
//CHECK IF USER IS NOT A BOT
if ($human !== 2 || $human !=2) {
$errHuman = 'Please... proof that you are not a bot :>';
}
//SEND THE EMAIL IF THERE ARE NO EXISTING ERRORS
if (empty($errName) && empty($errEmail) && empty($errMessage) && empty($errHuman)) {
if (mail($to, $subject, $body, $from)) {
$result = '<div class="alert alert-success">Thank You! We will be in touch</div>';
}
else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
<div id="contact-form" class="modal-window">
<div>
Close
<form id="contactForm" role="form" method="post">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<p class="text-danger"><?php echo $errName; ?></p>
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<p class="text-danger"><?php echo $errEmail; ?></p>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" rows="4" name="message" value="<?php echo htmlspecialchars($_POST['message']);?>"></textarea>
<p class="text-danger"><?php echo $errMessage; ?></p>
</div>
<div class="form-group">
<label for="human">1 + 1 = ?</label>
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<p class="text-danger"><?php echo $errHuman; ?></p>
</div>
<button id="send" name="send" type="submit" value="Send" class="btn btn-default btn-lg">Send</button>
</form>
</div>
</div>
<!--END SIGN UP-->
</body>
</html>
Your issue is in the logic of this line:
if (!empty($errName) && !empty($errEmail) && !empty($errMessage) && !empty($errHuman)) {
Basically this is saying if everything is wrong, send the email. The not empty check means that there was an error and the variable now holds the error string.
Instead, you just need to create the variables as an empty string and check if they are still empty:
$errName = $errEmail = $errMessage = $errHuman = '';
// CHECK NAME... etc... all the checks
if (empty($errName) && empty($errEmail) && empty($errMessage) && empty($errHuman)) {
However, you're not displaying the error to the user, and you're ending up with a bunch of loose variables. I recommend a slightly different approach using an array of errors...
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$errors = [];
//CHECK NAME
if (!$name || empty($name)) {
$errors['name'] = 'Please enter your name';
}
//CHECK EMAIL
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($email)) {
$errors['email'] = 'Please enter a valid email address';
}
//etc...
if (empty($errors)) {
//send email
} else {
$result = '<div class="alert alert-danger">Sorry there was an error sending your message:<br>';
foreach ($errors as $key => $error) {
$result .= $error . '<br>';
}
$result .= '</div>';
}
However, if you immediately call a header function after this, the user will never see the error or get a chance to fix it. You can use a query string to send errors back to head.php and display them.
(FYI, the action of a form sends all the POST data to that file, and if there is no action specified, then the form posts to itself.)
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
This is my first time working in php and I'm running into issues getting mail() to work. I am using MAMP Pro to test (and normally there is a real email address to send to). When I click the send button it clears the form but does not show the thank you message (it also isn't showing the two dividers on the bottom that are just stripes) and I have yet to receive a successful email. Any help is appreciated!
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!--[if IE]><meta http-equiv="cleartype" content="on" /><![endif]-->
<title>Twin City Grill</title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="styles/normalize.css">
<link rel='stylesheet' type="text/css" href="styles/main.css">
</head>
<body>
<nav class="navbar">
<a href="index.php">
<img class="logo" src="assets/Twin-City-Grill-white.png" alt="Twin City Grill, white blocky serif font">
<h1 class="screen-reader-only">Twin City Grill</h1>
</a>
<div class="links-container">
Contact
</div>
</nav>
<div class="wrapper">
<div class="divider"></div>
<div class="divider"></div>
<div class="contact-form">
<?php
$action=$_REQUEST['action'];
if ($action=="")
{
?>
<form action="" method="POST" enctype="multipart/form-data" role="form" id="contact-form">
<input type="hidden" name="action" value="submit">
<h2>Contact Us</h2>
<p class="form-instructions"><em>All fields are required.</em></p>
<div class="form-group">
<label for="name" class="screen-reader-only">Name:</label>
<input type="text" name="name" id="name" placeholder="Name" required="required">
</div>
<div class="form-group">
<label for="phone" class="screen-reader-only">Phone Number:</label>
<input type="tel" name="phone" id="phone" placeholder="Phone Number" required="required">
</div>
<div class="form-group">
<label for="email" class="screen-reader-only">Email Address:</label>
<input type="email" name="email" id="email" placeholder="Email Address" required="required">
</div>
<div class="form-group">
<label for="message" class="screen-reader-only">Message:</label>
<textarea name="message" id="message" cols="30" rows="10" placeholder="Your Message" required="required"></textarea>
</div>
<div class="form-group">
<input id="submit" name="submit" type="submit" value="Send" >
</div>
</form>
<?php
}
else
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$phone=$_REQUEST['phone'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message=="")||($phone==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Telephone: ".clean_string($phone)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
mail("testemail#gmail.com", $subject, $email_message, $from);
?>
<h2>Thanks!</h2>
<p>We will get back to you as soon as possible. Have a great day!</p>
<?php
}
}
?>
</div>
<div class="divider"></div>
<div class="divider"></div>
</div>
<footer>
</footer>
</body>
</html>
It looks like the mail() function is failing silently for whatever reason. try something like this to get the actual error:
$success = mail("testemail#gmail.com", $subject, $email_message, $from);
if (!$success) {
echo error_get_last()['message'];
}
Instead of $from variable, try this
$from = 'From: from#example.com' . "\r\n" .
'Reply-To: from#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Also check your spam
i do have a little problem with my contact.php :(
I needed for my page a contact form (very simple, just name, email and message).
It works perfectly but it sends the message even if the inputs are empty. My inputs do have "required" but it doesn't seem to work. My php skills are not that great. But you understand for sure where the problem is.
If there are empty fields, the form should not send the message and show an alert.
My current code:
HTML
<form method="post" id="myform" action="contact.php">
<div class="field half first">
<label for="name">Ihr Name</label>
<input type="text" name="name" id="name" required="required">
</div>
<div class="field half">
<label for="email">Ihre E-Mail</label>
<input type="text" name="email" id="email" required="required">
</div>
<div class="field">
<label for="message">Ihre Nachricht</label>
<textarea name="message" id="message" rows="5" required="required"></textarea>
</div>
<ul class="actions">
<li>
Senden
</li>
</ul>
</form>
contact.php
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'mail#gmail.com';
$subject = 'Nachricht von '.$field_name;
$body_message = 'Von: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Nachricht: '.$field_message;
$headers = 'Von: '.$field_email."\r\n";
$headers .= 'Antworte an: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Ich bedanke mich für Ihre Nachricht. Bald werde ich Sie kontaktieren./ Thank you for your message.');
window.location = 'http://';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Senden fehlgeschlagen./ Could not send the message');
window.location = 'http://';
</script>
<?php
}
?>
Any ideas, how to fix that small problem? :(
I think if your goal is to prevent someone send a blank message/contact, just use "required" in the end of your input tag like this :
<input type="text" name="something" required/>
or in your case, it should be like this :
<form method="post" id="myform" action="contact.php">
<div class="field half first">
<label for="name">Ihr Name</label>
<input type="text" name="name" id="name" required>
</div>
<div class="field half">
<label for="email">Ihre E-Mail</label>
<input type="text" name="email" id="email" required>
</div>
<div class="field">
<label for="message">Ihre Nachricht</label>
<textarea name="message" id="message" rows="5" required></textarea>
</div>
<ul class="actions">
<li>
Senden
</li>
</ul>
</form>
The "required" is a simple html5 tag attribute that helps you prevent someone send a blank form. I hope it works on you, make sure you are working with html5.
There is a function in PHP called empty(). You can combine this with the $_POST function like this example:
<?php
// The form is submitted.
if(isset($_POST["submit_button"])) {
// Now check if the posted input element is empty, if empty stop by echo a error message
// Otherwhise continue executing script
if(empty($_POST["form_name"])) {
echo "You forgot to fill in this form-element.";
}else{
// Continue
}
}
?>
Read more about this function: http://php.net/manual/en/function.empty.php
Check if the form is submitted using isset() function:
isset — Determine if a variable is set and is not NULL
Then, check if the fields are filled using empty() function:
empty — Determine whether a variable is empty
// this will return true if the Submit Button is clicked
if(isset($_POST["submit_button"])) {
if(empty($_POST['name']) {
echo "Name is not filled";
} else if (empty($_POST['email']) {
echo "Email is not filled";
} else if (empty($_POST['message']) {
echo "Message is not filled";
} else {
// Your original code
}
I have been searching the web for two days for some sort of PHP template (because I do not know how to code in PHP) that could highlight a borders red if the user did not enter all the proper information. I manage to get the contact form working but not the way I want. Essentially, I need the user to enter proper information; if not, a red border appears.
Current Situation:
[Basic Contact Form: No Validation]
Desired Situation:
[Validating Contact Form]
*
Name (Required)(If Error: Red Border)
Email (Required: At least including the # sign)(If Error: Red Border)
Subject (Required)(If Error: Red Border)
Message (Required)(If Error: Red Border)
*
My site is: www.haildark.com, if you want to see it in its full detail. Please help if you can. I think my brain is about to implode again. Thank you. It does not have to be in PHP. As long as the code gets the job done, its perfectly fine with me.
HTML:
<!-- Contact -->
<div class="wrapper wrapper-style5">
<article id="contact" class="container small">
<header>
<h2>
Contact Us
</h2>
<span>
If you have any questions, comments, or concerns, please contact below and we will respond as soon as we can.
Please allow us at least 72 hours to respond. Thanks for supporting us.
</span>
</header>
<div>
<div class="row">
<div class="12u">
<form method="post" action="contact.php">
<div>
<div class="row half">
<div class="6u">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" name="email" id="email" placeholder="Email" />
</div>
</div>
<div class="row half">
<div class="12u">
<input type="text" name="subject" id="subject" placeholder="Subject" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" id="message" placeholder="Message">
</textarea>
</div>
</div>
<div class="row">
<div class="12u">
<a href="#" class="button form-button-submit">
Send Message
</a>
<a href="#" class="button button-alt form-button-reset">
Clear Form
</a>
</div>
</div>
</div>
</form>
</div>
</div>
Basic PHP:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
$mail_to = 'info#domain.com';
$subject = 'HailDark® User: '.$field_subject." - ".$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers = 'Reply To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to info#domain.com');
window.location = 'index.html';
</script>
<?php
}
?>
I have seen that you are using HTML5 but not completely. You used:
<input type="text" name="email" id="email" placeholder="Email" />
You can use
<input type="email" name="email" id="email" placeholder="Email" required="required"/>
Html5 provides powerful way to manage your site like as the above line I used type="email" it automatically validates the email and also required="required" attribute provides you this field must not be empty it will highlighted you.
So use at first <!DOCTYPE html> before <html> tag and use html5 tags and use required="required" attribute for every tag where you want to validate the field.
<?php
if(empty($_POST['test'])) // other validation
{
$error = true;
}
?>
<input type="text" name='test' class='<?php echo ($error)?"red-border":"green-border"; ?>' />
<input type='submit' value='send' />
<style type="text/css">
.red-border{
border: 1px solid red;
}
.green-border{
border: 1px solid green;
}
</style>