Results 1 to 4 of 4

Thread: Passing values from drop down menu and checkbox

  1. #1
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Passing values from drop down menu and checkbox

    Hey,guys! I have a drop down menu and checkbox,how could I passed the selected value to database by POST in INSERT statement?Thanks...

    Code:
    <select name="category" title="category">
    <option selected>U</option>
    <option>18SG</option>
    <option>18SX</option>
    <option>18PA</option>
    <option>18PL</option>
    </select>
    Code:
    <input type="checkbox" name="CHK1" value="checkbox">12:00pm 
    <input type="checkbox" name="CHK2" value="checkbox">1:00pm 
    <input type="checkbox" name="CHK3" value="checkbox">2:00pm

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    with the dropdown you just need to assign a value to the option.
    Code:
    <select name="category" title="category">
    <option value="U" selected="selected">U</option>
    <option value="18SG">18SG</option>
    <option value="18SX">18SX</option>
    <option value="18PA">18PA</option>
    <option value="18PL">18PL</option>
    </select>
    the value is the field that you need to change, not the name.
    Code:
    <input type="checkbox" name="time[]" value="12:00">12:00pm 
    <input type="checkbox" name="time[]" value="13:00">1:00pm 
    <input type="checkbox" name="time[]" value="14:00">2:00pm
    also its better if you give the checkbox a meaningful name so that you / anyone else that may work on the script in the future will know what that is supposed to be.


    that would get it to the server-side, then you could edit it how you needed to on the back-end

    PHP Code:
    <?php
    $category 
    $_POST'category'];
    $time $_POST['time']; // this contains an array of each of the checkboxes selected

    _connect_to_database_
    $insert 
    "INSERT INTO tbl(fields) VALUES(values)";
    if( !
    mysql_query($insert) ) {
         echo 
    "error inserting data";
    }
    else {
         
    doSomething();
    }

    ?>
    Last edited by boogyman; 10-27-2007 at 02:12 PM. Reason: added the php backend example to finisht the question

  3. #3
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank for replying, is it neccessary for drop down menu to have selected="selected" ?

    How about if INSERT statement mix with $_POST and $variable?
    I having error on my INSERT statement.

    PHP Code:
     $category $_POST['category'];
     
    $time $_POST['time'];
     
    $insert mysql_query("INSERT INTO movie(name,director,casting,synopsis,screeningTime,classification) VALUES ('" $_POST['title'] . "','" $_POST['director'] . "', 
     '" 
    .$_POST['casting'] . "','" $_POST['synopsis'] ."' ,'" $time "',
    '" 
    $category ."')") or die(mysql_error()); 

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    the ="selected" is proper coding technique, but i dont know of any browser that will not allow it.
    same with checked="checked" on radio/checkbox

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
  •