今天遇到了一个奇怪的问题。所以我试图使用AJAX将数据插入到数据库中,这样页面就不需要重新加载以查看更改。但是,当我尝试插入数据,不知道如何把它,insert.php文件不会得到“触发”,例如行“你好Im插入.php”不显示。但是,Ajax代码工作正常,成功块被触发。PHP文件未使用jQuery/AJAX触发
形式:
<form action="insert.php" method="post" class="form-inline" id="forma">
<input type="text" class="form-control" name="inputName" id="inputName" placeholder="Name">
<input type="text" class="form-control" name="inputPrice" id="inputPrice" placeholder="Price">
<input type="text" class="form-control" name="inputStore" id="inputStore" placeholder="Store">
<input type="text" class="form-control" name="inputDate" id="inputDate" placeholder="Date"></br>
<button id="submit" type="submit" class="btn btn-success">Submit</button>
</form>
的jQuery:
$('#forma').submit(function() {
var name = $("#inputName").val();
var price = $("#inputPrice").val();
var store= $("#inputStore").val();
var date = $("#inputDate").val();
$.ajax({
type: "POST",
url: "insert.php",
data: {
inputPavadinimas: name,
inputKaina: price,
inputParduotuve: store,
inputData: date,
},
success: function() {
$("#success").show();
alert('success');
},
error: function(){
alert('error');
}
});
return false;
});
insert.php:
<?php
error_reporting(0);
require 'db/connection.php';
echo 'Hello Im insert.php';
if(!empty($_POST)){
print_r($_POST);
if(isset($_POST['inputName'],$_POST['inputPrice'],$_POST['inputStore'],$_POST['inputDate'])){
$name = trim($_POST['inputName']);
$price = trim($_POST['inputPrice']);
$store = trim($_POST['inputStore']);
$date = trim($_POST['inputDate']);
if(!empty($name) && !empty($price) && !empty($store) && !empty($date)){
$insert = $db->prepare("INSERT INTO prekes(name, price, store, date) VALUES (?, ?, ?, ?)");
$insert->bind_param('ssss',$name, $price, $store, $date);
if($insert->execute()){
echo 'INSERTED';
}
}
}
};
或者记录不被插入或仅回声值不打印?哪一个是你的问题? –
您的isset语句不会评估为真,因为您没有传递这些参数 – billyonecan
这个字符是什么'};'insert.php的最后一行。这是PHP的有效字符? – Baron