1

I am inserting a list of inputs through php loop.

For each input I need to get the value from database via ajax, For that I need to call a javascript function. I am not sure if it possible.

I am looking for something like this

<input ... value="javascript:getvalue(id)">

<script>
  function getvalue(id) {
    //set the value fron here
  }
</script>
3
  • 1
    are you simply wanting an explanation of how to write ajax or? Commented May 22, 2019 at 6:34
  • You'll have to create an array with the values and call them separately splicing it to use on the function for each input you'll have Commented May 22, 2019 at 6:35
  • why arent you retrieving the input values while or before running your php to insert the input elements? why use ajax after the fact? Commented May 22, 2019 at 6:38

2 Answers 2

1

value attr just accepts strings more here https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#value. You can use data-attributes or an id to assign the id and then get the value for it after its loaded. Please check the below snippet

<input data-id="id1">
<input data-id="id2">
<input data-id="id3">

<script>
  (function(){
    var inputs = document.getElementsByTagName("input");
    for(var i = 0; i < inputs.length; i++) {
      inputs[i].value = getvalue(inputs[i].dataset["id"])
    }
  }())
  function getvalue(id) {
    return id;
  }
</script>

Sign up to request clarification or add additional context in comments.

Comments

0

$(document).ready(function(){ 
  for(let i=0; i< $('.frmDatabase').length; i++)  {
    getvalue($('.frmDatabase').eq(i).attr('id'));
  }
})


function getvalue(id) {
//ajax call
//set async false and in success bind value from db to id
   $('#' + id).val(1);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="txtTest1" class="frmDatabase">
<input id="txtTest2" class="frmDatabase">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.