-1

Whenever i try to use a form to post data to PHP, It says it cant find any variables, it seems no information is going through the post. For example i try to get the variable "color" which is a drop down list, but it says undefined. Here is my HtML

<form name="myform" action="index.php" method="post" >
                    <p>Body: <select id="body"name="body" onchange='changeLevel()'list="body">
                        <option value="Snail">Snail</option>
                        <option value="Ghost">Ghost</option>
                    </select></p>
                    
                    <p>Color: <select name="color" list="colors">
                        <option value="Red">Red</option>
                        <option value="Green">Green</option>
                        <option value="Blue">Blue</option>
                    </select></p>
                    
                    <p>Boots? <select name="boots"list="boots" >
                        <option>yes</option>
                        <option>no</option>
                    </select> </p>
                    <p id="mouthtext">Mouth: <select name="mouth" list="mouths" >
                        <option id="mouth1" value="Happy">Happy</option>
                        <option id="mouth2" value="Scared">Scared</option>
                        <option id="mouth3"value="Sad">Sad</option>
                    </select></p>
                    
                    <p id="eyestext">Eyes: <select name="eyes" list="eyes">
                        <option id="eyes1"value="Angry">Angry</option>
                        <option id="eyes2"value="Excited">Excited</option>
                        <option id="eyes3"value="Tired">Tired</option>
                        <option id="eyes4"value="Dead">Dead</option>
                        <option id="eyes5"value="PinPoint">Pin Point</option>
                    </select></p>
                    
                    <p id="tattootext">Tattoo: <select name="Tattoos"list="tattoos">
                        <option id="tattoo1"value="Fire">Racing Fire</option>
                        <option id="tattoo2"value="Mom">Mom</option>
                        <option id="tattoo3"value="Scales">Scaley</option>
                        <option id="tattoo4"value="Slimy">Slimy</option>
                        <option id="tattoo5"value="Snail">Snail</option>
                    </select></p>
                    <p>Name: 
                    <input type="text" name="name" size="20" maxlength="30" autofocus="autofocus" ></p>
                    <script type="text/javascript">
                    function changeLevel(){
                          if(document.getElementById("body").value == "Snail"){
                            document.getElementById("mouthtext").style.display  = "none";
                            document.getElementById("eyestext").style.display  = "none";
                            document.getElementById("tattootext").style.display  = "inline";
                          }
                          else{
                            document.getElementById("mouthtext").style.display  = "inline";
                            document.getElementById("eyestext").style.display  = "block";
                            document.getElementById("tattootext").style.display  = "none";
                          }
                    }
                    changeLevel()
                    </script>
                    <input type="submit" name="submit" value="Make my character!"/>
                </form>

AND I HAVE THIS PHP right below it which should simply print out the color chosen, but does not

<?
if (isset($_POST['myform'])){
        $color = $_POST['color'];
?><?= $color;?><?}
?>

When i load the page it says 'color' is undefined variable, even though i definitely have a name equal to 'color'. Why cant i load any posts?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • Because `$color` is only defined if it enters the IF block. The form name is not passed through POST, so it's not hitting the if block. Define the variable before, and check for `$_POST['submit']` instead. – aynber Mar 19 '21 at 17:43
  • @aynber No, don't suggest that too. Some browsers don't send the value of submit as a part of the form data. – Praveen Kumar Purushothaman Mar 19 '21 at 17:44
  • 1
    @PraveenKumarPurushothaman Really? That's a new one on me. Off to research I go. – aynber Mar 19 '21 at 17:45
  • @aynber Yes man, got a bad experience with that too. :( – Praveen Kumar Purushothaman Mar 19 '21 at 17:46
  • **Do not** use `submit` as a name for any field, some browsers don't send them. You can use any other word on the world, even just `form_submitted`. That's hard to debug and no one expects that. To check what actually is sent you can at the beginning of the script use `var_dump($_POST)`; – biesior Mar 19 '21 at 18:03
  • @aynber ^^^^^ Look at above comment by biesior... ***Do not** use `submit` as a name for any field, some browsers don't send them. You can use any other word on the world, even just `form_submitted`. That's hard to debug and no one expects that. To check what actually is sent you can at the beginning of the script use `var_dump($_POST)`;* – Praveen Kumar Purushothaman Mar 19 '21 at 19:41
  • @DanielGoldberg Welcome to Stack Overflow. Please see, [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) Also go through the [tour] so that you will be familiar with how to use this platform. – Praveen Kumar Purushothaman Mar 19 '21 at 19:41

1 Answers1

1

You should never use this:

isset($_POST['myform'])

Because you're not sending it. Try using:

count($_POST) > 0

This is a better check or check the request method. A possible solution could be:

<?php
  if (count($_POST) > 0) {
    $color = $_POST['color'];
    echo $color;
  }
?>

Or the better way is:

<?php
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $color = $_POST['color'];
    echo $color;
  }
?>

Also, don't use isset($_POST["submit"]) or similar things too. Some browsers don't send the value of submit as a part of the form data, in case, if you're using <button> or <input /> without value or name. In simple terms, it's unreliable.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252