0

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
5
  • it's a typo $_POST('dogs'); should be square braces to access values in POST Commented Oct 24, 2022 at 4:25
  • @Kevin I changed it to ` $cats = $_POST('dogs[]');` and get the same result Commented Oct 24, 2022 at 5:22
  • should be $_POST['dogs'] Commented Oct 24, 2022 at 5:27
  • @Cositanto I tried $_POST['dogs'] - it's the original code in my post Commented Oct 24, 2022 at 5:29
  • 1
    your code is $_POST('dogs'), not $_POST['dogs'] Commented Oct 24, 2022 at 5:30

1 Answer 1

1

If you want to send the $dogs in your form, you can try:

<form action="tester.php" method="post">
<input type="text" name="text">
<?php
    foreach ($dogs as $dog) {
?>
    <input name="dogs[]" value="<?php echo $dog; ?>" style="display: none">
<?php
    }
?>
<input type="submit"/>
Sign up to request clarification or add additional context in comments.

2 Comments

What if $dogs is an array of arrays? A 2D array?
Maybe json is easier, like <input name="dogs" value='<?php echo json_encode($dogs); ?>' >, and use json_decode($_POST['dogs']) to receive it after form post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.