Vivek
Vivek

Reputation: 663

How to send PHP array by html form by POST type

I am trying to send PHP array by html form by POST. The code which I am using is below:

<?php
    $content = array('111', '222' );
?>

    <html>
    <head>
    <title>PAGE TITLE</title>
    </head>
        <body>
            <form name="downloadexcel" action="downloadexcel.php" method="post">
                <input type="text" name="data" value="<?php echo $content; ?>"/>
                <a href="javascript: submitform()">Download</a>
            </form>
            <script type="text/javascript">
                function submitform()
                {
                  document.downloadexcel.submit();
                }
            </script>
        </body>
    </html>

How can I send an PHP array by html form?

Any web link or source code would be appreciated.

Upvotes: 0

Views: 11003

Answers (3)

Paul T. Rawkeen
Paul T. Rawkeen

Reputation: 4114

You can't send array like this. It will return Array. You can do it this way

<form name="downloadexcel" action="downloadexcel.php" method="post">

<?php foreach ($content as $item): ?>
    <input type="text" name="data[]" value="<?php echo $item; ?>"/>
<?php endforeach ?>

<a href="javascript: submitform()">Download</a>
</form>

This variant is the most elegant to use in templates or anywhere in the code. HTML will be easily validated by IDE and code assist will be also available.

Upvotes: 2

Ajay Kadyan
Ajay Kadyan

Reputation: 1079

<?php
$content = array('111', '222' );
?>

<html>
<head>
<title>PAGE TITLE</title>
</head>
    <body>
        <form name="downloadexcel" action="downloadexcel.php" method="post">
            <?php foreach($content as $c) { ?>
               <input type="text" name="data[]" value="<?php echo $c; ?>"/>
            <?php }  ?>
            <a href="javascript: submitform()">Download</a>
        </form>
        <script type="text/javascript">
            function submitform()
            {
              document.downloadexcel.submit();
            }
        </script>
    </body>
</html>

Upvotes: 2

JK.
JK.

Reputation: 5136

Here's is an simple example:

$content = array('111', '222');

foreach($content as $c)
{
    echo "<input name='arr[]' value='{$c}'>";
}

You could alternatively use serialize and unserialize to send the values.

Upvotes: 0

Related Questions