html - Trying to get value from select form with JQuery -


what trying selected value measurement form pass using measurementid: $('#measurement').find(":selected").val() instead of using php, not seem work. have tried $('select[name=measurement]').val() , $('#measurement option:selected').val()

  <div>              <?php             require 'getmeasurementtypesbyuser.php';             $option1 = isset($_get['user']) ? $_get['user'] : false;             if ($option1) {                 $res = getmeasurementtypesbyuser($_get['user']);                  echo '<form method="post" action=""><select id="measurement" name="measurement" onchange="this.form.submit();">';                 echo '<option value="">select measurement</option>';                 foreach ($res $row) {                     if ($row['measurementid'] != 1 && $row['measurementid'] != 2) {                         echo '<option value="' . $row['measurementid'] . '">' . $row['measurementname'] . '</option>';                     }else if($row['measurementid']== 1 ){                         echo '<option value="1">pressure</option>';                     }                 }                 echo '</select></form>';                 $userid = htmlentities($_get['user'], ent_quotes, "utf-8");             }             $option = isset($_post['measurement']) ? $_post['measurement'] : false;             if ($option) {                 $measurementid = htmlentities($_post['measurement'], ent_quotes, "utf-8");                 if($measurementid==1){                     $measurementid2=2;                 }             }             echo '<form method="get" action=""><select id="user" name="user" onchange="this.form.submit();">';             echo '<option value="">select user id</option><option value="1">1</option><option value="2">2</option></select></form>';             ?>          </div>         <!-- javascript -->         <script src="libs/jquery-3.2.1.js" type="text/javascript"></script>         <script type="text/javascript">             $(document).ready(function () {                 $.ajax({                     url: "/kostas/measurementdata.php",                     type: "post",                     data: {userid: <?php echo json_encode($userid); ?>, measurementid: $('#measurement').find(":selected").val()}, //here                     success: function (data) {                         console.log(data); //goes on 

here's how i'd it:

html:

<select data-user="<?php echo json_encode($userid); ?>" id="myselect">     <option value="1">option 1</option>     <option value="2">option 2</option>     <option value="3">option 3</option> </select> 

js:

<script>     $(document).ready(function()     {         $('.mysubmitbtn').on('click', function()         {             var selected = $('#myselect').val(),                 userid   = $('#myselect').data('user');              $.ajax({                 data:    {value: selected, user: userid},                 url:     'myscript.php',                 type:    'post',                 success: function(res) {console.log(res)},                 error:   function(res) {console.log(res)}             })         })     }); </script> 

what is, assign user id select data value. means can use .data retrive data elements - syntax is: .data('attribute') - data-attribute.

it gets selected value on click, issue doing ajax straight away, is getting selected value, on document load, not on update. that's why shows old value.

it passes script whatever with.

note: code not like yours don't copy , paste ;)

update 1

an example (js) more tailored op

<script>     $(document).ready(function()     {         $('#myselect').on('change', function()         {             var selected = $(this).find('option:selected').val(),                 userid   = $(this).data('user');              $.ajax({                 data:    {value: selected, user: userid},                 url:     'myscript.php',                 type:    'post',                 success: function(res) {console.log(res)},                 error:   function(res) {console.log(res)}             })         })     }); </script> 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -