I am using php mail form on my page. It work good but when email is sent the message about successful post is opened in new blank page. What I want to do is show message directly under send button with some formating. My code looks like this.
<form action="contact.php" method="POST" id="contactform_main2" style="font-size:14px">
<ol>
<li>
<label for="email">Email</label>
<input type="email" id="email" name="email" class="text" required />
</li>
....etc
<li class="buttons">
<input type="submit" name="imageField" value="Odoslať" class="send" style="cursor:pointer" />
</li>
</ol>
</form>
and this is the php
<?php
...geting data
if(
mail("dominik0109#gmail.com", $subject,
$sprava, "From:" . $email)){
echo "<strong>Sucessfully sent</strong>";}
else {
echo "Error";}
?>
You can see it live on my page, fill any content to post form.
How to fix this please?
Thnaks
You just need to send the form to the same page and check if POST variables are passed.
mypage.php
<form action="#message" method="POST" id="contactform_main2" style="font-size:14px">
<ol>
<li>
<label for="email">Email</label>
<input type="email" id="email" name="email" class="text" required />
</li>
<!-- ....etc -->
<li class="buttons">
<input type="submit" name="imageField" value="Odoslať" class="send" style="cursor:pointer" />
</li>
</ol>
</form>
<?php
if(isset($_POST['email'])) {
// ...geting data
echo "<a name='message'></a>";
if(mail("dominik0109#gmail.com", $subject, $sprava, "From:" . $email)){
echo "<strong>Sucessfully sent</strong>";
}
else {
echo "Error";
}
}
?>
If you are looking not to refresh the page at all you'll need javascript and ajax.
Related
I am working on a simple contact form.
It uses PHP code to show a "Thank you" message after the submit.
The problem there is about it scrolls up as soon as you submit the form.
PHP CODE:
<?php
session_name("fancyform");
session_start();
$_SESSION['n1'] = rand(1,20);
$_SESSION['n2'] = rand(1,20);
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];
if(isset($_SESSION['sent']))
{
$success='<h1>Thank you!</h1>';
unset($_SESSION['sent']);
}
?>
FORM CODE (SECTION):
<section id="revealAnim" class="page5">
<div class="page_container">
<div class="icono red"></div>
<p class="titulo_cabecera">Necesito soluciones!</p>
<p class="subtitulo_cabecera">Rellena el formulario y estaremos en contacto en menos de 24h</p>
<form class="demo-form" name ="demo-form" data-parsley-validate method="post" action="submit.php" >
<ul>
<li class="js-hide-label">
<label for="name">Nombre:</label>
<input type="text" placeholder="Nombre" id="name" name="name" data-parsley-trigger="change" required tabindex="1" autocomplete="off" value="<?php echo (isset($_SESSION['post']['name']) ? $_SESSION['post']['name'] : ''); ?>" >
</li>
<li class="js-hide-label">
<label for="email">Your Email:</label>
<input type="email" placeholder="Your Email" id="email" data-parsley-trigger="change" name="email" autocomplete="off" required tabindex="2" value="<?php echo (isset($_SESSION['post']['email']) ? $_SESSION['post']['email'] : ''); ?>">
</li>
<li class="js-hide-label">
<label for="message">Message:</label>
<textarea placeholder="Message…" id="message" name="message" tabindex="3" required data-parsley-trigger="keyup" textarea id="message" data-parsley-minlength="20" data-parsley-maxlength="100" data-parsley-minlength-message = "Come on! You need to enter at least a 20 caracters long comment.." data-parsley-validation-threshold="10"><?php echo (isset($_SESSION['post']['message']) ? $_SESSION['post']['message'] : ''); ?></textarea>
</li>
<input class="btn btn-default"type="submit" name="button" id="button" value="Submit" />
</ul>
<?php echo isset($success)?$success:''; ?>
</form>
</section>
Should I work on a Javascript form to avoid the problem? Or there is any solution working on the PHP code?.
Otherwise I am working with ONE PAGE SCROLL so the scrolls breaks my usability on the 90% of users, since they have to scroll back to the bottom page (they are 5) to see if the form has been submited.
Thank you
You could either use AJAX/Javascript like you have suggested, or you could redirect the visitor to an id tag.
http://example.com/index.php#contact
So the body will automatically be scrolled to the contact form after submit.
By default a web page reloads when a form is submitted. This is how PHP works.
When a user clicks on the submit button an http request is sent to the server and thus requesting new data, so the page must be reloaded to preview response data.
The way to avoid this is by using AJAX
there is a good example found on this link: Submit A Form Without Page Refresh using jQuery
Script1 is based on CSS and html design(with some texture effect) I tried to setup php form action script to a html/css form however it keeps refreshing to the home page.. according to script if successful then it will refresh to thankyou.php, but it is not success.
However script2, I don't use any css, it is very basic html but it is working fine! but since my whole website design have css effect, I want the form also needs to have some css work.
Could someone please help me with script1? if it not possible then could you please suggest me an action script for below form, currently I don't have any action.php script, I tried few in online, unfortunately without success.
CSS form script:
<div class="row add-bottom-main">
<form name="myform" id="contactForm" action="" method="post">
<article class="span6">
<textarea id="msg" rows="3" cols="40" name="message" placeholder="Message">Message</textarea>
</article>
<article class="span6">
<input size="100" type="text" name="name" id="name" placeholder="Name">
<input type="text" size="30" id="email" name="email" placeholder="email">
<button type="submit" name="submit" id="submit" class="btn btn-renova-alt add-top-half">Send Message</button>
</article>
</form>
</div>
Script 1
<div class="row add-bottom-main">
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form name="myform" id="contactForm" action="" enctype="multipart/form-data" method="post">
<article class="span6">
<textarea id="msg" rows="3" cols="40" name="message" placeholder="Message">Message</textarea>
</article>
<article class="span6">
<input size="100" type="text" name="name" id="name" placeholder="Name">
<input type="text" size="30" id="email" name="email" placeholder="email">
<button type="submit" name="submit" id="submit" class="btn btn-renova-alt add-top-half">Send Message</button>
</article>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill again.";
}
else
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("niranjan.thampu#gmail.com", $subject, $message, $from);
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=thankyou.php">';
exit;
}
}
?>
</div>
SCRIPT 2
<div class="row add-bottom-main">
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form name="myform" id="contactForm" action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill again.";
}
else
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("niranjan.thampu#capital-placement.com", $subject, $message, $from);
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=thankyou.php">';
exit;
}
}
?>
</div>
Your "css form" as posted has no action set on the form and no PHP to process the page in the event that it's submitted. Your two other forms both have PHP that is testing to see if the form has been submitted and process it when it has. The PHP on those two pages is doing the work of processing the form. Without it, they would also not do anything when submit.
Good day/evening. I have this sort of form in HTML:
<form action="scripts/form-testimonial.php" class="form" method="post">
<ul>
<li><label for="name">Name</label> <input class="required" id="namef3" name="name" placeholder="Name" type="text"></li>
<li><label for="email">E-mail</label> <input class="required" id="emailf3" name="email" placeholder="E-mail" type="text"></li>
<li><label for="company">Company</label> <input id="companyf3" name="company" placeholder="Company (if applicable)" type="text"></li>
<li><label for="msg">Message</label> <textarea class="required" cols="43" id="msgf3" name="msg" placeholder="Your feedback" rows="8"></textarea></li>
</ul>
<button class="submit" type="submit">Send your feedback</button>
</form>
and this sort of php sript for this form as a separate file:
<?php
$receip = "office#avehire.co.uk";
$subj = "Testimonial";
$bodymsg = "Name: ".$_POST['name']."\n"
."Company: ".$_POST['company']."\n"
."Email: ".$_POST['email']."\n"
."Message: ".$_POST['msg']."\n";
if(!$_POST['msg']){
header("Location: ../error.html");
exit;
}
$email = $_POST['email'];
if(mail($receip, $subj, $bodymsg, 'From: Contact <'.$email.'>')){
header("Location: ../msg_sent_feedback.html");
}
?>
I also have a validator for this form, but this is not important. What is more important here is the way how this form works. Basically - when you click submit button - it goes to a separate php file, process the form and then - from what is already there - goes to msg_sent_feedback.html file. What I want to do is to NOT opens a separate file but just display a message in the same basic html file where the actual form exists. A message e.g. "Message sent".
Another thing - if I already have a validation script - do I need to have the first "if" in this form? or can I remove it totally? the one which redirects to error.html. Or can I remove these 4 lines? That line - I guess - is responsible for errors in the form, but as I mentioned, I have a validation script. I don't really want to change html. is it possible to modify the php script to work within the html document, as instructed above? thanks
Here is an Jquery ajax example that will help you.
<?php
$receip = "office#avehire.co.uk";
$subj = "Testimonial";
$bodymsg = "Name: ".$_POST['name']."\n"
."Company: ".$_POST['company']."\n"
."Email: ".$_POST['email']."\n"
."Message: ".$_POST['msg']."\n";
if(!$_POST['msg']){
echo "<h1>Error sending e-mail";
exit;
}
$email = $_POST['email'];
if(mail($receip, $subj, $bodymsg, 'From: Contact <'.$email.'>')){
echo "<h1>Email sent successfully</h1>";
}
?>
Html :
<!Doctype html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function submitmyform(){
data=$('#myform').serialize();
$.ajax({
url: "myphpfile.php",
type:'POST',
data:data,
async:false,
dataType:'html',
success: function(msg){
$('#response_goes_here').html(msg);
}
});
}
</script>
<form action="" id="myform">
<ul>
<li> <label for="name">Name</label> <input id="namef3" name="name" type="text" placeholder="Name" class="required"/>
</li>
<li> <label for="email">E-mail</label> <input id="emailf3" name="email" type="text" placeholder="E-mail" class="required"/>
</li>
<li> <label for="company">Company</label> <input id="companyf3" name="company" type="text" placeholder="Company (if applicable)" />
</li>
<li> <label for="msg">Message</label> <textarea id="msgf3" name="msg" cols="43" rows="8" placeholder="Your feedback" class="required"></textarea>
</li>
</ul>
<button type="button" class="button" onclick="submitmyform();">Send your feedback</button>
</form>
<div id="response_goes_here"></div>
I started working on a wordpress template and I am extremely stuck on this contact form, which I don't seem to find the problem. I must mention that I am somehow new to php, but I can understand pretty fast. This is what I am using for my contact page (there are also other elements but they do not interfere with the code I`m having trouble with):
<?php get_header();
$two2_option = two2_get_global_options();
global $post; setup_postdata($post); ?>
<?php if($two2_option['two2_contact_email']){ ?>
<form method="post" id="ajax_form" action="#">
<fieldset>
<ul class="form-item" id="field-name">
<li>
<label><?php _e("Name","jbirdie"); ?>:<span class="field-required" title="<?php _e("Required Field","jbirdie"); ?>">*</span></label>
<input type="text" maxlength="90" name="name" id="name" placeholder="<?php _e("name","jbirdie"); ?>" size="30" class="form-field required text_input clear" />
</li>
<li>
<label><?php _e("E-mail","jbirdie"); ?>:<span class="field-required" title="<?php _e("Required Field","jbirdie"); ?>">*</span></label>
<input type="email" maxlength="90" name="email" id="email" placeholder="<?php _e("e-mail","jbirdie"); ?>" size="30" class="form-field required email text_input clear" />
</li>
<li>
<label><?php _e("Phone","jbirdie"); ?></label>
<input type="number" maxlength="90" name="phone" id="phone" placeholder="<?php _e("phone number","jbirdie"); ?>" size="30" class="form-field required email text_input clear" />
</li>
<li class="message">
<label><?php _e("Comments","jbirdie"); ?> <span>Available for quotation</span></label>
<textarea cols="55" rows="" name="message" id="message" placeholder="<?php _e("Comments","jbirdie"); ?>" class="form-textarea text_area clear" ></textarea>
</li>
</ul>
<input type="submit" name="submit" id="submit" value="send" class="submit form-submit" />
<div id="result"></div>
</fieldset>
</form>
</div>
<?php } ?>
</div>
<?php } ?>
</div>
</div>
<?php get_footer(); ?>
and the following in functions.php :
add_action('wp_ajax_nopriv_two2_send_contact_form', 'two2_send_contact_form');
add_action('wp_ajax_two2_send_contact_form', 'two2_send_contact_form');
function two2_send_contact_form(){
$two2_option = two2_get_global_options();
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$subject = "Message from your portfolio";
$body = "From $name, \n\n$email, \n\n$phone, \n\n$message";
$to = $two2_option['two2_contact_email'];
$result = mail($to, $subject, $body);
if($result){
print "<h3>".__("Success","two2")."</h3><p>".__("Your email has been sent!","two2")."</p>";
} else {
print "<h3>".__("Error","two2")."</h3><p>".__("Try it again later, please.","two2")."</p>";
}
die();
}
...and it`s simply not working...can someone please give me a hint or sth to find the error?
Have you got a script to submit the form to the ajax handler?
e.g.
<script type="text/javascript" >
jQuery(document).ready(function($) {
$('#ajax_form').submit(function(evt){
var form_data = {
action: 'two2_send_contact_form',
name: jQuery('#name').val(),
email: jQuery('#email').val(),
phone: jQuery('#phone').val(),
message: jQuery('#message').val()
};
$.post('<?php echo admin_url('admin-ajax.php'); ?>', form_data, function(response) {
console.log(response);
});
return false;
});
});
</script>
http://codex.wordpress.org/AJAX_in_Plugins
Also there is an extra php closing brace <?php } ?> in your contact form.
Edit
Corrected jQuery selector & ajax url.
I have a document with 2 textboxes and 1 submit button. I want to send an email when I press the submit button and in it should be the value of the textboxes. My PHP knowledge is a bit dusty. I don't want to bother the user with the emailproces, so he can't see this and should just be redirected.
<html>
<body>
<div class="section_form" id="usernameSection">
<label for="username">Login:</label>
<input size="20" type="text" name="username" id="username" />
</div>
<div class="section_form" id="emailSection">
<label for="email">Email:</label>
<input size="20" type="text" id="email" name="email" maxlength="20"/>
</div>
<div id="submit_button">
<button type="submit" value="Submit" onmouseover="this.style.backgroundPosition='bottom';" onmouseout="this.style.backgroundPosition='top';" onclick="return SetFocus();">Submit</button>
</div>
</body>
</html>
Many thanks!
Use the PHP mail function - listed here.
That should be able to help.
Basically the core of what you're looking for is
<?php
// Check that all fields are present, construct the message body, etc
mail($to, $subject, $body);
header("Location: wherever.php");
exit();
?>
See the mail function in PHP documentation. (Also, thank you Alex for the exit() reminder.)
Use this code for your task.
<?php
if(isset($_POST['submit'])){
extract($_POST);
$message = "username : $username ; email : $email";
$to = "your#email.com";
$subject = "Email Subject";
mail($to, $subject, $message);
}
?>
<html>
<body>
<form method="POST" name="form1">
<div class="section_form" id="usernameSection">
<label for="username">Login:</label>
<input size="20" type="text" name="username" id="username" />
</div>
<div class="section_form" id="emailSection">
<label for="email">Email:</label>
<input size="20" type="text" id="email" name="email" maxlength="20"/>
</div>
<div id="submit_button">
<button type="submit" value="Submit" name="submit" onmouseover="this.style.backgroundPosition='bottom';" onmouseout="this.style.backgroundPosition='top';" onclick="return SetFocus();">Submit</button>
</div>
</form>
</body>
</html>
Start with:
- $_POST
- mail()
Also you need to check: Forms in HTML, you might want to use form to send the data of input fields to server.