Im creating page with parallax scrolling, contact box is in last section. If I use simple html + php contact box, page will refresh to first section, so user will have to scroll back to contact section to see result. To correct this I am trying to make script that will send email without reloading page. (by Jquery)
This is how it looks like:
HTML:
<form method="post" enctype="multipart/form-data" id="formularz">
<label for="name">Imię</label><input type="text" name="name" id="formularz_name"/></br></br>
<label for="email">Email</label><input type="text" name="email" id="formularz_email"/></br></br>
<label for="phone">Telefon</label><input type="text" name="phone" id="formularz_phone"/></br></br>
<label for="enquiry">Zapytanie</label><textarea name="enquiry" cols="20" rows="10" id="formularz_text"></textarea></br></br>
<input type="submit" id="contact_button" value="Wyślij" />
</form>
Jquery:
$('#contact_button').click(function () {
var name = $('#formularz_name').val();
var email = $('#formularz_email').val();
var phone = $('#formularz_phone').val();
var text = $('#formularz_text').val();
$.ajax({
url: 'inc/contact.php',
data: {'name': name, 'email': email, 'phone': phone, 'text': text},
type: 'POST',
success: function(odp){
alert(odp);
}
});
});
PHP:
<?php
if (!$name || !$email || !$phone || !$text) {
$problem = TRUE;
echo("<br><p>Musisz wypełnić wszystkie pola.</p>");
}
if (!$problem){
$data = date("d.m.y");
$message = "
<p>Name: $name</p>
<p>Phone: $phone</p>
<p>Email: $email</p>
<br>
<p>Enquiry: $text</p>";
$od = "$email";
$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('lethysek@gmail.com', 'New message from website Angelzena', $content, $header));
echo("<br><p>Wiadomość została wysłana.</p>");
}else{
echo("<p>Spróbuj ponownie</p>");
}
?>
Why it doesn't work? When i click submit, page is refreshed and nothing happens.
Using button
instead of submit
to prevent refreshing.
Change <input type="submit" id="contact_button" value="Wyślij" />
To <input type="button" id="contact_button" value="Wyślij" />
Try to serialize your form data to avoid unnecessary geting input values and prevent the default action of form (submit).
$("form").on("submit", function(event) {
event.preventDefault();
var formData = $(this).serialize();
$.ajax({
url: 'inc/contact.php',
data: formData,
type: 'POST',
success: function(odp){
alert(odp);
}
});
});
尝试删除标签周围的包装器(你已经有一个jquery ajax()调用POST)并添加$ _POST ['name']作为其他提到的实际初始化你的php变量。
You need to stop the form from submitting in your jquery. Try this:
$('#formularz').submit(function ( e ) {
var name = $('#formularz_name').val();
var email = $('#formularz_email').val();
var phone = $('#formularz_phone').val();
var text = $('#formularz_text').val();
$.ajax({
url: 'inc/contact.php',
data: {'name': name, 'email': email, 'phone': phone, 'text': text},
type: 'POST',
success: function(odp){
alert(odp);
}
});
e.preventDefault();
});
删除表单标签...它强制您的页面刷新。它将适合您。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.