我想提交一个带有ajax jQuery的表单,并且我希望在提交表单时确认提醒。在PHP和Sweet Alert中使用OOP时出现ajax表单提交错误
表单时,我没有使用任何AJAX或JavaScript,但我想提交表单无需重新加载页面,我想警报点击提交和取消按钮与甜美出现之前工作正常警报方法(即swal()
)。
这是我的HTML:
<form role="form" id="myform" class="countrysaveform" action="include/db_connect/actions.php" method= "POST" >
<div class="form-body">
<div class="form-group">
<label>Cournty Name</label>
<input type="text" name = "country" id="country" class="form-control" placeholder="">
</div>
<div class="form-group">
<input type="hidden" name= "userip" id= "userip" class="form-control" value="<?php $ip=$_SERVER['REMOTE_ADDR']; echo $ip; ?>" placeholder="">
</div>
<div class="form-group">
<label>Status</label>
<select name ="status" id= "status" class="form-control">
<option value="active">Active</option>
<option value="inactive">Inactive</option>
</select>
</div>
</div>
<div class="form-actions">
<button type="submit" name="save" id="save" class="btn blue">SAVE</button>
<button type="button" class="btn default">Cancel</button>
</div>
</form>
这是我class.php:
<?php
require 'connection.php';
//echo $current_timestamp;
class db_class extends db_connect{
public function __construct(){
$this->connect();
}
public function create($country, $status, $ip){
$stmt = $this->conn->prepare("INSERT INTO `ux_country` (`country_name`, `country_status`,`added_by_ip`,`added_date`,`added_on_time`) VALUES (?, ?, ?, now(), now())") or die($this->conn->error);
$stmt->bind_param("sss", $country, $status, $ip);
if($stmt->execute()){
$stmt->close();
$this->conn->close();
return true;
}
}
这是我Connection.php:
<?php
define('db_host', 'localhost');
define('db_user', 'root');
define('db_pass', '');
define('db_name', 'uniquesolutions');
class db_connect{
public $host = db_host;
public $user = db_user;
public $pass = db_pass;
public $dbname = db_name;
public $conn;
public $error;
public function connect(){
$this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
if(!$this->conn){
$this->error = "Fatal Error: Can't connect to database" . $this->connect->connect_error();
return false;
}
}
}
?>
这是我的action.php我在这里调用它来存储在数据库中。
require_once 'class.php';
if(ISSET($_POST['save'])){
$country = $_POST['country'];
$status = $_POST['status'];
$ip = $_POST['userip'];
//$current_timestamp = date();
$conn = new db_class();
$conn->create($country, $status, $ip);
header('location:../../Country_View.php');
}
这是MySQL数据库的结构:
TABLE `ux_country` (
`country_id` int(11) NOT NULL,
`country_name` varchar(25) NOT NULL,
`country_status` varchar(12) NOT NULL,
`added_date` date NOT NULL,
`added_on_time` time NOT NULL,
`added_by_ip` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
这是甜的,报警代码:
$(document).ready(function(){
$('#myform').on('submit',function(e) { //Don't foget to change the id form
$.ajax({
url:'include/db_connect/actions.php', //===PHP file name====
data:$(this).serialize(),
type:'POST',
success:function(data){
console.log(data);
//Success Message == 'Title', 'Message body', Last one leave as it is
swal("¡Success!", "Message sent!", "success");
},
error:function(data){
//Error Message == 'Title', 'Message body', Last one leave as it is
swal("Oops...", "Something went wrong :(", "error");
}
});
e.preventDefault(); //This is to Avoid Page Refresh and Fire the Event "Click"
});
});
所以,如果我明白,你只是想警报之前提交表单?你为什么不能在你的ajax调用之前移动'swal()'方法(即将它从你的回调方法中拉出来)?我错过了什么? – WillardSolutions
@SarfarazDalawi我看到你评论如下:“_when我点击保存按钮它显示成功消息,但不存储在数据库中,我认为它不是调用该actions.php文件_”。请参阅我更新后的答案,我将PHP代码(即_action.php_)放在最后。 –