如何停止重新提交php

时间:2014-10-27 13:09:40

标签: php forms redirect post

我尝试使用header('location:login.php');但是不起作用它甚至不会重定向页面。有任何想法吗? 我也试过用JavaScript删除输入但重新提交不停止。 无论在哪里放头('location:login.php');它不会起作用。

if(isset($_POST['vrsta_predmeta']) AND !empty($_POST['vrsta_predmeta']) AND 

    isset($_POST['res_text']) AND isset($_POST['glavni_dug']) AND isset($_POST['res']) AND isset($_POST['zaklj']) AND isset($_POST['povjerilac']) AND isset($_POST['duznik']) AND isset($_POST['predmet_zaveden'])){


    $racunob =  trim($_POST['rac']);
    $obrazlozenje = trim($_POST['obr']);     
    $ob_text = trim($_POST['res_ob']);  
    $res_text = trim($_POST['res_text']);
    $vrsta_pre = trim($_POST['vrsta_predmeta']);
    $izvrsenje = trim(strtolower($_POST['res']));
    $obrazac = trim($_POST['zaklj']);
    $povjerilac = $_POST['povjerilac'];
    $duznik = $_POST['duznik'];
    $datum= trim($_POST['predmet_zaveden']);
     foreach($povjerilac as $key){
     $lica = $db -> prepare("INSERT INTO p_lica(povjerilac, doc_br, dokument_vlasnik) VALUES('$key', '$dok_broj', '$ses_val')");



    }       


    foreach($duznik as $key1){
     $lica1 = $db -> prepare("INSERT INTO d_lica(duznik,doc_br, dokument_vlasnik) VALUES('$key1', '$dok_broj', '$ses_val')");


    }

    $insert_dok = $db -> prepare("INSERT INTO document_tbl(dokument_vlasnik,dokument_broj,vrsta_dokumenta,zakljucak, resenje_izvrsenja,datum,resenje_text,obrazlozenje,obtext,racunob) VALUES('$ses_val','$dok_broj', '$vrsta_pre','$obrazac','$izvrsenje','$datum','$res_text','$obrazlozenje','$ob_text','$racunob')");
    if($lica -> execute() AND $insert_dok -> execute() AND $lica1 -> execute()){


       $lica -> close();
       $lica1 -> close();
       $insert_dok -> close();


      echo '<script>new Messi(\'Dokument uspjesno dodat.\', {title: \'Obavjestenje\', titleClass: \'success\', buttons: [{id: 0, label: \'Close\', val: \'X\'}]});</script>';
    header('location:login.php');
      }else{
       echo '<script>new Messi(\'Dokument uspjesno dodat.\', {title: \'Obavjestenje\', titleClass: \'anim warning\', buttons: [{id: 0, label: \'Close\', val: \'X\'}]});</script>'; 
    }

}

1 个答案:

答案 0 :(得分:3)

输出内容后无法重定向。由于您输出JavaScript,因此您需要将重定向更改为由JavaScript完成。

echo '<script>new Messi(\'Dokument uspjesno dodat.\', {title: \'Obavjestenje\', titleClass: \'success\', buttons: [{id: 0, label: \'Close\', val: \'X\'}]});</script>';
header('location:login.php');
//^^^ This won't work.
// Try this instead. Redirects five seconds after the page is loaded.
echo 'window.setTimeout(function() {window.location = "login.php"}, 5000);';

就表单重新提交问题而言,您需要实现POST/REDIRECT/GET pattern。基本上,您需要使用303 HTTP标头进行重定向,以防止浏览器重新提交页面。处理完表单后,您可以使用类似的方法进行重定向:

header('Location: /login.php', true, 303);
exit;