我想提交一個帶有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_)放在最後。 –