我正在尝试使用PHP和ajax将数据提交到MySQL数据库但是它没有提交数据,当我尝试使用默认页面重新加载方法提交数据时,提交数据可能是我的ajax代码中存在一些问题。请在这方面指导我。
HTML代码:
<form method="POST" id="custForm" class="addcustform">
<div class="header">
<h2>
<strong>ADD CUSTOMER</strong>
<span class="pull-right sys-id">System ID - <input type="text" name="sys_id_input" class="sys_id_input" /></span>
</h2>
</div>
<div class="body">
<h2 class="card-inside-title">Personal Detials</h2>
<div class="row clearfix">
<div class="col-sm-6">
<div class="form-group">
<label>Customer Name</label>
<div class="form-line">
<input type="text" name="cust_name" class="form-control" />
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Email</label>
<div class="form-line">
<input type="email" name="cust_email" class="form-control" />
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6">
<div class="form-group">
<label>Mobile Number</label>
<div class="form-line">
<input type="text" name="cust_mobile" class="form-control mobile-phone-number">
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Address</label>
<div class="form-line">
<input type="text" name="cust_address" class="form-control" />
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6">
<label>Visa Type</label>
<div class="btn-group bootstrap-select form-control show-tick select-dropdown-trick-container">
<button type="button" class="btn dropdown-toggle btn-default" data-toggle="dropdown" title="-- Please select --">
<span class="filter-option pull-left select-dropdown-trick-text">-- Please select --</span> <span class="bs-caret"><span class="caret"></span></span>
<input type="hidden" class="dropdown-input" name="cust_visa" />
</button>
<div class="dropdown-menu open">
<ul class="dropdown-menu inner select-dropdown-trick" role="menu">
<li data-original-index="0" class="selected">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">-- Please select --</span><span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<li data-original-index="1">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">Visit Visa</span><span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<li data-original-index="2">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">Residental Visa</span><span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Nationality</label>
<div class="btn-group bootstrap-select form-control show-tick select-dropdown-trick-container">
<button type="button" class="btn dropdown-toggle btn-default" data-toggle="dropdown" title="-- Please select --">
<span class="filter-option pull-left select-dropdown-trick-text">-- Please select --</span> <span class="bs-caret"><span class="caret"></span></span>
<input type="hidden" class="dropdown-input" name="cust_nationality" />
</button>
<div class="dropdown-menu open large-list-short">
<ul class="dropdown-menu inner select-dropdown-trick" role="menu">
<?php include 'country_list.php'; ?>
</ul>
</div>
</div>
</div>
</div>
</div>
<h2 class="card-inside-title">Car Detials</h2>
<div class="row clearfix">
<div class="col-sm-6">
<div class="form-group">
<label>Car Modal</label>
<div class="btn-group bootstrap-select form-control show-tick select-dropdown-trick-container">
<button type="button" class="btn dropdown-toggle btn-default" data-toggle="dropdown" title="-- Please select --">
<span class="filter-option pull-left select-dropdown-trick-text">-- Please select --</span> <span class="bs-caret"><span class="caret"></span></span>
<input type="hidden" class="dropdown-input" name="cust_cars" />
</button>
<div class="dropdown-menu open large-list-short">
<ul class="dropdown-menu inner select-dropdown-trick" role="menu">
<?php include 'cars_list.php'; ?>
</ul>
</div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Car Color</label>
<div class="btn-group bootstrap-select form-control show-tick select-dropdown-trick-container">
<button type="button" class="btn dropdown-toggle btn-default" data-toggle="dropdown" title="-- Please select --">
<span class="filter-option pull-left select-dropdown-trick-text">-- Please select --</span> <span class="bs-caret"><span class="caret"></span></span>
<input type="hidden" class="dropdown-input" name="car_color" />
</button>
<div class="dropdown-menu open">
<ul class="dropdown-menu inner select-dropdown-trick" role="menu">
<li data-original-index="0" class="selected">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">-- Please select --</span><span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<li data-original-index="1">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">White</span><span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<li data-original-index="2">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">Black</span><span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<li data-original-index="2">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">Silver</span><span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<li data-original-index="2">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">Blue</span><span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<li data-original-index="2">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">Red</span><span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<li data-original-index="2">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">Brown</span><span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Plate Number</label>
<div class="form-line">
<input type="text" name="plate_number" class="form-control plate-input" />
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Start KM</label>
<div class="form-line">
<input type="text" name="start_km" class="form-control" />
</div>
</div>
</div>
</div>
<h2 class="card-inside-title">Rental Detials</h2>
<div class="row clearfix">
<div class="col-sm-6">
<div class="form-group">
<label>Start Date</label>
<div class="form-line">
<input type="text" name="start_date" class="form-control date-input" />
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>End Date</label>
<div class="form-line">
<input type="text" name="end_date" class="form-control date-input" />
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6">
<div class="form-group">
<label>Rental Amount</label>
<div class="form-line">
<input type="text" name="rental_amount" class="form-control" />
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Security Amount</label>
<div class="form-line">
<input type="text" name="scurity_amount" class="form-control" />
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-6">
<div class="form-group">
<label>Rental Company</label>
<div class="btn-group bootstrap-select form-control show-tick select-dropdown-trick-container">
<button type="button" class="btn dropdown-toggle btn-default" data-toggle="dropdown" title="-- Please select --">
<span class="filter-option pull-left select-dropdown-trick-text">-- Please select --</span> <span class="bs-caret"><span class="caret"></span></span>
<input type="hidden" class="dropdown-input" name="rental_company" />
</button>
<div class="dropdown-menu open large-list-short">
<ul class="dropdown-menu inner select-dropdown-trick" role="menu">
<?php include 'rental_company_list.php'; ?>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-2">
<div class="form-group">
<button class="btn bg-red btn-block btn-lg waves-effect btn-addcust" name="btn-addcust" type="submit">Save Details</button>
</div>
</div>
</div>
</div>
<form>
PHP代码:
if(isset($_POST['btn-addcust'])) {
# code...
$sys_id_input = $_POST['sys_id_input'];
$cust_name = $_POST['cust_name'];
$cust_email = $_POST['cust_email'];
$cust_mobile = $_POST['cust_mobile'];
$cust_address = $_POST['cust_address'];
$cust_visa = $_POST['cust_visa'];
$cust_nationality = $_POST['cust_nationality'];
$cust_cars = $_POST['cust_cars'];
$car_color = $_POST['car_color'];
$plate_number = $_POST['plate_number'];
$start_km = $_POST['start_km'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$rental_amount = $_POST['rental_amount'];
$scurity_amount = $_POST['scurity_amount'];
$rental_company = $_POST['rental_company'];
$sql = "CREATE TABLE IF NOT EXISTS `$rental_company` (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
sys_id_input VARCHAR(255) NOT NULL,
cust_name VARCHAR(255) NOT NULL,
cust_email VARCHAR(255) NOT NULL,
cust_mobile VARCHAR(255) NOT NULL,
cust_address VARCHAR(255) NOT NULL,
cust_visa VARCHAR(255) NOT NULL,
cust_nationality VARCHAR(255) NOT NULL,
cust_cars VARCHAR(255) NOT NULL,
car_color VARCHAR(255) NOT NULL,
plate_number VARCHAR(255) NOT NULL,
start_km VARCHAR(255) NOT NULL,
start_date VARCHAR(255) NOT NULL,
end_date VARCHAR(255) NOT NULL,
rental_amount VARCHAR(255) NOT NULL,
scurity_amount VARCHAR(255) NOT NULL,
rental_company VARCHAR(255) NOT NULL
)";
$db_con->exec($sql);
$sql = "INSERT INTO `$rental_company` (sys_id_input, cust_name, cust_email, cust_mobile, cust_address, cust_visa, cust_nationality, cust_cars, car_color, plate_number, start_km, start_date, end_date, rental_amount, scurity_amount, rental_company) VALUES ('$sys_id_input', '$cust_name', '$cust_email', '$cust_mobile', '$cust_address', '$cust_visa', '$cust_nationality', '$cust_cars', '$car_color', '$plate_number', '$start_km', '$start_date', '$end_date', '$rental_amount', '$scurity_amount', '$rental_company')";
$db_con->exec($sql);
echo "ok";
}
Ajax代码:
$('.addcustform').submit(function(){
var data = $('#custForm').serialize();
$.ajax({
type : 'POST',
url : '../include/addcust.php',
data : data,
beforeSend: function()
{
//$("#alert").fadeOut();
$(".btn-addcust").html('Saving ...');
},
success : function(response)
{
if(response=="ok"){
setTimeout( function(){
// Do something after 1 second
$(".btn-addcust").html('Success');
} , 1000 );
}
//alert(data);
}
});
return false;
});
任何帮助都会得到满足。感谢
答案 0 :(得分:0)
如果你使用chrome F12
,可以转到Network
,然后提交表单并查看新添加的行,服务器的响应是什么样的(应该是ok
根据您发布的代码)。也许您将数据提交到错误的网址(尝试使用/include/addcust.php
之类的绝对网址。)
您还没有为按钮设置一个值,因此jQuery不会将其生效,您的第一行PHP将导致false
。所以只需在提交按钮中添加value="submit"
之类的内容即可。
答案 1 :(得分:0)
表单的操作是它自己的页面,您说该页面会在提交时刷新。好像你javascript并没有阻止表单的默认操作,所以即使你的AJAX代码工作,页面也会在它完成之前刷新。
您需要在.submit()函数中添加event.preventDefault();
。
有人问过类似的问题: Ajax Form submit with preventDefault
您还可以在此处阅读有关阻止默认操作的更多信息:
https://api.jquery.com/submit/
https://api.jquery.com/event.preventdefault/
$('.addcustform').submit(function(){
event.preventDefault();
var data = $('#custForm').serialize();
$.ajax({
type : 'POST',
url : '../include/addcust.php',
data : data,
beforeSend: function()
{
//$("#alert").fadeOut();
$(".btn-addcust").html('Saving ...');
},
success : function(response)
{
if(response=="ok"){
setTimeout( function(){
// Do something after 1 second
$(".btn-addcust").html('Success');
} , 1000 );
}
//alert(data);
}
});
return false;
});
答案 2 :(得分:0)
更改
型= “提交”
进入
型= “键”
在您的HTML代码
上