Hej, poniżej zamieszczam kod formularza. Formularz:
- sprawdza poszczególne pola formularza i w razie czego wyświetla błąd
- jeśli wszystko jest ok to wysyła formularz i wyświetla "ok"
Chciałbym dodać funkcję, która po wyświetleniu wiadomości "ok" czyści pola formularza (tak jak przycisk RESET).
<div id="responseHolder"></div>
<form id="f-kontakt" action="kontakt.php?lang=en" method="post">
<dl>
<dt><label for="f-imie">Forename</label><span class="required">*</span></dt>
<dd><input id="f-imie" name="imie" type="text" /></dd>
</dl>
<dl>
<dt><label for="f-nazwisko">Surname </label><span class="required">*</span></dt>
<dd><input id="f-nazwisko" name="nazwisko" type="text" /></dd>
</dl>
<dl>
<dt><label for="f-email">E-mail</label><span class="required">*</span></dt>
<dd><input id="f-email" name="email" type="text" /></dd>
</dl>
<dl>
<dt><label for="f-telefon">Telephone</label><span class="required">*</span></dt>
<dd><input id="f-telefon" name="telefon" type="text" /></dd>
</dl>
<dl>
<dt><label for="f-tresc">Message</label></dt>
<dd><textarea id="f-tresc" cols="50" rows="4" name="tresc"></textarea></dd>
</dl>
<div style="display: none">leave this empty:<input type="text" name="www" /></div>
<button>Send</button>
</form>
<script type="text/javascript">// <![CDATA[
$("#responseHolder").hide();
$("#f-kontakt").bind("submit", {}, function() {
$("#responseHolder").load($("#f-kontakt").attr("action"), function(e){
var ret={};
e.each(function(ii ,ee){
ret[$(ee).attr("name")]=$(ee).val();
});
return ret;
}($("#f-kontakt :text, #f-kontakt textarea")), function(response, status){
if(status != "success") {
$(connectionError=document.createElement("div")).addClass("error").attr("id", "response").text("Connection failed, please try again");
$("#responseHolder").append(connectionError);
}
$(this).show("slow")
});
return false;
});
// ]]></script>
<?php
error_reporting(E_ALL);
$to='[email protected]';
$i18n=create_function('$key', '
$lang=@$_REQUEST["lang"]?$_REQUEST["lang"]:"pl";
$trans=array(
"en" => array(
"Błąd wysyłania formularza" => "Error sending form",
"imię" => "forename",
"nazwisko" => "surname",
"telefon" => "telephone",
"adresu email" => "e-mail",
"Pole %s jest wymagane i nie może zostać pominięte" => "The field %s is required",
"Adres E-mail niepoprawny" => "E-Mail address is invalid",
"Błąd wysyłania wiadomości" => "Error sending message",
"Wiadomość wysłana poprawnie" => "Message sent successfully",
"Formularz uznany za spam" => "Spam detected"
)
);
return (@$trans[$lang][$key])?$trans[$lang][$key]:$key;
');
try {
function odsyf($s) {
return wordwrap(trim(htmlspecialchars(get_magic_quotes_gpc()?stripslashes($s):$s, 120)));
}
if(@$_POST['www'] != '')
throw new ErrorException($i18n('Formularz uznany za spam'));
if(!isset($_POST['imie']) || !isset($_POST['nazwisko']) || !isset($_POST['telefon']) || !isset($_POST['email']) || !isset($_POST['tresc']))
throw new ErrorException($i18n('Błąd wysyłania formularza'));
$tresc=odsyf($_POST['tresc']);
foreach(array(
$i18n('imię') => $imie=odsyf($_POST['imie']),
$i18n('nazwisko') => $nazwisko=odsyf($_POST['nazwisko']),
$i18n('telefon') => $telefon=odsyf($_POST['telefon']),
$i18n('adresu email') => $email=$_POST['email']
) as $k=>$a)
if($a == "")
throw new ErrorException(sprintf($i18n("Pole %s jest wymagane i nie może zostać pominięte"), $k));
if(!preg_match('/^([^<>\\(\\)\\[\\]\\\\;:, @]*[^<>\\(\\)\\[\\]\\\\;:, .@]|("(\\\\.|[^\\\\"])+"))@([\\p{L}\\p{S}\\p{N}0-9_-]{1,63}\\.)+[a-z]{2,6}$/ui', $email))
throw new ErrorException($i18n('Adres E-mail niepoprawny'));
if(!mail($to, "domena.pl - $imie $nazwisko".($tresc
?(": ".($l=substr($tresc, 0, 40)).(strlen($l)>39?'...':''))
:''), "$imie $nazwisko\n$email\n$telefon\n\n$tresc", join("\r\n", array(
"From: $email",
"Reply-To: $email",
"X-Mailer: domena.pl",
"X-IP: {$_SERVER['REMOTE_ADDR']}",
"MIME-Version: 1.0",
"Content-type: text/plain; charset=utf-8"
))))
throw new ErrorException($i18n('Błąd wysyłania wiadomości'));
?><div id="mailResponse" class="success"><?=$i18n("Wiadomość wysłana poprawnie")?></div><?php
}
catch(ErrorException $e) {
header('content-type: text/html; charset=utf-8');
?><div id="mailResponse" class="error"><?echo $e->getMessage(); ?></div><?php
}
?>
Nie jest to kod mojego autorstwa, poproszony o przeróbkę chciałem zapytać kogoś bardziej biegłego w temacie zanim coś pomieszam :)
Pozdrawiam,
Kamil