RainbowdashTM
RainbowdashTM

Reputation: 21

PHP Variables not Posting

So pretty much this is my code. It's not generating any sort of post. Is there some reason why this is occuring? It's rather annoying. And is getting on my nerves.

I've been trying to de-bug this for about 30 minutes now. And I've tried numerous things. Is it because of it not actually posting the values? Or is it just not reading them properly?

echo $_POST["sntype"];

And here is the form code:

$htmloutput1 = '<html>
<body>
<form name="sntype" action="site.php" method="post">
<select>
  <option value="1">Please Choose...</option>
  <option value="Elect">Electronics</option>
  <option value="Food">Food</option>
  <option value="Other">Other</option>
<input type="submit" value="Submit">

</form>
</select>

</body>
</html>';

echo $htmloutput1;
echo $_POST["sntype"];

Upvotes: 0

Views: 80

Answers (2)

Giacomo1968
Giacomo1968

Reputation: 26066

The </select> is placed outside of the <form> and the <input> is inside the <select> as a result.

<form name="sntype" action="site.php" method="post">
<select>
  <option value="1">Please Choose...</option>
  <option value="Elect">Electronics</option>
  <option value="Food">Food</option>
  <option value="Other">Other</option>
<input type="submit" value="Submit">

</form>
</select>

How about trying this instead:

<form name="sntype" action="site.php" method="post">
  <select>
    <option value="1">Please Choose...</option>
    <option value="Elect">Electronics</option>
    <option value="Food">Food</option>
    <option value="Other">Other</option>
  </select>
  <input type="submit" value="Submit">
</form>

EDIT Also, what do you expect to get from echo $_POST["sntype"];? That is simply the name of the form: sntype. That is not the value of the <select>. And <select> has no name. So why not set this:

<form name="sntype_form" action="site.php" method="post">
  <select name="sntype">
    <option value="1">Please Choose...</option>
    <option value="Elect">Electronics</option>
    <option value="Food">Food</option>
    <option value="Other">Other</option>
  </select>
  <input type="submit" value="Submit">
</form>

I changed the name of the <form> to be sntype_form and set <select> to now be <select name="sntype"> so the name is now sntype.

Upvotes: 2

Onimusha
Onimusha

Reputation: 3385

Ok so lots of problems with your code.

Try the following:

<?php

if(isset($_POST["sntype"])){ //if sntype was submited
    echo $_POST["sntype"];
}
else{ //otherwise display form
?>

<html>
<body>
<form action="" method="post">
    <select name="sntype">
      <option value="1">Please Choose...</option>
      <option value="Elect">Electronics</option>
      <option value="Food">Food</option>
      <option value="Other">Other</option>
    </select>

    <input type="submit" value="Submit">
</form>

</body>
</html>
<?php } ?>

Issues with your code

  • Ensure all fields are in the FORM tag
  • Each field you want to submit should have the name attribute
  • I'm not sure what you want to achieve with the code but the above should get you going

Upvotes: 1

Related Questions