Diskuze: PHP contact form
V předchozím kvízu, Online test znalostí PHP, jsme si ověřili nabyté zkušenosti z kurzu.


ConflictBoy:8.12.2017 13:05
A nemáš v tom kódu ještě náhodou někde vložený javascript?
Peter Sciranka:8.12.2017 13:08
Ahoj, prezrel som to len v rýchlosti a v poslednom kóde máš id="phone1",
skús dať len id= phone"
Peter Sciranka:8.12.2017 13:19
Zle som to napísal, tak ešte raz
Kód, ktorý pridávaš tak v tagu <input> máš id="phone1", skús to
zmeniť na id="phone".
Pretože tento id potom zisťuješ v kóde
empty($_POST['phone']);
$phone = $_POST['phone'];
Je nutné aby tieto položky boli rovnaké.
Ten phone1 byl pouze preklep tady na foru, ale diky .. Conflict boy mel pravdu je tam i
JS.
Ted jsem vsechny kody prepsal a stejne to nechodi. Vidite tam nekdo chybu?
HTML
<form name="sentMessage" id="contactForm" novalidate>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" id="name" class="form-control" placeholder="Name" required="required">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="tel" id="phone" class="form-control" placeholder="Phone" required="required">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="email" id="email" class="form-control" placeholder="Email" required="required">
<p class="help-block text-danger"></p>
</div>
</div>
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" placeholder="Message" required></textarea>
<p class="help-block text-danger"></p>
</div>
<div id="success"></div>
<button type="submit" class="btn btn-default">Poslat zprávu</button>
</form>
PHP
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['phone']) ||
empty($_POST['email']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email_address = $_POST['email'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'petr.vit@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\nPhone: $phone\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
JavaScript
$(function() {
$("input,textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var phone = $("input#phone").val();
var email = $("input#email").val();
var message = $("textarea#message").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "././mail/contact_me.php",
type: "POST",
data: {
name: name,
phone: phone,
email: email,
message: message
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
$('#success').html('');
});
Nefunguje znamena co? Nejaka php chyba nebo js chyba? Kiukam na to a nevidim chybu.
ConflictBoy:11.12.2017 12:59
Zkoušel jsem si tvůj kód a funguje I telefonní číslo došlo.
Nicméně pokud tím nefungováním myslíš to, že ti e-mail nedošel, možná
je to tím, že to posíláš na G-mail. Ono posílání e-mailů je trochu
věda, aby byl e-mail doručen, chce to mít ideálně DKIM, SPF a další
věci, které vyhodnocují důvěryhodnost e-mailu. Google na důvěryhodnosti
docela trvá, takže když odešleš několikrát prostý e-mail pomocí PHP
funkce mail(), zřejmě to časem vyhodnotí jako spam. Někdy ten e-mail
nedojde vůbec. Takže prvně se podívej do Spamu, jestli to neskončilo tam,
pokud ne, zkus změnit e-mail třeba na Seznam.cz.
Zobrazeno 7 zpráv z 7.