I have a pre-defined $dogs array. I'd like to send it with an HTML form:
<?php
$dogs = array("A", "B", "C");
?>
<form action="tester.php" method="post">
<input type="text" name="text">
<input name="dogs[]" style="display: none">
<input type="submit"/>
</form>
<?php
$textValue = '';
if (sizeof($_POST) != 0) {
try {
$textValue = $_POST['text'];
print_r($dogs);
print_r('after...');
$cats = $_POST('dogs');
print_r($cats);
} catch (Exception $exception) {
print_r($exception);
}
}
?>
<div>Value: <?php echo $textValue ?></div>
The form submits to the same page.
I get the output:
Array ( [0] => A [1] => B [2] => C ) after...
If I comment out the last two lines in the try statement:
$textValue = $_POST['text'];
print_r($dogs);
print_r('after...');
// $cats = $_POST('dogs');
// print_r($cats);
I get:
Array ( [0] => A [1] => B [2] => C ) after...
Value: myvalue
I'd like to get output:
Array ( [0] => A [1] => B [2] => C ) after...Array ( [0] => A [1] => B [2] => C )
Value: myvalue
How should I change the code to get it?
EDIT:
I changed the code to
<?php
$dogs = array("A", "B", "C");
?>
<form action="tester.php" method="post">
<input type="text" name="text">
<input name="dogs[]" style="display: none">
<input type="submit"/>
</form>
<?php
$textValue = '';
if (sizeof($_POST) != 0) {
try {
$textValue = $_POST['text'];
print_r($dogs);
print_r('after...');
$cats = $_POST['dogs'];
print_r($cats);
} catch (Exception $exception) {
print_r($exception);
}
}
?>
<div>Value: <?php echo $textValue ?></div>
now I get this output:
Array ( [0] => A [1] => B [2] => C ) after...Array ( [0] => )
Value: myvalue
$_POST('dogs');should be square braces to access values in POST$_POST['dogs']$_POST['dogs']- it's the original code in my post$_POST('dogs'), not$_POST['dogs']