PHP:从HTML表单添加输入到现有PHP数组

时间:2016-05-27 16:42:10

标签: php arrays forms

我想创建一个PHP类,它从HTML表单中获取输入的数据并将其添加到现有的php数组$profileArray中,然后可以将其包含在我的过滤器for和{{1}中循环。

我现有的硬编码数据按if进行过滤,具体取决于使用'Age'按钮在第一种表单中所做的选择。我的问题是,如何使用php类将输入添加到硬编码数据库radio,然后通过$profileArray将其包含在过滤器中。

HTML

Age

主PHP文件:

<html>

<body>
<p>Please choose at least one quality you would like in your new cat: </p>
        <form action="profileFilter.php" method="POST">
    <p>Age: <br>
            <input type="radio" name="ageChoice" value="1">1 <br>
            <input type="radio" name="ageChoice" value="2">2 <br>
            <input type="radio" name="ageChoice" value="3">3<br>
            <input type="submit" name="submitButton">   
    </form>

<p>Submit your own cat for adoption: </p>
        <form action="profileData.class.php" method="GET">
    <p>Name: <input type="text" name="inputData[Name]" placeholder="What name?"></p>
    <p>Age: <input type="text" name="inputData[Age]" placeholder="What age?"></p>
    <p>Colour: <input type="text" name="inputData[Colour]" placeholder="What Colour?"></p>
    <input type="submit" name="catQualitiesBtn">   
    </form>

</body></html>

PHP类文件:

    <?php
//Acting as hardcoded database
$profileArray = array( 
array(  'Name' => "Toby",
        'Age' => 3, 
        'Colour' => "Ginger",
        ),

array(  'Name' => "Cassie",
        'Age' => 3, 
        'Colour' => "Tabby",
        ),

array(  'Name' => "Lucy",
        'Age' => 1, 
        'Colour' => "Black",
        ),

array(  'Name' => "Rufus",
        'Age' => 2, 
        'Colour' => "White",
        ),
);

if (isset ($_POST['ageChoice']) && !empty($_POST['ageChoice'])) {
    $passedAgeChoice = $_POST['ageChoice'];
    echo $passedAgeChoice."<br>";
    for ($i=0; $i < 7 ; $i++) { 

           if ($profileArray[$i]['Age'] == $passedAgeChoice) {
            echo " Name: ".$profileArray[$i]['Name']."<br>";
            echo " Age: ".$profileArray[$i]['Age']."<br>";
            echo " Colour: ".$profileArray[$i]['Colour']."<br>";
            echo " Gender: ".$profileArray[$i]['Gender']."<br>";
            echo " Personality: ".$profileArray[$i]['Personality']."<br>"."<br>";}
        }
}
else {
    $passedAgeChoice = NULL;
    echo "***YOU HAVE NOT ENTERED AN AGE***"." <br>";
} 
}


///This is where I am having trouble///
$inputtedData = $_GET['inputData[]']; 

$inputDataArrayObject = new InputDataArray;
$inputDataArrayObject->setFormDataInput($inputtedData);
$inputDataArrayObjectArray= $inputDataArrayObject->getFormDataInput();
 echo "The data you inputted is ". $inputtedData."<br>";
       ?>

0 个答案:

没有答案