SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast stefanb's Avatar
    Join Date
    Nov 2002
    Location
    Holland
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    adding data from prompt to database

    I use the function below to add a new option to my dropdown menu, if I submit the value, the value is lost and not saved in the database.

    Is there a way to do this? I am using PHP.

    THanx

    This is my function:
    Code:
    function addnewitem()
     {
     newitem = prompt("Please type in the new item!");
     sel_length = document.first.cars.length;
     if (newitem != '')
      {
      document.first.cars.options[sel_length] = new Option(newitem,'','',true);
      }
     }
    Anytime, Anyplace, Anyhow

  2. #2
    SitePoint Evangelist azizur_rahman's Avatar
    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     function addnewitem()
     {
     newitem = prompt("Please type in the new item!" );
     sel_length = document.first.cars.length; if (newitem != '')
     {
     document.first.cars.options[sel_length] = new Option(newitem,'','',true);
    
    // Do a database insert here, Most likely use a iframe so you dont need to refresh this/current page
     }
     } 
    Azizur Rahman
    Web Application Developer

  3. #3
    SitePoint Enthusiast stefanb's Avatar
    Join Date
    Nov 2002
    Location
    Holland
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll Try

    Thanx
    Anytime, Anyplace, Anyhow

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first argument to the Option() constructor is the text displayed in the select menu; the second is the value.
    Code:
    document.first.cars.options[sel_length] = new Option(newitem,newitem,'',true);

  5. #5
    SitePoint Enthusiast stefanb's Avatar
    Join Date
    Nov 2002
    Location
    Holland
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked right away.

    Do you know what the third does?

    Problem Solved, Thanks
    Anytime, Anyplace, Anyhow

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It sets 'defaultSelected', as:

    <option selected="selected">blah</option>

    cheers


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
  •