MK12
MK12

Reputation: 491

select text from ajax response and show in input field when user click on item

user type london record fetch from data base and show below the input field

<input type="text" id="inputSuccess" name="name" onkeyup="autosearch(this.value)" > //user type london 

when user click on london than london add in input field but it cannot set in input field what jquery script require to select the value from p tag and show in input field

<script>
function autosearch(name){
   //get data from database
   $.ajax({
       url:"ajax.php",
       type:'post',
       data: {name: name},
       success:function(result){
           $('.result').html(result);
       }
   });
}
</script>

ajax.php

<div style="border: 1px solid #ccc;width:100%;" >
   <a href="javascript:void(0)">
</div>
<div>
  <p><?php echo $data['cityName']; ?> </p> //london show here 
</div>
  </a>
</div>

Upvotes: 1

Views: 3671

Answers (2)

Syed Aqeel
Syed Aqeel

Reputation: 1009

If I got your question then Here is an example to let you clear how to add text in the input. Please checkout below:

HTML:

<input type="text" id="inputSuccess" name="name" onkeyup="autosearch(this.value)" >

<a href="javascript:void(0)" data-val="textToShow" onclick="addTextInInput($(this).data('val'))">
  <div>
    <p>asd </p> 
  </div>
</a>

JS:

function addTextInInput(txt){
 //get data from database
   $("#inputSuccess").val(txt)
 }

Replace the textToShow in data-val with your PHP code and try it. Also, find working example here https://jsbin.com/witilohinu/1/edit?html,js,console,output

Please give your feedback in the comment.

Upvotes: 1

Namindu Sanchila
Namindu Sanchila

Reputation: 414

I am not sure its you want. But this i understand from your code. if its wrong say me. I will help you fix this. Add the class for your <input> and set it Ajax result.

 <input type="text" id="inputSuccess" name="name" class='result'

       onkeyup="autosearch(this.value)" > //user type london


   <script>

   function autosearch(name){
 //get data from database
   $.ajax({
     url:"ajax.php",
    type:'post',
    data:{name:name},
    success:function(result){
      $('.result').html(result);
     }
  });
 }

</script>

You not need this Field.

<p><?php echo $data['cityName']; ?> </p> //london show here 
</div>

Upvotes: 0

Related Questions