John Davidson

php - Add a response from Jquery ajax to an already existing modal window

0 comments
Message:


i have a little problem. I can't add response (barcode with title as div) that i get from jquery ajax.


js part


function generate_barcode(name) {
$('#modal-barcode-profile').modal('show');
$.ajax({
url: 'data/barcode_profile.php',
method: "POST",
dataType: 'json',
data: {
code: name
},
success: function (response) {
doSomething(response);
},
})
}

function doSomething(response) {

$('#modal-barcode-profile').html(response);

}

html page


<tbody>
<?php foreach($res as $r): ?>
<tr>
<td><?= $r['name']; ?></td>
<td>
<center>
<button type="button" class="btn btn-warning btn-xs" onclick="fill_form('<?= $r['id'] ?>','<?= $r['name'] ?>');">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span> Edit</button>
<button type="button" class="btn btn-danger btn-xs" onclick="get_id('<?= $r['id'] ?>','del');">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
<button type="button" class="btn btn-info btn-xs" onclick="generate_barcode('<?= $r['name'] ?>')">
<span class="glyphicon glyphicon-print" aria-hidden="true"></span> Print</button>
</center>
</td>
</tr>
<?php endforeach; ?>
</tbody>

barcode_profile.php


<?php
$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
extract($_POST);
?>
<div id='test'>
<center><img src="data:image/png;base64,<?php echo base64_encode($generator->getBarcode($code, $generator::TYPE_CODE_128)) ?>"></center>
<div id="code"><center><?php echo $code ?></center></div>
</div>

and modal window


<div class="modal fade" id="modal-barcode-profile">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">Barcode</h4>
</div>
<div class="modal-body">


</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" id="print">Print Barcode</button>
<button type="button" class="btn btn-default" data-dismiss="modal">OK
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
</button>
</div>
</div>
</div>
</div>

I get response in browser when i click button to generate barcode, but div doesn't appear.


Thank you in advance!



My Answer:
.ajax({
url: 'generate_barcode.php',
type: 'POST',
data: { name: name },
success: function(response) {
$('#modal-body').append('
' + response + '
');
}
});
}

// Call the function with the name parameter
generate_barcode('John Doe');

In this code snippet, we have a function called generate_barcode that makes an AJAX request to a PHP file called generate_barcode.php. The PHP file generates a barcode based on the name parameter passed to it.

Once the response is received, the success function appends the response (barcode with title as div) to the modal window with the id 'modal-body'.

You can call the generate_barcode function with the desired name parameter to display the barcode in the modal window.

Rate this post

5 of 5 based on 8563 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us