SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with drop down list

    I am working on learning php so I am working on a small project. I have a form to enter a name and when you push the button, it puts the name in a list on the screen. Now I want to be able to select the name in a drop down list and push a button to delete the name from the list.

    Here is my drop down list

    Code:
    <form action="remove.php">
    <select name="dropname">
    <?php foreach ($name as $n): ?>
    <option><?php echo $n ?></option>
    <?php endforeach; ?>
    </select>
    <input type="submit" name="go" value="delete name"  />
    </form>
    and the code I am working on to delete the name

    Code:
    include('connection.php');
    
    if(isset($_POST['go']))
    {
    	foreach($name as $n):
       mysql_query("DELETE FROM mytable WHERE names = $_POST[dropname]");
    	error_reporting(E_ALL);
    	
    	endforeach;
    }
    if (!mysql_query($con))
      {
      die('Error: ' . mysql_error());
      }
    I keep getting an error saying query is not populated or something like that. I have changed the code many times. this is the latest version. which could be wrong. I am just a newb. Any help would be greatly apreciated.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    In your code which receives the form, there is no array called $name to loop over

    The value from the select box will be in $_POST, and the key will be the name of the select element

    $_POST['dropname']

    However you have not given your <option> tags any values, so it's not going to send the selected name.

    So amend your form:

    PHP Code:
    <?php foreach ($name as $n): ?>
    <option value="<?php echo $n?>"><?php echo $n ?></option>
    <?php endforeach; ?>
    And your processing code is:
    PHP Code:
    include('connection.php');

    if (isset(
    $_POST['go']))
    {
       
    $result mysql_query("DELETE FROM mytable WHERE names = '$_POST[dropname]'");
       if (!
    $result)) die("Error: " mysql_error());

    Also note that I added single quotes around the name in the SQL query, as all literal strings have to be surrounded by quotes in SQL.

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, i understand what you were saying about the values being in the post array, and I get that the value of the dropdown needed a value to send into post, but it still doesn't work. sometimes, it even inserts a blank row into the list, so i believe the connection is going. I don't get any errors, it just doesn't delete the name. My database only has two fields, ID, and names. Do I need to select using both fields?

  4. #4
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My bad, it's working now. I removed the method=post from the form, I just put it back and now it's working. Thanks so much for your help, I really appreciate it.


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
  •