PHP Mail how to show success box on page - php

I am new to PHP, and I made this PHP Mail, for a regular contact form... When the form is completed, the page is redirected and displays "The message was sent succesfully". The problem is, i don't want the page to redirect.. I want to show a feedback on the index, something like a success modal box or a simple text underneath the form "your message was sent"...
Any ideas on how to do this?
<?php
$nome = $_POST['nome'];
$email = $_POST['email'];
$emaildestino = 'contato#zerodesign.com.br';
$email_from='contato#zerodesign.com.br';
$mensagem = $_POST['mensagem'];
$assunto = $_POST['assunto'];
$telefone = $_POST['telefone'];
$titulo = 'Mensagem - TESTE';
<?php
$nome = $_POST['nome'];
$email = $_POST['email'];
$emaildestino = 'contato#zerodesign.com.br';
$email_from='contato#zerodesign.com.br';
$mensagem = $_POST['mensagem'];
$assunto = $_POST['assunto'];
$telefone = $_POST['telefone'];
$titulo = 'Mensagem - TESTE';
$juntando = '<p>Esta mensagem foi enviada pelo site</p><br/>
<p><b>Nome:</b> '.$nome.'</b></p><br>
<p><b>Email:</b> '.$email.' </b></p><br>
<p><b>Telefone:</b> '.$telefone.'</p> </br>
<p><b>Mensagem:</b></p>
<p><i>'.$mensagem.'</i></p>
<hr>';
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= "From: $email_from " . "\n";
$envio = mail($emaildestino, $titulo, $juntando, $headers, "-r".$email_from);
if($envio)
echo "Mensagem enviada com sucesso";
else
echo "A mensagem nãpode ser enviada";
?>
Here is my form:
<form method="POST" name="contactform" action="envia.php">
<p><input id="inputt" type="text" name="nome" placeholder="Nome" id="nome" ></p>
<p><input id="inputt" type="text" name="email" placeholder="Email" id="email"></p>
<p><input class="inputt" type="text" name="telefone" placeholder="Telefone" id="telefone"> </p>
<p><textarea id="mensagem" name="mensagem" rows="5" cols="40" placeholder="Mensagem" id="mensagem"></textarea></p>
<input id="button" type="submit" value="Enviar"><br>
</form>

If i understand correctly, what you want to do is not visit the envia.php page, you'll need to send an AJAX call there instead. When the ajax call finishes, you can use javascript to update your index page with the success message.

