Както е ясно, че съм доста зле с ajax-a и js, като цяло. Затова отново моля за помощ, че се оплетох бая.
Искам след събмит на формата да си изпълни добавянето в базата и да покаже съобщение за успех. Ето, какво измайсторих за момента, но освен, че рефрешва страницата, то нищо и не добавя в базата. Сигурен съм, че проблема е в javascrpta, понеже пробвах директно action на формата да е към Post.php и тогава работи.
това е формата
и php-то
Искам след събмит на формата да си изпълни добавянето в базата и да покаже съобщение за успех. Ето, какво измайсторих за момента, но освен, че рефрешва страницата, то нищо и не добавя в базата. Сигурен съм, че проблема е в javascrpta, понеже пробвах директно action на формата да е към Post.php и тогава работи.
Код:
$(".open2").click(function() {
function submitForm()
{
var data = $("#basicform").serialize();
$.ajax({
type : 'POST',
url : 'post.php',
data : data,
success : function(data)
{
if(data=="success")
{
$("#loader").show();
setTimeout(function(){
$("#basicform").html('<img src="assets/images/logo3.png" style="width:450px;height:250px;margin-left: 30%;"><h2>Awesome! We have received your details! Sit tight while we prepare your itinerary!</h2>');
}, 1000);
return false;
}
}
});
return false;
}
/* form submit */
});
Код:
<form name="basicform" id="basicform" method="post" action="" role="form">
<div class="panel panel-primary setup-content" id="step-1">
<div class="panel-heading">
<h3 class="panel-title">Tell us about yourself</h3>
</div>
<div class="panel-body">
<div class="form-group">
<label class="control-label">Your Name *</label>
<input maxlength="100" type="text" name="entry_name" required="required" class="form-control" placeholder="" />
</div>
<button class="btn btn-primary nextBtn pull-right open1" type="button">Next</button>
</div>
</div>
<div class="panel panel-primary setup-content" id="step-2">
<div class="panel-heading">
<h3 class="panel-title">Final Step!</h3>
</div>
<div class="panel-body">
<div class="form-group">
<label class="control-label">By submitting the form , you agree to the Terms & Conditions and Privacy Policy</label>
<div class="editable" disabled><?php include 'privandeerms.php'; ?></div>
</div>
<button class="btn btn-danger pull-left" type="reset" >I Don’t Agree</button>
<button class="btn btn-success pull-right open2" type="submit" name="add" id="add">I agree. I’m ready to get my itinerary!</button>
<img src="assets/images/spinner.gif" alt="" id="loader" style="display: none; width:32px;height:32px;">
</div>
</div>
</form>
Код:
if(isset($_POST)){
$entry_name = $_POST['entry_name'];
try {
$stmt = $pdo->prepare('INSERT INTO entries (entry_name) VALUES (:entry_name)');
$stmt->bindParam(":entry_name",$entry_name);
if($stmt->execute())
{
echo "success";
}
//else catch the exception and show the error.
} catch(PDOException $e) {
var_dump ($e->getMessage());
exit;
}
}