SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form data in dropdown menu

    Hi guys trying to store the session in a dropdown menu when a user clicks back on form

    heres my php

    PHP Code:
      $category $_POST['category'];

    $_SESSION['category'] = $category

    PHP Code:
    <select name="catergory" value="<? htmlspecialchars($_POST['category']); ?>">

    but it isnt working

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    selects themselves dont have a value attribute. That is what the <option> tags are for.

    PHP Code:
    <select name="category">
        <option value="<?php echo htmlspecialchars($_POST['category']); ?>" selected><?php echo htmlspecialchars($_POST['category']); ?></option>
    is the basic way but you might want to put an if condition in there
    PHP Code:
    <?php if(isset($_POST['category'])) { ?>
    <option value="<?php echo htmlspecialchars($_POST['category']); ?>" selected><?php echo htmlspecialchars($_POST['category']); ?></option>
    <?php ?>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how would i integrate in this

    PHP Code:
    <select name="category" value="<? htmlspecialchars($_POST['category']); ?>">
        <option value="">------------------------- Please select a category -------------------------- </option>
        <option value="AeroSpace"> AeroSpace </option>
        <option value="Agriculture, Fishing, Forestry"> Agriculture, Fishing, Forestry </option>
        <option value="Banking, Insurance, Finance"> Banking, Insurance, Finance </option>
        <option value="Catering & Hospitality"> Catering & Hospitality </option>
        <option value="Construction"> Construction </option>
        <option value="Customer services, Call centres"> Customer services, Call centres </option>
        <option value="Catering & Hospitality"> Catering & Hospitality </option>
        <option value="Education"> Education </option>
        <option value="Electronics"> Electronics </option>
        <option value="Engineering, Manufacturing, Utilities"> Engineering, Manufacturing, Utilities </option>
        <option value="Graduate"> Graduate </option>
        <option value="Health, Nursing"> Health, Nursing </option>
        <option value="Human resources"> Human resources </option>
        <option value="IT & Internet"> IT & Internet </option>
        <option value="Legal"> Legal </option>
        <option value="Management consultancy"> Management consultancy </option>
        <option value="Marketing, Advertising, PR"> Marketing, Advertising, PR </option>
        <option value="Media, New media, Creative"> Media, New media, Creative </option>
        <option value="Not for profit, Charities"> Not for profit, Charities </option>
        <option value="Property"> Property </option>
        <option value="Public sector & Services"> Public sector & Services </option>
        <option value="Recruitment sales"> Recruitment sales </option>
        <option value="Retail, Wholesale"> Retail, Wholesale </option>
        <option value="Sales"> Sales </option>
        <option value="AeroSpace"> AeroSpace </option>
        <option value="Agriculture, Fishing, Forestry"> Agriculture, Fishing, Forestry </option>
        <option value="Banking, Insurance, Finance"> Banking, Insurance, Finance </option>
        <option value="AeroSpace"> AeroSpace </option>

  4. #4
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First, I think that data should definitely be something to come from the database; I mean all those options in the select box. But then again I hate hardcoding anything.

    But what you need to do (another reason I recommend the database for the options) is go through and compare each option value with the value of whatever was previously selected. If it matches then added selected="selected" to the option tag.

  5. #5
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how can u put both the option value and also echo the value?

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    if they are all hardcoded like that then:
    PHP Code:
    <select name="category">
    <?php if(isset($_POST['category'])) { ?> 
    <option value="<?php echo htmlspecialchars($_POST['category']); ?>" selected><?php echo htmlspecialchars($_POST['category']); ?></option> 
    <?php ?> 
        <option value="">------------------------- Please select a category -------------------------- </option> 
        <option value="AeroSpace"> AeroSpace </option> 
        <option value="Agriculture, Fishing, Forestry"> Agriculture, Fishing, Forestry </option> 
        <option value="Banking, Insurance, Finance"> Banking, Insurance, Finance </option> 
        <option value="Catering & Hospitality"> Catering & Hospitality </option> 
        <option value="Construction"> Construction </option> 
        <option value="Customer services, Call centres"> Customer services, Call centres </option> 
        <option value="Catering & Hospitality"> Catering & Hospitality </option> 
        <option value="Education"> Education </option> 
        <option value="Electronics"> Electronics </option> 
        <option value="Engineering, Manufacturing, Utilities"> Engineering, Manufacturing, Utilities </option> 
        <option value="Graduate"> Graduate </option> 
        <option value="Health, Nursing"> Health, Nursing </option> 
        <option value="Human resources"> Human resources </option> 
        <option value="IT & Internet"> IT & Internet </option> 
        <option value="Legal"> Legal </option> 
        <option value="Management consultancy"> Management consultancy </option> 
        <option value="Marketing, Advertising, PR"> Marketing, Advertising, PR </option> 
        <option value="Media, New media, Creative"> Media, New media, Creative </option> 
        <option value="Not for profit, Charities"> Not for profit, Charities </option> 
        <option value="Property"> Property </option> 
        <option value="Public sector & Services"> Public sector & Services </option> 
        <option value="Recruitment sales"> Recruitment sales </option> 
        <option value="Retail, Wholesale"> Retail, Wholesale </option> 
        <option value="Sales"> Sales </option> 
        <option value="AeroSpace"> AeroSpace </option> 
        <option value="Agriculture, Fishing, Forestry"> Agriculture, Fishing, Forestry </option> 
        <option value="Banking, Insurance, Finance"> Banking, Insurance, Finance </option> 
        <option value="AeroSpace"> AeroSpace </option>
    which would duplicate the entry. What devbanana suggests is that if the select options are dynamically generated then you would loop through the results and compare it to the POST'ed value. thus eliminating the duplicate
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and make it a whole lot easier besides, because you're going to have to have a lot of conditional statements; the exact number according to the number of options, while if you just have it from the database, you just have to do that condition once.


Bookmarks

Posting Permissions

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