Saturday, June 22, 2013

PHP - Redirect and send data via POST

It is a simple way to do that.
Suppose you are in from.php, just call the function like:
<?php
actionPost("www.pritom.com/to.php", array(
    "id" => 1,
    "name" => "Pritom K Mondal";
)); 
?>
And suppose in to.php, write the following code:
<?php
print_r($_POST);
?>

That will output:

<?php
Array
(
    [id] => 1
    [name] => Pritom K Mondal
)
?>


<?php
function actionPost($action, $data = array()) 
{
    ?><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="<?php echo $action; ?>" >
                    <?php
                    if (!is_null($data)) {
                        foreach ($data as $k => $v) {
                            ?><input type="hidden" name="<?php echo $k; ?>" value="<?php echo $v; ?>" /><?php
                        }
                    }
                    ?>
                </form>
            </body>
        </head>
    </html>
    <?php exit();
}
?>

1 comment:

  1. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
    PHP Database Programming in India

    ReplyDelete