karthika pommudu

karthika pommudu

  • NA
  • 321
  • 35k

How to get textbox value into php query from javascript & html

Jul 16 2020 2:09 AM
Great day :) I want to get 'id='countryname_" + counter + "'' value from javascript,then pass the value to ajax function.I have tried few methods but its doesn't work.can you please help me to get a great output.
  1. <div class="row">  
  2. <div class="col-md-6">  
  3. <label>Name</label>  
  4. <input type="text" id='name' name="Name" value="<?php echo $name; ?>">  
  5. </div>  
  6. </div>  
  7. <table>  
  8. <tr>  
  9. <td> </td>  
  10. <td>  
  11. Select:  
  12. <select id="ail_select" name="ail_select">  
  13. <option value="">--- Select ---</option>  
  14. <option value="Access card">Access card</option>  
  15. <option value="Vehicle">Vehicle</option>  
  16. </select>  
  17. </td>  
  18. </tr>  
  19. </table>  
  20. <button type="button" class='btn btn-success addmore'>+ Add More</button>  
  21. <td><button type="button" style ='background-color: #f44336;' class='btn btn-danger delete'>- Delete</button></td>  
  22. <br>  
  23. <div class="row">  
  24. <div class="col-sm-6">  
  25. <span class="fw-separator"></span>  
  26. <button class="previous-form action-button color-bg" id ="sub"; onclick ="getdet()" name="sub">Update Details</button>  
  27. </div>  
  1. $(document).ready(function()  
  2. {  
  3. $(".delete").on("click"function() {  
  4. $(".case:checkbox:checked").parents("tr").remove();  
  5. $(".check_all").prop("checked"false);  
  6. check();  
  7. });  
  8. var counter = $("table tr").length - 1;  
  9. $(".addmore").on("click"function(){  
  10. var ail = $("#ail_select").val();  
  11. if (ail.length > 0) {  
  12. counter++;  
  13. var data = "<tr><td><input type='checkbox' class='case'/></td><td><span id='snum" + counter + "'>" + ail + ".</span></td>";  
  14. data += "<td><input class='form-control' type='text' id='countryname_" + counter + "' name='countryname[]' required value='<?php echo $datval; ?>'/></td>";  
  15. data += "</tr>";  
  16. $("table").append(data);  
  17. // var datval = $('#datval').val();  
  18. var val = document.getElementById("countryname_" + counter + "").value;  
  19. }  
  20. });  
  21. });  
  22. //......ajax..  
  23. function getdet()  
  24. {  
  25. var datval = $('#datval').val();  
  26. var name = $('#name').val();  
  27. var result={"name": name};  
  28. $.ajax({  
  29. url: "sample.php",  
  30. type: "POST",  
  31. data: {"name": name,},  
  32. success: function () {  
  33. console.log('Updated');  
  34. },  
  35. error: function (err)  
  36. {  
  37. console.log(err);  
  38. }  
  39. });  
  40. }  

Answers (6)