gpt4 book ai didi

php - 使用ajax提交结帐表单

转载 作者:行者123 更新时间:2023-12-01 05:13:56 24 4
gpt4 key购买 nike

我想使用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";
}
}
?>

最佳答案

欢迎来到 Stackoverflow @prabhu-msd

添加 e.preventDefault; 作为此代码下的第一行:

$('#submit').click(function(){

它会阻止页面刷新并运行 Ajax。成功后,在此行 $('#message').val("form Submitted"); 后面添加 location.reload();,以便在数据加载后刷新页面已提交(如果您想刷新页面),否则做您想做的事。

在您的 PHP 文件中,您使用 exit 来阻止查询的执行。请删除该导出

关于php - 使用ajax提交结帐表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54437330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com