-3

I have an HTML-form with 3 inputs of type text and one input of type submit, where a name of an animal is to be inserted into each textbox. The data should then be inserted into a PHP-array.

This is my HTML-form:

<form action="Test.php" method="post" name="myForm" id="myForm">
    <input type="text" placeholder="Enter animal one..." name="animal1" id="animal1">
    <input type="text" placeholder="Enter animal two..." name="animal2" id="animal2">
    <input type="text" placeholder="Enter animal three..." name="animal3" id="animal3">
    <input type="submit" value="Submit" name="send" id="send">
</form>

And this is my much experimental PHP-code:

<?php

if (isset ($_POST["send"])) {
    $farmAnimals = $_POST["animal1"];
    $farmAnimals = $_POST["animal2"];
    $farmAnimals = $_POST["animal3"];
}

// As a test, I tried to echo the saved data

echo $farmAnimals;

?>

This does not work, as the data doesn't magically turn into an array, unfortunately, which I thought it would.

I have also tried this:

<?php

if (isset ($_POST["send"])) {
    $farmAnimals = $_POST["animal1", "animal2", "animal3"];
}

    echo $farmAnimals;

?>

This gives me an error message. How can I insert this data into a PHP-array?

4 Answers4

-1

First initialize the $farmAnimals as array, then you can push elements

if (isset ($_POST["send"])) {
    $farmAnimals = array();
    $farmAnimals[] = $_POST["animal1"];
    $farmAnimals[] = $_POST["animal2"];
    $farmAnimals[] = $_POST["animal3"];

    print_r($farmAnimals);
}
shushu304
  • 1,506
  • 1
  • 7
  • 15
-1

Try this:

$string = ""; 
foreach($_POST as $value){ 
    $string .= $value . ","; 
} 

Or this:

$string = implode(',', $_POST);  
Fernando León
  • 516
  • 7
  • 15
-1

If you change the text field names to animal[] then you have an array of animal names when it is posted

<form action='Test.php' method='post'>
    <input type='text' placeholder='Enter animal one...' name='animal[]' />
    <input type='text' placeholder='Enter animal two...' name='animal[]' />
    <input type='text' placeholder='Enter animal three...' name='animal[]' />
    <input type='submit' value='Submit' name='send' id='send'>
</form>

You can then process that array in php easily for whatever end purpose you have - the output of which would be like this:

Array
(
    [animal] => Array
        (
            [0] => giraffe
            [1] => elephant
            [2] => rhinocerous
        )

    [send] => Submit
)

To access these animals as an array in their own right you could do:

$animals=array_values( $_POST['animal'] );
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
-1

Try naming your input fields like this:

<form action="Test.php" method="post" name="myForm" id="myForm">
    <input type="text" placeholder="Enter animal one..." name="animal[0]" id="animal1">
    <input type="text" placeholder="Enter animal two..." name="animal[1]" id="animal2">
    <input type="text" placeholder="Enter animal three..." name="animal[2]" id="animal3">
    <input type="submit" value="Submit" name="send" id="send">
</form>

This way you be able to access those values in your PHP code like this:

if(isset($_POST["send"])) {
    $farmAnimals = $_POST["animal"];
}

var_dump($farmAnimals);
// Array( [0] => Animal1, [1] => Animal2, [2] => Animal3 )
// Where Animal1,... are the values entered by the user.
// You can access them by $farmAnimals[x] where x is the desired index.

Tip: You can use empty() to check a variable for existance and if it has a "truthy" value (a value which evaluates to true). It will not throw an exception, if the variable is not defined at all. See PHP - empty() for details.

Futher reference about the superglobal $_POST array, showing this "array generating name" approach: https://secure.php.net/manual/en/reserved.variables.post.php#87650

Edit:

I just saw, that you actually overwrite your $farmAnimals variable each time. You should use the array(val1, val2, ...) with an arbitray amount of parameters to generate an numeric array.

If you prefer an associative array do this:

$myArray = array(
    key1 => value1,
    key2 => value2,
    ....
);

To append a value on an existing array at the next free index use this syntax:

// This array contains numbers 1 to 3 in fields 0 to 2
$filledArray = array(1,2,3); 
$filledArray[] = "next item";
// Now the array looks like this:
// [0 => 1, 1 => 2, 2 => 3, 3 => "next item"]
GxTruth
  • 509
  • 4
  • 9