Getting the value of a select element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anhkhoa168
    New Member
    • Jul 2007
    • 1

    Getting the value of a select element

    Hello everybody!
    I want to ask a question.
    If i want to get the value in the combox into array, how did i do?
    Thank you very much if i receive your reply soon!
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Originally posted by anhkhoa168
    Hello everybody!
    I want to ask a question.
    If i want to get the value in the combox into array, how did i do?
    Thank you very much if i receive your reply soon!

    Hi,

    Is the combobox on a form? If so then you could, on submission of the form, take the the value from the combobox and add it to the array:

    [CODE=html]
    <!-- Form -->
    <form id="testForm" action="logComb o.php" method="post">
    <select id="testSelect" >
    <option value = "1">Item 1</option>
    <option value = "2">Item 2</option>
    <option value = "3">Item 3</option>
    </select>
    <input id="submit" type="submit" value="Submit" title="Submit Form"/>
    </form>
    [/CODE]

    Then on the php page:
    [CODE=php]
    $laMyArray = newArray();

    if (isset($_POST['testSelect']))
    {
    $laMyArray[0] = $_POST['testSelect'];
    }
    [/CODE]

    I beleive this should work, but I must admit I have not tried it yet. I need to do something similar for a current project and this is the way I have set things up to work. The only thing I am not sure of is the use of 'testSelect', it may be that you need to do something to determine which of the options has selected set to "selected".

    I hope this is helpful
    nathj
    Last edited by nathj; Jul 17 '07, 11:04 AM. Reason: typo

    Comment

    • gregerly
      Recognized Expert New Member
      • Sep 2006
      • 192

      #3
      Originally posted by nathj
      Hi,

      Is the combobox on a form? If so then you could, on submission of the form, take the the value from the combobox and add it to the array:

      [CODE=html]
      <!-- Form -->
      <form id="testForm" action="logComb o.php" method="post">
      <select id="testSelect" >
      <option value = "1">Item 1</option>
      <option value = "2">Item 2</option>
      <option value = "3">Item 3</option>
      </select>
      <input id="submit" type="submit" value="Submit" title="Submit Form"/>
      </form>
      [/CODE]
      You will also need the name attribute for the select tag for your code to work properly. IE:

      [HTML]<select id='testSelect' name='testSelec t'>[/HTML]

      Then you can access the value in the PHP:

      [PHP]$mySelectBox = $_POST['testSelect'];[/PHP]

      Hope that helps.

      Greg

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Changed thread title to better describe the problem.

        Comment

        Working...