Mario.Rdz
Mario.Rdz

Reputation: 13

Value from Select tag shows blank on PHP variable

I've got this form up in a site. I can't get the value from the select tag to get collected by a variable on the php. No matter what option gets selected, it shows blank in the PHP.

Every other input value gets accurately collected by it's php variable.

I believe i checked enough for typos, unclosed tags or missing characters. Would it be some sort of rule about < select > tags that i'm not aware of?

The HTML

<form method="post" action="contact.php" name="contactform" id="contactform" autocomplete="off">
    <fieldset>
        <label id="sendtolabel"><span>Send To:</span></label>
        <select name="select" id="select">
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
            <option value="4">Option 4</option> 
        </select>
        <label for="name" accesskey="U"><span class="required">Name</span></label>
        <input name="name" type="text" id="name" size="30" title="name" />
        <label for="email" accesskey="E"><span class="required">Email</span></label>
        <input name="email" type="text" id="email" size="30" title="email" />
        <label for="comments" accesskey="C"><span class="required">Comments</span></label>
        <textarea name="comments" cols="40" rows="3" id="comments" title="comments"></textarea>
        <input type="submit" class="submit" id="submit" value="Submit" />
     </fieldset>
  </form>

The PHP

$select = $_POST['select']; 
$name = $_POST['name']; 
$email = $_POST['email']; 
$comments = $_POST['comments']

Upvotes: 0

Views: 2263

Answers (3)

Satyam Saxena
Satyam Saxena

Reputation: 571

Working fine for me.

Just try in this way. Put this code on top of contact.php to test whether all values are retrieving properly or not.

<?php
    if ( $_POST ) {
       echo '<pre>';
       print_r( $_POST );
       die;
    }
?>

See the out put below: http://d.pr/i/dgN3

Upvotes: 0

ErickBest
ErickBest

Reputation: 4692

It's Working Here... You can try this:

<?php
   if ( isset( $_POST['select'] ) ) {
       var_dump( $_POST['select'] );
       $select = $_POST['select']; 
       $name = $_POST['name']; 
       $email = $_POST['email']; 
       $comments = $_POST['comments'];
       echo "THE SELECT VAR's Value is: __".$select;
   }
?>
<form method="post" action="" name="contactform" id="contactform" autocomplete="off">
   <fieldset>
        <label id="sendtolabel"><span>Send To:</span></label>
        <select name="select" id="select">
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
            <option value="4">Option 4</option> 
        </select>
        <label for="name" accesskey="U"><span class="required">Name</span></label>
        <input name="name" type="text" id="name" size="30" title="name" />
        <label for="email" accesskey="E"><span class="required">Email</span></label>
        <input name="email" type="text" id="email" size="30" title="email" />
        <label for="comments" accesskey="C"><span class="required">Comments</span></label>
        <textarea name="comments" cols="40" rows="3" id="comments" title="comments"></textarea>
        <input type="submit" class="submit" id="submit" value="Submit" />
     </fieldset>
   </form>

NOTE: You can Test the code above in a phpfiddle.

1) Go to http://phpfiddle.org/
2) Paste the Code in the Editor then press Run[F9]

Click here to test the Code in fiddle: http://phpfiddle.org/main/code/tuc-6jm

Upvotes: 3

Chico3001
Chico3001

Reputation: 1963

When i have a problem like that, i just print all the $_POST global to see what has been returned

print_r($_POST);

And nope.. your code is correct

<html>
    <head></head>
    <body>
       <form method="post" action="test.php" name="contactform" id="contactform" autocomplete="off">
          <fieldset>
          <label id="sendtolabel"><span>Send To:</span></label>
          <select name="select" id="select">
              <option value="1">Option 1</option>
              <option value="2">Option 2</option>
              <option value="3">Option 3</option>
              <option value="4">Option 4</option> 
          </select>
          <label for="name" accesskey="U"><span class="required">Name</span></label>
          <input name="name" type="text" id="name" size="30" title="name" />
          <label for="email" accesskey="E"><span class="required">Email</span></label>
          <input name="email" type="text" id="email" size="30" title="email" />
          <label for="comments" accesskey="C"><span class="required">Comments</span></label>
          <textarea name="comments" cols="40" rows="3" id="comments" title="comments"></textarea>
          <input type="submit" class="submit" id="submit" value="Submit" />
         </fieldset>
     </form>
<?php 

   print_r( $_POST );
   echo $_POST['select'];
   $select = $_POST['select']; 
   $name = $_POST['name']; 
   $email = $_POST['email']; 
   $comments = $_POST['comments'];
   echo $select;
?>
    </body>
</html>

select is returning what it has to return... (1, 2, 3, 4)

Upvotes: 0

Related Questions