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.
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.
Share Improve this question asked Jul 27, 2012 at 8:36 VivekVivek 6632 gold badges13 silver badges40 bronze badges 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
). – Leri Commented Jul 27, 2012 at 8:39 - Why some one put negative marking of this question? Can any body tell me please. – Vivek Commented Jul 27, 2012 at 8:50
3 Answers
Reset to default 2You 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.
<?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>
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.