#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    8
    Rep Power
    0
    I have a question/problem. I have a dropdown list with values and I am having trouble getting the value chosen into a variable so that I can use it in an if statement. Please help me.

    ------------------
    please contact me via my e-mail at kitkaty10@aol.com.
  2. #2
  3. No Profile Picture
    Robert_J_Sherman
    Guest
    Devshed Newbie (0 - 499 posts)
    Katherine,
    To obtain the value of an "selected" option in a form select menu, simply refer
    to the named fields "value".
    <select name="Options">
    <option value="XX">XX</option>
    <option value="YY">YY</option>
    </select>

    In PHP to reference the value of "Options", you simply use something like:
    if ($Options == "XX") {
    echo $Options;
    }
    If you just wanted to ensure an option was selected, you might do something like:
    if (!$Options) { //what to do since nothing was selected//
    }
    if (!$Option == "") or something along these lines.

    ------------------
    SnR Graphics,
    Low Cost Hosting and Web Development.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    8
    Rep Power
    0
    Hi, That didn't work. I am not sure what I am doing wrong. Here is some of my code. Let me know if you have any ideas.

    <form name="pick" ><select name="designers">
    <option value="1">Gianni Versace</option>
    <option value="2">Donna Haag</option>
    <option value="3">Cheri Milaney</option>
    <option value="4">Lee Yau</option>
    <option value="5">Ralph Lauren</option>
    <option value="6">Vera Wang</option>
    </select>
    </form>
    </td>
    </tr>
    <tr>
    <td>
    <?php
    if ($designers == "6") {
    include("verawang.inc");
    }
    else {
    echo "No interview has been done with this designer yet. Please check back.<br>";
    }
    ?>
    </td>
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    0
    Rep Power
    0
    Is the form and reference to the variable on the same page? I could be wrong, but I was under the impression that the code is parsed first, so the call to the variable isn't working because, technically, the variable isn't defined or set yet.

    The way I usually do it is having the form action lead to a seperate page, and doing any variable calculations once the data is submitted to it from the form.

    If I'm wrong, or if they already are on seperate pages, completely ignore me. =)

  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    8
    Rep Power
    0
    Hi, All of the code is on the same page. My whole idea here is to have it so that the person selects something from the dropdown box and then depending on what that item is determines what is displayed/included in the bottom half of the screen. Is my logic off and this can't be done?
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    No you can not do what you want with PHP without refreshing the page - so you have to add an action and method to your form -ie

    <form name="pick" method="post" action="<? echo $PHP_SELF;?>"> (the $PHP_SELF sends the form back to the same page)

    and then do your include.

    also to avoid lots of if/else statements - try something like this

    <select name="designers">
    <option value="versace">Gianni Versace</option>
    <option value="haag">Donna Haag</option>
    <option value="verawang">Vera Wang</option>
    etc etc - the value reflecting the name of the file to include..

    and then just
    <?
    $inc=$designers.".inc";
    include($inc);
    ?>

    but this will have to happen after the submit button has been pressed and the page is refreshed.... until that time $designers - has no value.


    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL
  12. #7
  13. No Profile Picture
    Robert_J_Sherman
    Guest
    Devshed Newbie (0 - 499 posts)
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Katherine Baxter:
    Hi, That didn't work. I am not sure what I am doing wrong. Here is some of my code. Let me know if you have any ideas.

    <form name="pick" ><select name="designers">
    <option value="1">Gianni Versace</option>
    <option value="2">Donna Haag</option>
    <option value="3">Cheri Milaney</option>
    <option value="4">Lee Yau</option>
    <option value="5">Ralph Lauren</option>
    <option value="6">Vera Wang</option>
    </select>
    </form>
    </td>
    </tr>
    <tr>
    <td>
    <?php
    if ($designers == "6") {
    include("verawang.inc");
    }
    else {
    echo "No interview has been done with this designer yet. Please check back.<br>";
    }
    ?>
    </td>
    [/quote]


    First the others are right, no output will be delivered by PHP until you "post" the form data to either $PHP_SELF or some other
    exterenal php file.

    So, if you want to "include" the file defined
    by $INC, your form will need to be submited
    with the appropriate selection made.

    Which means you'll have to complete the form tag with the "method" and "action" attributes, add a "submit" button.

    However, if you are just wanting to display a specific file based on a "designer" value, you could also use a "GET" statement like:

    myinterviews.php?designers=6

    When the link is clicked, for whatever page, the page then loads with the appropriate included file.

    No need for a "form" tag at all.

    Granted, there are a number of ways to work this, depending on just how much you have involved here.

    In short, you can "post" data back to the page, or you can "GET" the data by passing the information to your php script document via a standard anchor..

    Just depends on how you want to obtain or go about obtaining your data.




    ------------------
    SnR Graphics,
    Low Cost Hosting and Web Development.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    UK
    Posts
    9
    Rep Power
    0
    As much as I love PHP would it not be easier to implement this using JavaScript by hiding and showing layers dynamically depending on what is selected? As long as you don't need a DB lookup or anything.



    ------------------
    --
    Mark Ogden
    http://homepage.ntlworld.com/pogden
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    8
    Rep Power
    0
    Hi,

    Thank you all for your help. I added the method and the action and then added the submit button and it now works great. I appreciate all of your help!

Similar Threads

  1. include variables update
    By jnccneto in forum PHP Development
    Replies: 4
    Last Post: November 21st, 2003, 09:02 AM

IMN logo majestic logo threadwatch logo seochat tools logo