Gabe Tucker
Gabe Tucker

Reputation: 59

PHP: $_POST cannot retrieve existing inputs

I'm attempting to make a button that, based on the selected form/inputs on the page, will bring you to a page called "typeDefine.php?openness=3?conscientiousness=2?extroversion=1?agreeableness=2?neuroticism=1"(the numbers varying based on the selected inputs). However, $selectedNum- the variable that would ideally be containing the $_POST for each input- is returning an error immediately once the page is loaded, saying:

Undefined index

<?php
                
    $typeWords = array("openness", "conscientiousness", "extroversion", "agreeableness", "neuroticism");
    $typeLetters = array("o", "c", "e", "a", "n");
    $typePath = "";
    $correspondingLetters = array("I", "II", "III");

    $isFirst = true;
                
    foreach($typeWords as $typeWord)
    {
        $selectedNum = $_POST[$typeWord];//error here!!!
                    
        if(isset($selectedNum))//if got $typeWord in a form
        {
            $separationChar;
                        
            if($isFirst)
            {
                $separationChar = "?";
                
                $isFirst = false;
            }
            else
            {
                $separationChar = "&";
            }

            $typePath = $typePath . $separationChar . $typeWord . "=" . $selectedNum;//e.g. $typePath = "?openness=3?conscientiousness=2?extroversion=1?agreeableness=2?neuroticism=1" for $_GET method after arriving on next page
        }
    }
                
    echo '<a href = "typeDefine.php' . $typePath . '" class = "button" style = "font-size: 400%; padding: 3.85rem 0;">search for type</a>
                
    <div>';
                    
        foreach($typeWords as $typeWord)
        {
            $typeLetter = substr($typeWord, 0, 1);
                        
            echo '<form method = "post" class = "column">';
                        
                for($i = 1; $i <= 3; $i++)
                {
                    echo '<input type = "radio" name = "' . $typeWord . '" id = "' . $typeLetter . $i . '"><label for = "' . $typeLetter . $i . '">' . $correspondingLetters[$i - 1] . '</label>';//sets each input name to $typeWord for $_POST above
                }
                            
                echo '<li class = "textHighlight">' . $typeWord . '</li>
                            
            </form>';
        }
                    
    echo '</div>';
            
?>

What can I do to fix this error, in turn filling $typePath and making the script correctly bring you to the desired url upon the button's click?

Here is a screenshot of the page for the sake of comprehension

Thanks in advance!

Upvotes: 1

Views: 42

Answers (2)

Barmar
Barmar

Reputation: 781141

You should perform the isset() test on the $_POST element, not the variable that you set from it.

    foreach ($typeWords as $typeWord)
    {
        if (isset($_POST[$typeWord])) {
            $selectedNum = $_POST[$typeWord];
            $typePath = $typePath . "?" . $typeWord . "=" . $selectedNum;
        }
    }

Note that multiple parameters need to be separated with &, ? should only be used at the beginning. There's a built-in function that will create a query string from an array, you can use that:

    $typeArray = [];
    foreach ($typeWords as $typeWord)
    {
        if (isset($_POST[$typeWord])) {
            $selectedNum = $_POST[$typeWord];
            $typeArray[$typeWord] = $selectedNum;
        }
    }
    $typePath = $typePath . "?" . http_build_query($typeArray);

You can also replace the loop with:

$typeArray = array_intersect_key($_POST, array_flip($typeWords));

Upvotes: 1

Tyr
Tyr

Reputation: 2810

You execute your code immidiately without a present $_POST array.

You have to wrap your post related code with an if statement like this:

if ($_POST) {
    $selectedNum = $_POST[$typeWord];
}

Upvotes: 0

Related Questions