我想使用ajax提交表单,我的脚本代码也没有任何错误,但我的php也没有任何错误,但是当我单击“提交”按钮时,什么也没发生,只是表单将为空,否则什么也没发生,数据没有存储在数据库中。我不知道这是什么错误。我不知道如何找到它,有人可以帮我解决这个问题吗?
<div class="container-fluid">
<h3 class="alert alert-success" align="center">CHECKOUT FORM</h3>
<form id="checkout_form">
<div class="row">
<div class="col-md-2">
</div>
<div class="col-md-4">
<div class="form-group">
<label for="usr">Name:</label>
<input type="text" class="form-control" id="name" name="name">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" class="form-control" id="email" name="email">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address">
</div>
<div class="form-group">
<label for="city">City:</label>
<input type="text" class="form-control" id="city" name="city">
</div>
<div class="form-group">
<label for="city">State:</label>
<input type="text" class="form-control" id="state" name="state">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="card">Card Name:</label>
<input type="text" class="form-control" id="card_name" name="card_name">
</div>
<div class="form-group">
<label for="cvv">CVV:</label>
<input type="text" class="form-control" id="cvv" name="cvv">
</div>
<div class="form-group">
<label for="card_num">Card Number:</label>
<input type="text" class="form-control" id="card_number" name="card_number">
</div>
<div class="form-group">
<label for="expired_month">Expire Month:</label>
<input type="text" class="form-control" id="expired_month" name="expired_month">
</div>
<div class="form-group">
<label for="zip">Zip Code:</label>
<input type="text" class="form-control" id="zip_code" name="zip_code">
</div>
<input type="submit" name="submit" class="btn btn-success" value="CHECKOUT" action = "checkout" id="submit">
</div>
</div>
</form>
</div>
脚本代码
<script>
$(document).ready(function(){
$('#submit').click(function(){
var name = $('#name').val();
var email = $('#email').val();
var address = $('#address').val();
var city = $('#city').val();
var state = $('#state').val();
var card_name = $('#card_name').val();
var cvv = $('#cvv').val();
var card_number = $('#card_number').val();
var expired_month = $('#expired_month').val();
var zip = $('#zip_code').val();
var data_string ='name='+ name + '&email=' + email + '&address=' + address + '&city=' + city + '&state=' + state + '&card_name=' + card_name + '&cvv=' + cvv + '&card_number=' + card_number + '&expired_month' + expired_month + '&zip' + zip_code ;
$.ajax({
url : "action1.php",
method : "POST",
data :data_string,
success:function(data){
$('#checkout_form').html("<div id='message'></div>");
$('#message').val("form submitted");
}
});
});
});
</script>
action1.php
<?php
include('database_connection.php');
if(isset($_POST['submit']){
$data = array(
':name' => $_POST['name'],
':email' => $_POST['email'],
':address' => $_POST ['address'],
':city' => $_POST['city'],
':state' => $_POST['state'],
':card_name' => $_POST ['card_name'],
':cvv' => $_POST['cvv'],
':card_number' => $_POST['card_number'],
':expired_month' => $_POST['expired_month'],
':zip_code'=> $_POST['zip_code']
);
$query = "INSERT INTO checkout (name,email,address,city,state,card_name,cvv,card_number,expired_month,zip_code) VALUES (:name,:email,:address,:city,:state,:card_name,:cvv,:card_number,:expired_month,:zip_code)";
$statement = $connect ->prepare($query);
if($statement->execute($data))
{
echo "form submitted";
}
}
?>
答案 0 :(得分:0)
单击提交,您的表单页面将刷新。这样做:
$('#submit').click(function(e){
e.preventDefault;
如评论中所述,
echo $query;
exit; //This exit is causing the query not to run because the script stops running.
您的字段名称前面有分号。删除它们:
$data = array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'address' => $_POST ['address'],
'city' => $_POST['city'],
'state' => $_POST['state'],
'card_name' => $_POST ['card_name'],
'cvv' => $_POST['cvv'],
'card_number' => $_POST['card_number'],
'expired_month' => $_POST['expired_month'],
'zip_code'=> $_POST['zip_code']
);
答案 1 :(得分:0)
欢迎来到Stackoverflow @ prabhu-msd
在此代码下将e.preventDefault;
添加为第一行:
$('#submit').click(function(){
它防止刷新页面并运行Ajax。在成功下,在location.reload();
行之后添加$('#message').val("form submitted");
,以在提交数据后刷新页面(如果要刷新页面),否则可以执行任何操作。
在您的PHP文件中,您正在使用退出,这会阻止执行查询。请删除该exit
。