0

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.

2
  • 1
    It's not really clear what you try to achieve but the best way to send arrays is serialization (I think better practice is to use json_encode/json_decode). Commented Jul 27, 2012 at 8:39
  • Why some one put negative marking of this question? Can any body tell me please. Commented Jul 27, 2012 at 8:50

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2
<?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>

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.