Reputation: 663
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
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
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
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