在ajax(原生JS/notJQUERY)中将成功消息传输到重定向主页
Transfering the success message to the redirected main page in ajax (native JS/not JQUERY)
我有一个任务应用程序,其中管理员有权在主页上编辑任务。更新在新打开的页面上完成,然后显示结果“数据已保存”,重定向到主页并且必须将消息“已编辑”传达给重定向页面上的任务。也许有人可以给我一个关于如何将消息从 Ajax?
传输到重定向页面的建议
JS:
edit_form.addEventListener('submit', async(event)=>{
event.preventDefault();
try {
const response = await fetch(`/tasks/save/${id}`, {//обработчик action
method: 'POST',
body: new FormData(edit_form)
});
let answer = await response.text();
console.log("ответ сервера " + answer);
if (answer === true){
result.innerHTML = TASK_SAVED;
}else{
result.innerHTML = TASK_UNSAVED ;
setTimeout("window.location.replace('/')" , 2000);
}
}catch (error) {
console.log("ошибка", error);
}
});
PHP:
public function saveAction($id){
$saved_data=$this->request->post();
$saved =$this->mainservice->saveTask($saved_data, $id);
header('Content-Type: text/plain');
if ($saved ==="1"){
echo "Task saved";
}if ($saved ==="0")
echo "task isn't saved";
}
public function saveTask($saved_data, $id){
$sql = "UPDATE tasks SET name = :name, email = :email, textarea = :textarea , status= :status where id = :id";
$params =[
'name' => trim($saved_data['name']),
'email'=> trim( $saved_data['email']),
'textarea'=> trim($saved_data['textarea']),
'status' => trim($saved_data['status'][0]),
'id'=>$id
];
$dbConnection = $this->dbConnection->getConnection();
return $this->dbConnection->executeSql($sql, $params);
你可以用session转消息然后用js函数catch调用flash
将您的编辑结果保存在 $_SESSION['msg'] 和 $_SESSION['class'] 中以 bootstrap
控制消息颜色
在您的 php 代码中
function flash() {
$message = $_SESSION['msg'];
$class = $_SESSION['class'];
if(!empty($message)){
echo '<div class="bg-'.$class.' msg-flash alert alert-warning alert-dismissible fade show" role="alert"
style="transition: all .9s ease; width:100%;" >'.$message.'<button type="button" class="close white "
data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button></div>';
}
// empty $message after show
$message = "";
}
在您的 javascript 代码中
function flash() {
// collect result from php session msg by http request
}
运行 函数在 javascript 文件中的每个函数之后和 dom 加载后
我有一个任务应用程序,其中管理员有权在主页上编辑任务。更新在新打开的页面上完成,然后显示结果“数据已保存”,重定向到主页并且必须将消息“已编辑”传达给重定向页面上的任务。也许有人可以给我一个关于如何将消息从 Ajax?
传输到重定向页面的建议JS:
edit_form.addEventListener('submit', async(event)=>{
event.preventDefault();
try {
const response = await fetch(`/tasks/save/${id}`, {//обработчик action
method: 'POST',
body: new FormData(edit_form)
});
let answer = await response.text();
console.log("ответ сервера " + answer);
if (answer === true){
result.innerHTML = TASK_SAVED;
}else{
result.innerHTML = TASK_UNSAVED ;
setTimeout("window.location.replace('/')" , 2000);
}
}catch (error) {
console.log("ошибка", error);
}
});
PHP:
public function saveAction($id){
$saved_data=$this->request->post();
$saved =$this->mainservice->saveTask($saved_data, $id);
header('Content-Type: text/plain');
if ($saved ==="1"){
echo "Task saved";
}if ($saved ==="0")
echo "task isn't saved";
}
public function saveTask($saved_data, $id){
$sql = "UPDATE tasks SET name = :name, email = :email, textarea = :textarea , status= :status where id = :id";
$params =[
'name' => trim($saved_data['name']),
'email'=> trim( $saved_data['email']),
'textarea'=> trim($saved_data['textarea']),
'status' => trim($saved_data['status'][0]),
'id'=>$id
];
$dbConnection = $this->dbConnection->getConnection();
return $this->dbConnection->executeSql($sql, $params);
你可以用session转消息然后用js函数catch调用flash
将您的编辑结果保存在 $_SESSION['msg'] 和 $_SESSION['class'] 中以 bootstrap
在您的 php 代码中
function flash() {
$message = $_SESSION['msg'];
$class = $_SESSION['class'];
if(!empty($message)){
echo '<div class="bg-'.$class.' msg-flash alert alert-warning alert-dismissible fade show" role="alert"
style="transition: all .9s ease; width:100%;" >'.$message.'<button type="button" class="close white "
data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button></div>';
}
// empty $message after show
$message = "";
}
在您的 javascript 代码中
function flash() {
// collect result from php session msg by http request
}
运行 函数在 javascript 文件中的每个函数之后和 dom 加载后