简体   繁体   中英

Selected option value in <select> does not $_POST with PHP and Ajax

I am using two dropdowns. One of them (the second one) uses AJAX for selecting data from the database and displaying partial records on the page. I was able to post other input text(machine_no) but not the selected values in dropdowns.

HTML part

<form action="page.php" name="form" method="post">
   <select name="material_type" form="form" required>
         <option disabled selected>(Select Type)</option>
         <option value="1">1</option>
         <option value="2">2</option>
   </select>

   <select name="part_no" id="part_no" form="form" required>
         <option disabled selected>(Select Part)</option>
         <option value="1">1</option>
         <option value="2">2</option>
   </select>
  <div class="lot-no-list" id="lot-no-list"></div>

  <div class="col-sm-4">
       <input type="text" name="machine_no" id="machine_no" maxlength="15" required>
  </div>
 </form>

jQuery + AJAX part

$('#part_no').change(function (event) 
{
      var option = this.value;

  if (option == '') 
  {
       $("#group4").hide();    

  }else
  {
      $("#group4").show();

       if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                /*$('lot-no-list').html(xmlhttp.responseText);*/
                document.getElementById("lot-no-list").innerHTML = xmlhttp.responseText;
                console.log(option);
             }
        };
        xmlhttp.open("GET","get_assignments.php?rec="+option,true);
        xmlhttp.send();

  }
});

I was able to post the value changing the jquery part

from

var option = this.value;

to

var option = $(this).children(':selected').val();

The answer was previously posted and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM