1

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>
7
  • 1
    Replace $data['cityName']; To : $_POST['name']; Commented Sep 8, 2017 at 6:17
  • I can barely understand what you are asking for. If you want to get text from p tag and set it as value of input use jQuery like this: $("input#inputSuccess").val($("p").text()); Commented Sep 8, 2017 at 6:18
  • user type london record fetch from data base-> where you fetch record? Commented Sep 8, 2017 at 6:18
  • @AlivetoDie $data i am using while loop Commented Sep 8, 2017 at 6:28
  • if there is any image gallery site where i put my code screent shot ?? Commented Sep 8, 2017 at 6:38

2 Answers 2

1

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.

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

Comments

0

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>

2 Comments

i think i want to edit my question so everyone understand my problem more clearly
<div style="width:80%;"> <p><?php echo $data['cityName']; ?></p> </div>

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.