且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过POST类型通过html表单发送PHP数组

更新时间:2022-12-06 16:34:41

你不能像这样发送数组。它将返回 Array 。你可以这样做

 < form name =downloadexcelaction =downloadexcel.phpmethod =post &GT; 

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

< a href =javascript:submitform()>下载< / a>
< / form>

这个变体是最适合在模板或代码中使用的。 HTML 将很容易通过 IDE验证$ c>和代码助手会也可用。


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.

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.