I believe you are looking for something like:
PHP script added to the top of the form page redirecting to the same page
if(isset($_POST['nome'], $_POST['email'], $_POST['mensagem'], $_POST['assunto'], $_POST['telefone']){
...
$envio = mail($emaildestino, $titulo, $juntando, $headers, "-r".$email_from);
if($envio){
$msg = "Mensagem enviada com sucesso";
}else{
$msg = "A mensagem nãpode ser enviada";
}
}
HTML:
<form method="POST" name="contactform" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
<p><input id="inputt" type="text" name="nome" placeholder="Nome" id="nome" ></p>
<p><input id="inputt" type="text" name="email" placeholder="Email" id="email"></p>
<p><input class="inputt" type="text" name="telefone" placeholder="Telefone" id="telefone"> </p>
<p><textarea id="mensagem" name="mensagem" rows="5" cols="40" placeholder="Mensagem" id="mensagem"></textarea></p>
<input id="button" type="submit" value="Enviar"><br>
<span id="msg"><?php if(isset($msg)){echo $msg;}?></span>
</form>
looking at your site you might want to add the <?php if(isset($msg)){echo $msg;}?> to the top of the page or add a js script that scrolls to the bottom of the page

Just make the action attribute empty like...
<form method="POST" name="contactform" action="">
So, the page will submit to itself.

Related

PHP contact form is not sending email. [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I honestly can't find out what is wrong with the code. Could someone please help? I just want it to send the input to my email. And yes, it an email associated with my host.
The PHP:
<?php
if (isset($_POST['submit'])) { .
$name = $_POST['name'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "my#email.com";
$headers = "De: " .$mailfrom;
$txt = "Você recebeu um email de ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.php?mailsend");
}
The HTML:
<form id="contact" action="contactform.php" method="post">
<h3>Deixe a sua mensagem.</h3>
<input placeholder="Seu Nome" name="name" type="text" tabindex="1" required autofocus oninvalid="this.setCustomValidity('Favor digitar nome válido.')" oninput="setCustomValidity('')">
<input placeholder="Seu E-Mail" name="mail" type="email" tabindex="2" required oninvalid="this.setCustomValidity('Favor digitar Email válido.')" oninput="setCustomValidity('')">
<textarea placeholder="Digite sua mensagem aqui..." tabindex="5" name="message" required oninvalid="this.setCustomValidity('Favor digitar mensagem válida.')" oninput="setCustomValidity('')"></textarea>
<button name="submit" type="submit" id="contact-submit" data-submit="...Sending">ENVIAR</button>
</form>
Yeah it's a mess, sorry about that.
what is that .?
if (isset($_POST['submit'])) { . <--------
I would try removing that period. Unless it's a speck on my monitor. haha!
If that isn't the problem, then it could be your smtp settings aren't set properly

PHP Validation to require One of Two Form Fields filled in

I am developing a site that uses javascript to handle many functions and PHP based Captcha code to validate the form field.
It works great, but the form will submit if none of the fields are filled in.
I need one of two form fields ['email' or 'phone'] filled in, but neither can be left blank.
The error message can be the same error message thrown up when the captcha field is left blank or filled in incorrectly.
I am new to PHP code and cannot figure out for the life of me how to call the function.
The function code is:
<?php
if(isset($_POST['send'])){
$emailFrom = "clientemail.com";
$emailTo = "clientemail.com";
$subject = "Contact Form Submission";
$first_name = strip_tags($_POST['first_name']);
$last_name = strip_tags($_POST['last_name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$message = strip_tags(stripslashes($_POST['message']));
$body .= "First Name: ".$first_name."\n";
$body .= "Last Name: ".$last_name."\n";
$body .= "Email: ".$email."\n";
$body .= "Phone: ".$phone."\n";
$body .= "Comments: ".$message."\n";
$headers = "From: ".$emailFrom."\n";
$headers .= "Reply-To:".$email."\n";
if($_SESSION['security_code'] == $_POST['security_code']){
$success = mail($emailTo, $subject, $body, $headers);
if ($success){
echo '<p class="yay">Your e–mail has been sent.</p>';
}
} else {
echo '<p class="oops">Something went wrong. Hit the back button and try again.</p>';
}
} else {
?>
The form field:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" id="contact" name="contact">
<fieldset>
<label for="name"><span style="color:#bf252b;">*</span>First Name</label>
<input type="text" id="first_name" name="first_name" minlength="2"/>
<label for="name">Last Name</label>
<input type="text" id="last_name" name="last_name" minlength="2"/>
<label for="email"><span style="color:#bf252b;">*</span> Email</label>
<input type="text" id="email" name="email" />
<label for="phone"><span style="color:#bf252b;">*</span> Phone</label>
<input type="text" id="phone" name="phone" />
<label for="message">Message</label><div style="clear:both;"></div>
<textarea id="message" name="message" cols="40" rows="10" ></textarea>
<img src="../captcha.php" id="captcha" alt="captcha" style="padding:25px 0px 20px 0px;" />
<label for="security_code">Enter captcha</label>
<input type="text" id="security_code" name="security_code" autocomplete="off" class="required"/>
<button type="submit" id="send" name="send" style="margin:0px 0px 10px 12px;">Send!</button>
</fieldset>
</form>
<?php } ?>
There is a .php document for running captcha, but am I right in thinking there is a simple solution for this; some extra code in the existing code that will fix my issue? I really want to avoid javascript and plugins if I can help it.
Thanks in advance!!
Try this,
if($_SESSION['security_code'] == $_POST['security_code'] && (!empty($email) || !empty($phone))) {
instead of
if ($_SESSION['security_code'] == $_POST['security_code']) {
This is not convenient way to validate form but I hope this will help.
Add the required attribute to the fields you need required.
Fix your code!
Notice: Undefined variable: body in /../sendform.php on line 14
Do that:
$body = "First Name: ".$first_name."\n";
$body .= "Last Name: ".$last_name."\n";
$body .= "Email: ".$email."\n";
$body .= "Phone: ".$phone."\n";
$body .= "Comments: ".$message."\n";
Solution
if(($_SESSION['security_code'] == $_POST['security_code']) AND
(!empty($email) OR !empty($phone)) ) {
In other words:
Security code must be match
Email or Phone can not be empty

How to run php script while adding text to same html page?

So basically, I want to have someone complete a form, run a php script, and instead of pulling up the php file's web page, I want to append text at the end of the form.
Currently, I have a form:
<form action="scripts/email.php" method="POST">
<p>Name: <input type="text" name="name" placeholder="Name"></p>
<p>Email: <input type="text" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea name="message"></textarea></p>
which looks like this:
When 'Submit' is clicked, it leads to my PHP script:
<?php
$from = $_POST['email'];
$to = "comp#c0mp.org";
$subject = $_POST['subject'];
$name = $_POST['name'];
$message = $_POST['message'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
print "Your message has been sent!";
?>
which leads to a blank web page that just says this in plain text:
"Your message has been sent!"
Basically, what I want is text added onto the submit form after the script is ran, such as:
http://i.stack.imgur.com/zha4d.png
How can I achieve running a PHP script on a web page, and instead of loading a new web page of the PHP script's path, but append text on the current HTML web page?
TRY THIS : WRITE THIS CODE INTO A SINGLE PAGE.
<form action="<?php $PHP_SELF ;?>" method="POST">
<p>Name: <input type="text" name="name" placeholder="Name"></p>
<p>Email: <input type="text" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea name="message"></textarea></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'] ))
{
$from = $_POST['email'];
$to = "comp#c0mp.org";
$subject = $_POST['subject'];
$name = $_POST['name'];
$message = $_POST['message'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
echo "Your message has been sent";
}
?>
you can use ajax for acheive more secure and fast execution of script.
HTML code:
<form method="POST">
<p>Name: <input type="text" id="name" name="name" placeholder="Name"></p>
<p>Email: <input type="text" id="email" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" id="subject" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea id="area" name="message"></textarea></p>
<input type="submit" name="submit" value="Submit" onclick="mail_js();">
</form>
<div id="success_msg" style="display:none;"></div>
Just give proper style to this div.
JS code:
function mail_js()
{
var input_val=$('#name').val();
var email_val=$('#email').val();
var subject_val=$('#subject').val();
var message_val=$('#area').val();
jQuery.ajax({
type: 'post',
data:{
'input_val':input_val,
'email_val':email_val,
'subject_val':subject_val,
'message_val':message_val
},
url: 'email.php',
success:function(response){
$("#success_msg").append(response).show();
},
error: function(errorThrown){
alert('error');
console.log(errorThrown);
}
});
}
PHP script:
$from = $_POST['email_val'];
$to = "comp#c0mp.org";
$subject = $_POST['subject_val'];
$name = $_POST['input_val'];
$message = $_POST['message_val'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
echo "Your message has been sent";

jQuery.post not POSTing

I am trying to use jQuery to post data to an emailus.php script.
Here is the script part:
<script>
jQuery(document).ready(function(){
jQuery('#submitt').click(function(){
jQuery.post("/emailus.php", jQuery("#mycontactform").serialize(), function(response) {
jQuery('#success').html(response);
});
return false;
});
});
</script>
and here is the HTML used:
<form action="" method="get" id="mycontactform" >
<label for="name">Your Name:</label><br />
<input type="text" name="name" class="cleann" /><br />
<label for="email">Your Email:</label><br />
<input type="text" name="email" class="cleann" /><br />
<label for="message">Your Message:</label><br />
<textarea name="message" class="cleann" rows="7"></textarea><br />
<input type="button" value="send" id="submitt" class="cleannsubmit" /><div id="success" style="color:green;"></div>
</form>
and here is the php script:
<?php
// Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'nohanada#gmail.com';
$subject = 'Fortrove Contact';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message.'\n\nItem:'.$itemname;
print_r($_POST);
if($name && $email && $message){
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
mail($to, $subject, $message);
echo "Your email was sent!";
}
else echo "<span style='color:red'>Invalid email format.</span>";
}
else echo "<span style='color:red'>Please fill all fields</span>";
?>
Problem is that it does not POST the actual fields to the php script. What am i doing wrong?
you have placed the mycontactform inside product_addtocart_form that is the reason which is not allowed so the browser seems to be remocing the mycontactform

Sending email with data from form

I have problem with simple contact box. I want to send informations from form to my email.
Already tried with register globals on and off (.httacess), tried with simple $formname and $_POST['formname'] but still getting email without form info.
<?php
if ($action == "send"){//isset wyslij
//if (!$name || !$email || !$phone || !$enquiry) {
//$problem = TRUE;
//echo("<p>You have to fill all form.</p>");
//}
if (!$problem){
$name = $_POST['name'];
$data = date("d.m.y");
$message = "
<p>Name: $name</p>
<p>Phone: $phone</p>
<p>Email: $email</p>
<br>
<p>Enquiry: $enquiry</p>";
$od = "blabla#email.com";
$content = $message;
$header = "From: $od \r\n";
$header .= 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
(mail('myemail#gmail.com', 'New message from website Angelzena', $content, $header));
echo("<br><p>Message has been sent.</p>");
}else{
echo("<p>Try <a href=contact.php>again</a></p>");
}
}
?>
<form action="contact.php?action=send" method="post" enctype="text/plain">
<label for="name">Name</label><input type="text" name="name" /></br></br>
<label for="email">Email</label><input type="text" name="email" /></br></br>
<label for="phone">Phone</label><input type="text" name="phone" /></br></br>
<label for="enquiry">Enquiry</label><textarea name="enquiry" cols="20" rows="10"></textarea></br></br>
<input type="submit" id="contact_button" value="Send" />
</form>
<form action="contact.php?action=send" method="post" enctype="text/plain">
When POSTing forms the enctype attribute should be application/x-www-form-urlencoded or multipart/form-data.
PHP does not handle forms POSTed as text/plain.

Categories