Nicht mehr ganz neu hier
Hi, ich hab hier ein einfaches Formular mit input feld das über jQ - ajax in die Datenbank eingetragen wird. Ich möchte nun bei success den zurückgelieferten wert prüfen und darauf hin meine Nachricht an den User anpassen (war die Eingabe erfolgreich oder nicht). Das klappt, allerdings möchte ich bei einem Fehler die Fehlernachricht anzeigen und nach ca. 10sekunden soll wieder das Formular geladen werden, das bekomm ich nicht hin.
Dazu hier mein Code:
INDEX.PHP
Ajax.php
Dazu hier mein Code:
INDEX.PHP
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Portfolio </title>
<link rel="stylesheet" href="css/template.css" type="text/css" />
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("form#submitform").submit(function() {
// we want to store the values from the form input box, then send via ajax below
var fname = $('#email').attr('value');
$.ajax({
type: "POST",
url: "ajax.php",
data: "fname="+ fname,
success: function(result){
$('#formElemWrap').fadeOut('slow', function() {
if(result == '1') {
$('#postBack').append('Your email has been stored.');
}
if(result == '2') {
$('#postBack').append('Oops there was a problem storing your email.');
}
if(result == '3') {
$('#postBack').append('Please enter a valid email address.');
}
$('#postBack').fadeIn(1250);
});
}
});
return false;
});
});
</script>
</head>
<body>
<div id="container">
<div id="header">
<div id="logo"><img src="Bilder/logo.jpg" alt="Logo P" title="Logo P" /></div><!-- EOF logo -->
<div id="nav">
<div id="border-left"> </div><!-- EOF border-left -->
<div id="navBg">
<ul id="navigation">
<li id="home">Home</li>
<li id="service">Service</li>
<li id="aboutUs">About Us</li>
<li id="contact">Contact</li>
<li id="faq">FAQ</li>
</ul>
</div><!-- EOF navBg -->
</div><!-- EOF nav -->
</div><!-- EOF header -->
<div id="preview">
<!-- LAUNCHING PAGE -->
<div id="mainContentWrap">
<div id="launch">
<div class="mainBoxLaunchingText">
<img src="Bilder/contBoxLaunchText.png" alt="We‘re not quite there yet - but we‘re getting there ! - and really want you to know when we‘re ready. Here‘s how to stay updated:" title="stay updated" />
<br />
</div>
<div id="form" style="display: block;">
<form id="submitform" method="post" style="display: block;" action="">
<div id="formElemWrap">
<div id="email1" style="float: left; margin-top: -4px; margin-right: 10px;"><label for="email" style="margin-top: -4px;"><img src="Bilder/email.png" alt="" /></label></div>
<div id="email2" style="float: left;"><input type="text" id="email" name="email" /></div>
<div id="sub" style="float: left; margin-top: -4px; margin-left: 10px;"><input type="image" src="Bilder/a_subscribe.png" id="submit" /></div>
</div>
<ul id="success"><li id="postBack" style="display: none"> </li></ul>
</form>
</div>
</div>
</div><!-- EOF mainContentWrap -->
<div id="sidebarWrap">
<div class="sidebarBoxWrap">
<div class="sbWhiteBorder"> </div>
<div class="sidebarTop"> </div>
<div class="sidebarRep"> </div>
<div class="sidebarFoot"> </div>
</div><!-- EOF sidebarBoxWrap -->
</div><!-- EOF sidebarWrap -->
</div><!-- EOF container -->
</body>
</html>
PHP:
<?php
include ("db/database.php");
$database = new database();
// CLIENT INFORMATION
$fname = htmlspecialchars(trim($_POST['fname']));
$pattern = '/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/';
if(preg_match($pattern, $fname)) {
$addClient = "INSERT INTO launch (email) VALUES ('$fname')";
$set = $database->setQuery($addClient);
if($set) {
$success = 1;
echo $success;
} else {
$success = 2;
echo $success;
}
} else {
$success = 3;
echo $success;
}
?>