0

I am having this piece of code, to load data form PHP after users click on link. Then I am displaying received data to div id="vies":

<script>
    $(document).ready(function(){
      $("#data-received").hide();
      $("#click_me").click(function(){
        $("#data-received").show();
        $("#vies").load('127.0.0.1/get_data/', {VATID : $("#vat_id").val()});   
      });
    });
</script>


<label>VATID</label>
<input type="text" id="vat_id" name="vat_id" value="">
<a href="#" id="click_me">Check VATID</a>


<div id="data-received">
    <label>Data received from PHP</label>
    <div id="vies">.. checking ..
        <input type="text" name="put-here" id="put-here" value="test-value"/>
    </div>
</div>

The question is how to load data and insert as a input value here:

<input type="text" name="put-here" id="put-here" value="test-value"/>

instead to whole div.

2
  • Can you give us an exmple of the data you get from /get_data/? Commented Jan 25, 2015 at 14:23
  • Mor Haviv: In this simple example I need to pass just one variable. But in reality I need to receive whole data from PHP (name, address, postcode etc.) and then paste the data as a few inputs. Is there any way to receive a array from PHP or this need to be done in different way? Commented Jan 25, 2015 at 14:51

2 Answers 2

1
<script>
    $(document).ready(function(){
        $("#data-received").hide();
        $("#click_me").click(function(){
            $("#data-received").show();
            $.post('http://127.0.0.1/get_data/', {
                VATID : $("#vat_id").val()
            }, function(data) {
                $('#put-here').val(data);
            });
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

load() is a convenience function which does an ajax request and then replaces the target element with the data returned. When you don't want to replace the whole element, use jquery.ajax() to do the request. In the success callback you can set your value to the returned data using .val().

$.ajax({
  url: "127.0.0.1/get_data/",
  data: {VATID : $("#vat_id").val()}
}).done(function(data) {
  $( '#put-here' ).val(data);
});

1 Comment

Thank you for the provided information. I wronlgy thought that I can use load() function.

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.