Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Else if not working correcting with simple form.

    I have a simple form with a drop down box. What I'm trying to do is echo different things depending on what is selected. The problem is that no matter which option is selected, the script echoes the first option.

    In other words, it seems to be returning the first 'if' as always true.


    The html is this...
    Code:
    <html>
    <body>
    
    <form action="myform.php" method="post">
    <p>Customer name: <input type="text" name="name" value="[FIRST]" /><br />
    </p>
    
    <p>Service requested.</p>
    	<select name="service"> 
    		<option id="entry">VIP Entry</option>
    		<option id="bottles">Bottle Service</option>
    	</select>
    	<select name="venue">
    		<option id="xs">XS</option>
    		<option id="tryst">Tryst</option>
    		<option id="haze">Haze</option>
    		<option id="other">other</option>
    	</select>
    <p><input type="submit" value="Send it!"></p>
    </form>
    
    </body>
    </html>
    The php is this...
    Code:
    <?php echo $_POST["name"] ?>
    
    <?php
    if ($_POST['service']="entry") {
      echo "ENTRY"; }
      
    elseif ($_POST['service']="bottles") {
      echo "BOTTLES"; }
      
    else {
      echo "NOTHING!"; }
    
    ?>

  • #2
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    PHP Code:
    <?php echo $_POST["name"]; ?>

    <?php
    if ($_POST['service']=="entry") {
      
        echo 
    "ENTRY"

    } elseif (
    $_POST['service']=="bottles") {

      echo 
    "BOTTLES"

    } else {
      
        echo 
    "NOTHING!"

    }

    ?>
    = is the assignment operator
    == is comparison
    === is same type and same value

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I knew it was something simple!

    is there any way for the form to post the 'id' of the option instead of the display text?

  • #4
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    use the value attribute.

    <option id="xs" value="this is the value">XS</option>

    id is the identifier to "identify" the element.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •