Phone field is breaking my contact form [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I am trying to add a telephone field into a contact form but when I do, the script stops working and I don´t receive an email.
<?php
// check if fields passed are empty
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['telephone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No se ha introducido toda la información.";
return false;
}
$name = $_POST['name'];
$email_from = $_POST['email'];
$telephone = $_POST['telephone'];
$message = $_POST['message'];
// create email body and send it
$to = 'h.........#gmail.com'; // put your email
$email_subject = "Formulario de Contacto Con la Mochila al Hombro";
$email_body = "Has recibido un mensaje desde el formulario de contacto de la Web. \n\n".
"Aquí están los detalles:\n\n".
"Nombre: $name \n\n".
"Email: $email_from\n\n".
"Teléfono: $telephone\n\n".
"Mensaje: $message";
$headers = 'From: '.$email_from."\r\n".
'X-Mailer: PHP/' . phpversion();
mail($to,$email_subject,$email_body,$headers);
return true;
?>
And this is the html I am using:
<div class="control-group">
<div class="controls">
<input type="phone" class="form-control" placeholder="Teléfono" id="telephone" required data-validation-required-message="Por favor, dinos tu teléfono para que podamos contactar contigo." />
</div>
</div>
The form works perfectly fine if I don´t put the telephone in the php script, but as soon as I add it, it stops working. I am assuming I am setting some values wrong but I really don´t know much abut PHP.
I believe now that it is a problem with the AJAX validation script.You can see the script live in here http://talleresnaj2.com/js/contacto.min.js

Your <input /> doesn't have a name attribute, hence its value is never sent to the PHP script, making empty($_POST['telephone']) return true.
You just need to add name="telephone" in your input.

You forgot to add the input name.
Try
<input name="telephone" type="phone" class="form-control" placeholder="Teléfono" id="telephone" required data-validation-required-message="Por favor, dinos tu teléfono para que podamos contactar contigo." />

Related

How to send a email with attachments with php email [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Send attachments with PHP Mail()?
(16 answers)
Closed 3 years ago.
I have a html bootstrap form and have a php code that sends a email of form information. This has a google reCaptcha check before the email is sent.
I copied the code that does the validation of the text, the email sending and the recaptcha from another php form that works.
How can I make the php code send the 2 images it receives from the form bootstrap form as attachments in the email.
These are my file upload form elements
<form role="form" class="form" action='https://opeturmo.com/parapente_ecuador/competencia_form.php' method='post'>
<div class="form-group row">
<div class="col-12"><label for="photo">Adjunte su foto - Add a photo of you</label></div>
<div class="col-12"><p>IMPORTANTE: Adjunta una foto de tu cara. No de tu parapente o alguna otra cosa. Máx 10MB Tamaño: mayor de 400*400 px –IMPORTANT: A photo of your face. Not your glider or anything else. Max 10MB Size: not less than 400*400 px</p></div>
<div class="col-12"><input type="file" class="form-control-file" id="photo" accept="image/*" name="my_file"></div>
</div>
<div class="form-group">
<label for="pago">Adjunte su foto de pago - Add your payment photo</label>
<p>IMPORTANTE: Adjunta una foto de tu cara. No de tu parapente o alguna otra cosa. Máx 10MB Tamaño: mayor de 400*400 px –IMPORTANT: A photo of your face. Not your glider or anything else. Max 10MB Size: not less than 400*400 px</p>
<input type="file" class="form-control-file" id="pago" >
</div>
and this is my php
<?php
//Checking For reCAPTCHA
$captcha;
if (isset($_POST['g-recaptcha-response'])) {
$captcha = $_POST['g-recaptcha-response'];
}
// Checking For correct reCAPTCHA
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LcPkZEUAAAAAL4DJYl-y3iOl1TdavmqhsghRXeU&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
if (!$captcha || $response.success == false) {
header('location: ' . $_SERVER['HTTP_REFERER']);
} else {
/* Check all form inputs using check_input function */
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if( isset($_POST) ){
//user sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date_sent = date('d/m/Y');
$time = date('H:i:s');
//form data
$email = check_input($_POST['email']);
//photo
$file_name = $_FILES['my_file']['photo'];
$participant_number = check_input($_POST['participant_number']);
$firstName = check_input($_POST['firstName']);
$lastName = check_input($_POST['lastName']);
$country = check_input($_POST['country']);
$sexo = check_input($_POST['sexo']);
$date = check_input($_POST['date']);
$direccion = check_input($_POST['direccion']);
$tel = check_input($_POST['tel']);
$equipo = check_input($_POST['equipo']);
$homologacion = check_input($_POST['homologacion']);
$sponsor = check_input($_POST['sponsor']);
$licenciaFAI = check_input($_POST['licenciaFAI']);
$civilId = check_input($_POST['civilId']);
$team = check_input($_POST['team']);
$camisa = check_input($_POST['camisa']);
$seguro = check_input($_POST['seguro']);
$seguronumber = check_input($_POST['seguronumber']);
$emergencyContact = check_input($_POST['emergencyContact']);
$emergencyNumber = check_input($_POST['emergencyNumber']);
$sangre = check_input($_POST['sangre']);
//pago foto
$file_pago = $_FILES['file_pago']['pago'];
//send email if all is ok
$headers = "From: opeturmo#opeturmo.com" . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$emailbody = "<h2>Booking - Reservas</h2>
<p><strong>Email: </strong> {$email} </p>
<p><strong>Nuero participante: </strong> {$participant_number} </p>
<p><strong>Nombre: </strong> {$firstName} </p>
<p><strong>Apellido: </strong> {$lastName} </p>
<p><strong>Pais: </strong> {$country} </p>
<p><strong>Sexo: </strong> {$sexo} </p>
<p><strong>Fecha: </strong> {$date} </p>
<p><strong>Direccion: </strong> {$direccion} </p>
<p><strong>Telefono: </strong> {$tel} </p>
<p><strong>Marca equipo parapente: </strong> {$equipo} </p>
<p><strong>Sponsor: </strong> {$sponsor} </p>
<p><strong>Licencia Fai: </strong> {$licenciaFAI} </p>
<p><strong>Civil Id: </strong> {$civilId} </p>
<p><strong>Equipo con quien compute: </strong> {$team} </p>
<p><strong>Talla camisa: </strong> {$camisa} </p>
<p><strong>Compania Seguros: </strong> {$seguro} </p>
<p><strong>Poliza numero : </strong> {$seguronumber} </p>
<p><strong>Contacto de Emergencia : </strong> {$emergencyContact} </p>
<p><strong>Numero de Contacto de Emergencia : </strong> {$emergencyNumber} </p>
<p><strong>Tipo de Sangre : </strong> {$sangre} </p>
<p>This message was sent from the IP Address: {$ipaddress} on {$date_sent} at {$time}</p>";
$body = "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
mail ("opeturmo#gmail.com","Formulario Competencia",$emailbody,$body, $headers);
//redirect back to form
header('location: ' . $_SERVER['HTTP_REFERER']);
}
}
?>
I haven't done much with php so I am lost with this.

Cannot POST php mail (Vue)

I am building a really small webapp in Vue with a simple contact form as component. I would like to use a really basic php script for sending the mail. I am not an expert on PHP but for some reason I can't figure out this bug I get when I try to submit the contact form. It says
Cannot POST /mail_contact.php
HTML/Vue component
<form class="contact" action="mail_contact.php" method="post">
<div class="container">
<input type="text" class="st-input" placeholder="Vul hier jouw naam in" name="name">
<input type="text" class="st-input" placeholder="Vul hier kort en bondig jouw probleem in" name="problem">
<input type="text" class="st-input" name="email" placeholder="Vul hier jouw e-mail adres in">
<input type="text" class="st-input" placeholder="Vul hier jouw telefoonnummer in" name="tel">
<select name="telOrEmail" required><option disabled>Maak een keuze</option><option value="Tel">Telefoon</option><option value="Email">Email</option></select>
</div>
<button class="send-button" type="submit">Verzenden!</button>
</form>
PHP Script
<?php
if(isset($_POST['submit'])) {
//getters from form
$name = $_POST['name'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$problem = $_POST['problem'];
$telOrEmail = $_POST['telOrEmail'];
//mail content
$formcontent=" Van: $name \n Telefoonnummer: $tel \n Emailadres: $email \n Problem: $problem \n Terugbellen of Emailen: $telOrEmail";
//ontvanger van mail
$recipient = "g.gijsberts#sqits.nl";
//Onderwerp
$subject = "Contact formulier Sqits-it";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Bedankt voor je bericht! Wij hopen zo snel mogelijk te reageren.";
}
?>
Is there something I just don't see?
Edit
Project structure
index.html (File)
src (Directory) -> App.vue, main.js, router.js, components (directory) -> Contact.vue
dist (Directory) -> build.js, build.js.map, index.html, mail_contact.php
mail_contact.php (File)
So as you can see I've place my php script in the same directory as the main file (Index.html), but also in the directory where the build file is placed. Good point to say is this is a Vue project.
For a weird reason I get a 404 error (on localhost:8080/mail_contact.php) when submitting the form before I am redirected to the blank page with the Cannot POST /mail_contact.php. How is this possible if the file is actually there in my directory?

PHP Mail Function 404 Error [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
HTML Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>BuddyTeam | Website</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="page-wrap">
<img src="images/title.png" alt="BuddyTeam | Website" /><br /><br />
<div id="contact-area">
<form method="post" action="contactengine.php">
<p style="text-align:right" onclick="alert('Só podes registar uma conta secundária se tiveres uma principal! Neste espaço escreve o Nickname da tua conta registada no Servidor.')"><font color="blue">Precisas de ajuda? Clica Aqui!</font></p>
<label for="Nickname"></label>
<p><font face="serif"></font></p><input type="text" name="Nickname" id="Nickname" placeholder="Informa o Nickname da conta principal registada no Servidor." required />
<p style="text-align:right" onclick="alert('Para não haver fraudes precisamos de saber que és mesmo tu! Vai ao Servidor com a tua conta registada, usa o comando ( /meuid ) e escreve os números neste espaço.')"><font color="blue">Precisas de ajuda? Clica Aqui!</font></p>
<label for="ID"></label>
<p><font face="serif"></font></p><input type="text" name="ID" id="ID" placeholder="Informa o ID da conta principal registada no Servidor." required />
<p style="text-align:right" onclick="alert('Contas nunca são demais! Neste espaço escreve o Nickname da nova conta que queres registar.')"><font color="blue">Precisas de ajuda? Clica Aqui!</font></p>
<label for="Novo"></label>
<input type="text" name="Novo" id="Novo" placeholder="Informa o Nickname da conta que pretendes registar." required />
<p style="text-align:right" onclick="alert('Segurança em primeiro lugar! Neste espaço escreve uma senha para a nova conta que queres registar.')"><font color="blue">Precisas de ajuda? Clica Aqui!</font></p>
<label for="Senha"></label>
<input type="text" name="Senha" id="Senha" placeholder="Informa uma senha para a tua nova conta." required />
<p style="text-align:right" onclick="alert('Depois entra em contacto! Coloca aqui o teu Email para seres contacto quando a conta for registada.')"><font color="blue">Precisas de ajuda? Clica Aqui!</font></p>
<label for="Email"></label>
<input type="text" name="Email" id="Email" placeholder="Informa um Email para contacto.">
<input type="submit" name="submit" value="Enviar" class="submit-button" />
</form>
<div style="clear: both;"></div>
</div>
</div>
</body>
</html>
PHP Code:
<?php
$EmailFrom = "geral#buddyplays.net";
$EmailTo = "geral#buddyplays.net";
$Subject = "Novo Pedido de Registo de Conta";
$Nickname = Trim(stripslashes($_POST['Nickname']));
$ID = Trim(stripslashes($_POST['ID']));
$Novo = Trim(stripslashes($_POST['Novo']));
$Senha = Trim(stripslashes($_POST['Senha']));
$Email = Trim(stripslashes($_POST['Email']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=erro.html\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Conta Registada: ";
$Body .= $Nickname;
$Body .= "\n";
$Body .= "ID da Conta Registada: ";
$Body .= $ID;
$Body .= "\n";
$Body .= "Nova Conta: ";
$Body .= $Novo;
$Body .= "\n";
$Body .= "Senha: ";
$Body .= $Senha;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=concluido.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=erro.html\">";
}
?>
When I fill out the form and submit, I'm redirected to "erro.html" and the email is not sent. What is wrong?
The system stops working from one moment to another without me doing any kind of editing.
What do I need to change for the system to work again?
The problem is that your mail() function isn't working because you have supplied an invalid fourth parameter (as your From address). "From: <$EmailFrom>" shouldn't have the brackets around the email address. If you do make use of the brackets, you need to supply a name in front of them.
Instead of $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
What you're looking to do is add it as a header:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: ' . $Nickname. ' <' . $EmailFrom . '>';
$success = mail($EmailTo, $Subject, $Body, $headers);`
This will evaluate to: From: $Nickname <$EmailFrom>.
Also note that some mail servers prevent the From address from being manipulated in order to prevent phishing.
You're getting a 404 simply because erro.html doesn't exist on your server. Creating the relevant file will resolve the error and redirect you there automatically when there's an error sending of the email.
Hope this helps! :)

Weird WAMP localhost error [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I'm trying to make a form that mails the information to me. Here's my code.
PHP:
$to = "mosescu_b#yahoo.com";
$subject = "Comanda de Magneti";
$name = $_POST["nume"] . $_POST["prenume"];
$email = $_POST["email"];
$phone = $_POST["telefon"];
$adress = $_POST["adresa"];
$message = <<<EMAIL
Comanda de Magneti de la $name
Adresa de E-mail este $email
Numarul de telefon este $phone
Adresa este $adress
EMAIL;
$header ="$email";
if($_POST){
mail($to, $subject, $message, $header);
$feedback = "Multumesc pentru comanda!"
}
HTML
<div class="form-group">
<form style="font-size: 1.25em; margin-left: 20px;" method="post" action="#">
<label for="nume">Nume<span style="color: red;">*</span></label> <input name="nume" id="nume" class="form-control" type="text" placeholder="Nume" title="nume">
<label for="prenume">Prenume<span style="color: red;">*</span></label> <input name="prenume" id="prenume" class="form-control" type="text" placeholder="Prenume" title="prenume">
<label for="email">E-Mail</label><span style="color: red;">*</span><input name="email" id="email" class="form-control" type="email" placeholder="E-Mail" title="email">
<label for="telefon">Numar Telefon</label><input name="telefon" id="telefon" class="form-control" type="number" placeholder="Numar Telefon" title="telefon">
<label for="adresa">Adresa</label><span style="color: red;">*</span><input name="adresa" id="adresa" class="form-control" type="text" placeholder="Strada/Numar/Localitate/Judet" title="adresa"> <br />
<input name="submit" class="btn-default" type="submit" value="Comanda!"></input>
</form>
<p id="feedback"><?php echo $feedback ?> </p>
</div>
The error I'm getting is :"Parse error: syntax error, unexpected '}' in C:\wamp64\www\Test\comanda-magneti.php on line 30"
Which is weird, since I'm very sure that the code wouldn't run properly without the curly bracket.
Change this:
$feedback = "Multumesc pentru comanda!"
to
$feedback = "Multumesc pentru comanda!";
You forgot to add semi colon. Semicolon is act is line terminator in PHP and of course you can skip it for last line. But in this case you have closing curly braces after that.
Also please move if($_POST) to top as you are checking for it below and accessing POST properties above.
So something like:
if(isset($_POST)){
$to = "mosescu_b#yahoo.com";
$subject = "Comanda de Magneti";
$name = (isset($_POST["nume"]) ? $_POST["nume"] : '') . isset($_POST["prenume"]) ? $_POST["prenume"] : '' ;
$email = isset($_POST["email"]) ? $_POST["email"];
$phone = isset($_POST["telefon"]) ? $_POST["telefon"] : '';
$adress = isset($_POST["adresa"]) ? $_POST["adresa"] : '';
$message = <<<EMAIL
Comanda de Magneti de la $name
Adresa de E-mail este $email
Numarul de telefon este $phone
Adresa este $adress
EMAIL;
$header ="$email";
mail($to, $subject, $message, $header);
$feedback = "Multumesc pentru comanda!"
}
Also if possible do necessary check for POST variable for required fields.

PHP Notice: Undefined index - Contact Form not working [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
so this is a Contact Form php script with basic validations. This script was running absolutely fine without any issues or errors. Until recently I transferred the file to another web hosting.
The previous web host had PHP version 5.4.35
While the new web host has PHP version 5.4.45
I don't know much about PHP so I don't know what's going on. Here's what the error_log had logged everytime someone submitted the contact form.
[17-Jun-2016 17:05:20 Etc/GMT] PHP Notice: Undefined index: name in /home/domain/public_html/contact.php on line 70
[17-Jun-2016 17:05:20 Etc/GMT] PHP Notice: Undefined index: email in /home/domain/public_html/contact.php on line 76
[17-Jun-2016 17:05:20 Etc/GMT] PHP Notice: Undefined index: message in /home/domain/public_html/contact.php on line 82
In order to solve this I initialized the error variables ($name,$email,$message) as null in that case there was no more errors but the contact form failed to work.
Please help me! I don't know why this problem is occuring.
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$to = 'example#domain.com';
$subject = 'Contact Form';
$header = "From:contact#domain.com \r\n";
$header = "Cc:contact2#domain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
if ($human !== 2) {
$errHuman = 'Your anti-spam is incorrect';
}
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail($to, $subject, $body, $header)) {
$result='Thank You! Your message will be replied soon!';
} else {
$result='Sorry there was an error sending your message.';
}
}
}
?>
<form class="col l12" method="post" action="contact.php">
<input id="name" name="name" type="text" class="validate" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='red-text'>$errName</p>";?>
<label for="name">Name</label>
<input id="email" name="email" type="email" class="validate" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='red-text'>$errEmail</p>";?>
<label for="email">Email</label>
<textarea name="message" class="materialize-textarea"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='red-text'>$errMessage</p>";?>
<label for="message">Message</label>
<label for="human"><strong>AntiSPAM Check:</strong> 5 - 3 = ?</label>
<input id="human" name="human" type="text" class="validate">
<?php echo "<p class='red-text'>$errHuman</p>";?>
<p class="left-align"><button class="blue darken-1 btn-large waves-effect waves-light" id="submit" type="submit" style="font-weight:500;" name="submit">Send</button>
<?php echo $result; ?>
</form>
Those errors are telling you that your $_POST array doesn't have name or email or message. Someone submitted an empty form.
Now, you check for missing values later on in your script, but not until after you've already tried to access these missing array values.
Best thing would be to move your validation code...
if (!$_POST['name']) {
...
...to the top so it runs first. Then, only do $name = $_POST['name']; once you're sure that it exists.

Categories