Ya Studio
Ya Studio

Reputation: 75

insert Ajax result in input field

I have this code:

$('body').on("click", '.saveimage', function () {     
    $.ajax({
        type: "post",
        url: "save.php",
        data: foo.crop(570, 765, 'png')
    }).done(function(data) {                
       $('#ajaxDiv').html(data);
    });
});

When I get the result from save.php i need to insert it in other form in hidden input field

For example:

<div class="ajaxDiv">
    <form method="post">
        <input type="hidden" value="result from ajax">
        <input type="submit" value="ok">
    </form>
</div

Is there any way to achieve this?

Upvotes: 2

Views: 10495

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337627

Firstly you should give the hidden input an id so you can select it:

<input type="hidden" value="" id="result">

Then you can use the val() method to set its value to the AJAX response:

.done(function(data) {
   $('#result').val(data);
});

Upvotes: 4

Related Questions