How to SEND dropdown list value to FORM with method POST?

Discussion in 'PHP' started by anupviews, Oct 8, 2010.

  1. #1
    <?php $query= "SELECT date, id FROM Puspa ORDER BY id ASC";
    $result = $db_connect->query($query);
    ?>
    <form action="output.php" method="post">
    <select name='p'>
    <?php
    while (list($date, $id) = $result->fetch_row()) {
    echo "<option value=\"$id\"> $id : $date</option>";
    }

    ?>
    </select>
    <input type="submit" value="submit"/><br />
    </form>

    I've dynamically listed my result in dropdown list as the code above.

    I would like to know the shortcut and secure way to submit the SELECTED value using POST method.

    How to do this?
     
    Last edited by a moderator: Oct 8, 2010
    anupviews, Oct 8, 2010 IP
  2. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #2
    just pull the data after submission as

    if($_POST['p']){
    $item = $_POST['p'];
    }

    Then you can sanitize it after you grab the submitted data.
     
    lowridertj, Oct 8, 2010 IP
  3. anupviews

    anupviews Member

    Messages:
    795
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    35
    #3
    Are you sure about it?
    The HTML code for drop down list will look like this:

    <select name='p'>
    <option value='1'>Value 1</option>
    <option value='2'>Value 2</option>
    <option value='3'>Value 3</option>
    </select>

    how will it pull the value? I know how to pull the value. But I wanted to get for example, Value 2 in $_POST['p']....
     
    anupviews, Oct 8, 2010 IP
  4. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #4
    lowridertj is correct. The post value "p" is populated with what is selected in the dropdown.

    Also, don't forget to use "==" in your while loop:
    
    while (list($date, $id) == $result->fetch_row())
    
    HTML:
     
    Thorlax402, Oct 8, 2010 IP
  5. anupviews

    anupviews Member

    Messages:
    795
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    35
    #5
    Can i know what's that == is for? I don't think it's right place to use ==!
     
    anupviews, Oct 8, 2010 IP
  6. anupviews

    anupviews Member

    Messages:
    795
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    35
    #6
    @lowridertj thanx it worked.
     
    anupviews, Oct 8, 2010 IP
  7. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #7
    no problem glad it worked out for you.
     
    lowridertj, Oct 8, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    danx10, Oct 8, 2010 IP