title: select dropdown option delete from sql
body: <p>I'm trying to add/remove drop-down options with Javascript. Currently everything works fine except when you select the option you want to delete from the dropdown. Click the delete button deletes the html, but not the sql entry.</p>
  
  <p>EDIT: I edited this to what works for me now, maybe save someone else time. CodeGodie's answer was ultimately correct, but I had to simplify the php and some issues with my config file were messing me up. This simply gives you a text input to add select options and a button to delete selected options. Still needs to be cleaned up and obviously the idea is for an authenticated admin to have this ability, not the free world. Thanks for the help!</p>
  
  <p>Here's my code:</p>
  
  <pre><code>  &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"&gt;&lt;/script&gt;
  &lt;?php include 'config.php'; ?&gt;
  &lt;script type="text/javascript"&gt;
  $(document).ready(function() {
  
      //##### send add record Ajax request to response.php #########
      $("#FormSubmit").click(function (e) {
              e.preventDefault();
              var txt=document.getElementById("contentText").value;  //inputID
  
              var newcontent = document.createElement('option'); //creates option
               newcontent.innerHTML = txt;
               document.getElementById("choice").appendChild(newcontent); //choice is select ID
  
              if($("#contentText").val()==='')
              $("#FormSubmit").hide(); //hide submit button
              var myData = 'content_txt='+ $("#contentText").val(); //build a post data structure
              jQuery.ajax({
              type: "POST", // HTTP method POST or GET
              url: "response.php", //Where to make Ajax calls
              dataType:"text", // Data type, HTML, json etc.
              data:myData, //Form variables
              success:function(response){
                  $("#contentText").val(''); //empty text field on successful
              }
              });
      });
  
      //##### Send delete Ajax request to response.php #########
       window.onload = function () {
      document.getElementById("btn1").onclick = function () {
          var myData = 'choice='+ $("#choice option:selected").val(); 
          jQuery.ajax({
          type: "POST", // HTTP method POST or GET
          url: "response.php", //Where to make Ajax calls
          dataType:"text", // Data type, HTML, json etc.
          data:myData, //Form variables
          });
         $("#choice option:selected").remove();
          }
      };
  });
  &lt;/script&gt;
  &lt;form&gt;
  &lt;select id="choice"&gt;
  &lt;?php
  //MySQL query
  $stmt = $db-&gt;query("SELECT id,content FROM add_delete_record");
  //get all records from add_delete_record table
  while($row = $stmt-&gt;fetch()){
  $content = $row['content'];
  
  echo '&lt;option&gt;' . $content . '&lt;/option&gt;' ;} ?&gt;
  &lt;/select&gt;
      &lt;input type="button" id="btn1" class="btn btn-default" value="Delete" /&gt;
      &lt;div class="form_style"&gt;
      &lt;input type="text" name="content_txt" id="contentText" cols="45" rows="5" placeholder="Enter some text"&gt;&lt;/textarea&gt;
      &lt;button id="FormSubmit"&gt;Add record&lt;/button&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>and the response.php..</p>
  
  <pre><code>&lt;?php
  //include db configuration file
  include 'config.php';
  if(isset($_POST["content_txt"]) &amp;&amp; strlen($_POST["content_txt"])&gt;0) 
  {   //check $_POST["content_txt"] is not empty
  
      //sanitize post value, PHP filter FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH Strip tags, encode special characters.
      $contentToSave = filter_var($_POST["content_txt"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); 
  
      // Insert sanitize string in record
      $insert_row = $db-&gt;query("INSERT INTO add_delete_record(content) VALUES('".$contentToSave."')");
  
  }
  if(isset($_POST["choice"])){
  
      $contentToDelete = $_POST["choice"]; 
  
      $delete_row = $db-&gt;query("DELETE FROM add_delete_record WHERE content='$contentToDelete'");
  
  }
  ?&gt;
  </code></pre>