Redirect Page and Send Some Post Data

7:28 PM | , , , , , , , ,

Iseng-iseng bikin fungsi untuk redirect form sekaligus mengirimkan data dengan method POST tanpa menggunakan ajax. Fungsi ini gunanya untuk mengirimkan data sekaligus redirect ke halaman tertentu tetapi variabel yg dikirimkan berbeda-beda pada beberapa kasus.

function redirect_post($url, array $data) {
?>
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
       <script type="text/javascript">
           function closethisasap (){
               document.forms["redirectpost"].submit();
           }
       </script>
       <body onload="closethisasap();">
           <form name="redirectpost" method="post" action="<? echo $url; ?>" >
               <?
               if (!is_null($data)) {
                   foreach ($data as $k => $v) {
                       echo '<input type="hidden" name="'.$k.'" value="'.$v.'"> ';
                   }
               }
               ?>
           </form>
       </body>
   </head>
</html>
<?
exit();
}

Cara pemanggilan fungsinya:

$data['varname1'] = "value1";
$data['varname2'] = "value2";
$url = "Some location";
redirect_post($url, $data);

0 comments:

Post a Comment

Please leave a comment