| Autor | Zpráva | ||
|---|---|---|---|
| baffik Profil * |
#1 · Zasláno: 25. 5. 2015, 09:37:52
Dobrý den,
koupil jsem si šablonu webu včetně kontaktního formuláře, avšak ten nefunguje na mém hostingu vedeného u gigaserver.cz a nevím v čem by mohl být problém? kód ze stránky: <div id="contact-form"> <form method="post" action="contact.php"> <div class="field"> <label>Jméno:</label> <input type="text" name="name" class="text" /> </div> <div class="field"> <label>E-mail: <span>*</span></label> <input type="text" name="email" class="text" /> </div> <div class="field"> <label>Zpráva: <span>*</span></label> <textarea name="message" class="text textarea" ></textarea> </div> <div class="field"> <input type="button" id="send" value="Odeslat zprávu"/> <div class="loading"></div> </div> </form> </div> php soubor: <?php
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$message = ($_GET['message']) ?$_GET['message'] : $_POST['message'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$message) $errors[count($errors)] = 'Please enter your message.';
//if the errors array is empty, send the mail
if (!$errors) {
// ====== Your mail here ====== //
$to = 'domena.cz <info@domena.cz>';
//sender
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Message from your website';
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr><td>Name:</td><td>' . $name . '</td></tr>
<tr><td>Email:</td><td>' . $email . '</td></tr>
<tr><td>Message:</td><td>' . nl2br($message) . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
//if the errors array has values
} else {}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
?> |
||
| smiesek Profil |
#2 · Zasláno: 25. 5. 2015, 10:34:57
baffik:
vhodné by bylo napsat, co nefunguje: a) nezobrazuje se formulář b) neodesílá zprávu c) něco jiného |
||
| baffik Profil * |
#3 · Zasláno: 25. 5. 2015, 12:45:29
add a) formulář se zobrazuje vč. kontroly polí a loading divu po kliknutí odeslat
add b) zprávu neodešle add c) nelepší je asi vidět v praxi: http://3dum.cz/kontakt/ Dle tvůrce půjde o nastavení serveru(hostingu) neboť další zákazníci víceméně problém nemají, ale neřeší problémy s formulářem neboť není php programátor a jedná se o převzatý script Díky |
||
| baffik Profil |
Zřejmě asi "někdo" něco pokoušel a ve 13:29 přišel mail:
Message from your website Name: name Email: email Message: message Běžně to však stále nefunguje! Nevíte v čem by mohl být problém? Ještě jsem nalezl část kódu týkajícího se formuláře v javascriptu: /*----------------------------------------------------*/
/* Contact Form
/*----------------------------------------------------*/
(function() {
var animateSpeed=1000;
var emailReg = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
//if submit button is clicked
function validateName(name)
{
if (name.val()=='*')
{
name.addClass('validation-error',animateSpeed);
return false;
}
else
{
name.removeClass('validation-error',animateSpeed);
return true;
}
}
function validateEmail(email,regex)
{
if (!regex.test(email.val()))
{
email.addClass('validation-error',animateSpeed);
return false;
}
else
{
email.removeClass('validation-error',animateSpeed);
return true;
}
}
function validateMessage(message)
{
if (message.val()=='')
{
message.addClass('validation-error',animateSpeed);
return false;
}
else
{
message.removeClass('validation-error',animateSpeed);
return true;
}
}
$('#send').click(function()
{
// result of action
var result=true;
//Get the data from all the fields
var name = $('input[name=name]');
var email = $('input[name=email]');
var message = $('textarea[name=message]');
// validate of name input
if(!validateName(name)) result=false;
if(!validateEmail(email,emailReg)) result=false;
if(!validateMessage(message)) result=false;
if(result==false) return false;
//organize the data properly
var data = 'name=' + name.val() + '&email=' + email.val() + '&message=' + encodeURIComponent(message.val());
//disabled all the text fields
$('.text').attr('disabled','true');
//show the loading sign
$('.loading').fadeIn('slow');
//start the ajax
$.ajax({
//this is the php file that processes the data and send mail
url: "contact.php",
//GET method is used
type: "GET",
//pass the data
data: data,
//Do not cache the page
cache: false,
//success
success: function (html) {
//if process.php returned 1/true (send mail success)
if (html==1) {
//show the loading sign
$('.loading').fadeOut('slow');
//show the success message
$('.success-message').slideDown('slow');
//deactivate submit
$('#send').attr('disabled',true);
//if process.php returned 0/false (send mail failed)
} else
{
$('.loading').fadeOut('slow')
alert('Sorry, unexpected error. Please try again.');
}
}
});
//cancel the submit button default behaviours
return false;
});
$('input[name=name]').blur(function(){
validateName($(this));
});
$('input[name=email]').blur(function(){
validateEmail($(this),emailReg);
});
$('textarea[name=message]').blur(function(){
validateMessage($(this));
});
})();Edit: aktuálně mám info, že právě javascript nepouští funkci dál, ale otázka zní co? |
||
|
Časová prodleva: 4 dny
|
|||
| baffik Profil |
#5 · Zasláno: 29. 5. 2015, 12:01:50
Vyřešeno samostatně, nalezena chyba v kontrole formuláře u jména.
|
||
| smiesek Profil |
#6 · Zasláno: 29. 5. 2015, 14:04:59
baffik:
v rámci se něčemu přiučit a nového nastudovat, ať koukám na to jak koukám, můžete prosím vypsat číslo řádku, kde byla chyba v kontrole? |
||
|
Časová prodleva: 3 dny
|
|||
| baffik Profil |
#7 · Zasláno: 1. 6. 2015, 07:51:08
smiesek:
v javascriptu řádek 13. vymazání hvězdičky, pak to začalo fungovat.[/b] |
||
|
Časová prodleva: 20 dní
|
|||
| Pesso69 Profil |
#8 · Zasláno: 21. 6. 2015, 14:46:37
Zdravim, najprv som si vsimol, ze element input ma atribut type="button". Ked chceme odosielat formular je potrebne pouzit type="submit"
Problem, moze byt aj u niektorych webhostingoch, ktore nedovolia odoslat email cez PHP, ked sa zada neexistujuci email FROM vramci webhostingu samozrejme. Tento kod, pojde urcite: <!--// contact form -->
<div id="contact-form">
<form method="post" action="contact.php">
<div class="field">
<label>Jméno:</label>
<input type="text" name="name" class="text" />
</div>
<div class="field">
<label>E-mail: <span>*</span></label>
<input type="text" name="email" class="text" />
</div>
<div class="field">
<label>Zpráva: <span>*</span></label>
<textarea name="message" class="text textarea" ></textarea>
</div>
<div class="field">
<input type="submit" name="odoslat" id="send" value="Odeslat zprávu"/>
<div class="loading"></div>
</div>
</form>
</div>
<!-- contact form //-->
<?php
// contact.php
if(( null !== $_POST['odoslat'])) {
if( ($_POST['name'] === null) AND ($_POST['email'] === null) AND ($_POST['message'] === null) ) {
echo 'Vyplňte všetky polia označené *';}
else {
require('class.phpmailer.php');
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';
$mail->IsHTML(true);
$mail->setFrom('emailfrom@example.sk', 'EmailFrom');
$mail->addAddress('emailto@example.sk');
$mail->Subject = 'Predmet spravy';
$message .= 'Name: '.$_POST['name'];
$message .= 'E-mail: '.$_POST['email'];
$message .= 'Sprava: '.$_POST['message'];
$mail->Body = $message;
if (!$mail->send()) {
echo "Chyba: ".$mail->ErrorInfo;
} else {
echo "Správa bola úspešne odoslaná";
}
}
?> |
||
|
Časová prodleva: 10 let
|
|||
0