aristeidhsP aristeidhsP - 1 year ago 102
jQuery Question

echo last rows of db and then print

I am trying to echo last 5 inserted rows of db one by one and then print it through jquery plugin . For example into the input area to be inserted last row then print then last row -1 then print etc..
in my html i have these inputs in my printablearea

<li><input type="number" min=1 name="printmultiple" value="1" id="printmultiple"/></li>
<div id="printableArea">
<div align="center">


<input type="text" name="barcode2" id="barcode2" required readonly >
<input type="text" name="barcodecountry" id="barcodecountry" >
<input type="text" name="barcodecountry" id="barcodecountry2" >

<div id="print">

<label id="idbar">id.:</label><input type="text" name="barcodesurname" id="barcodesurname" required readonly>
<label id="pricebar">price:</label><input type="text" name="barcodename" id="barcodename" required readonly>
<input type="text" name="barcodecountry" id="barcodecountry3">

then in my php i am inserting multiple values depending of the value of input printmultiple


$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data

// validate the variables ======================================================
// if any of these variables don't exist, add an error to our $errors array
if (empty($_POST['surname']))
$errors['surname'] = 'Surname is required.';
if (empty($_POST['name']))
$errors['name'] = 'Name is required.';

// return a response ===========================================================

// if there are any errors in our errors array, return a success boolean of false
if ( ! empty($errors)) {

// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
} else {

$printmultiple = $_POST['printmultiple'];
$signintime = date("Y-m-d H:i:s");
$username = mysqli_real_escape_string($con,$_POST['username']);
$surname =mysqli_real_escape_string($con,$_REQUEST['surname']);
$name= mysqli_real_escape_string($con,$_REQUEST['name']);
$seat= mysqli_real_escape_string($con,$_POST['seat']);
$mail= mysqli_real_escape_string($con,$_POST['mail']);
$telephone= mysqli_real_escape_string($con,$_POST['telephone']);
$expdate = date('Y-m-d H:i:s', time()+36000);
$submittedby = mysqli_real_escape_string($con,$_SESSION["username"]);

$result = mysqli_query($con, "SELECT customid FROM `salonika` ORDER BY `customid` DESC LIMIT 1");
$row = mysqli_fetch_array($result);
$stmt = $con->prepare("INSERT INTO salonika (surname, name, telephone,customid,seat,mail,ticketprice,barcode,expdate,submittedby,signintime) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

for ($i=0; $i<$printmultiple; $i++) {
$surname = $surname;
$name = $name;
$telephone = $telephone;
$stmt->bind_param('sssississss', $surname, $name, $telephone, $max,$seat,$mail,$ticketprice,$barcode,$expdate,$submittedby,$signintime);




// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';


// return all our data to an AJAX call
echo json_encode($data);

in my js file i have this

var formData = {
'surname' : $('input[name=surname]').val(),
'name' : $('input[name=name]').val(),

'telephone' : $("#telephone").val(),
'mail' : $("#mail").val(),
'barcode' : $("#barcode").val(),
'customid' : $("#customid").val(),
'ticketprice' : $("#ticketprice").val(),
'seat' : $("#seat").val()

// process the form
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'process.php', // the url where we want to POST
data : formData,// our data object
dataType : 'json', // what type of data do we expect back from the server
encode : true
// using the done promise callback
.done(function(data) {

// log data to the console so we can see

// here we will handle errors and validation messages
if ( ! data.success) {
if (data.errors.surname) {
$('#surname-group').addClass('has-error'); // add the error class to show red input
$('#surname-group').append('<div class="help-block">' + data.errors.surname + '</div>'); // add the actual error message under our input
else if ( {
$('#name-group').addClass('has-error'); // add the error class to show red input
$('#name-group').append('<div class="help-block">' + + '</div>'); // add the actual error message under our input

} else {
// ALL GOOD! just show the success message!
$('form').append('<div class="alert alert-success">' + data.message + '</div>');

is there any way to print to the printer one by one last inserted rows?

Answer Source

Add the following code after $stmt->close();, according to your question here I simply build a query and execute it fetching the last $printmultiple rows, that the user inserts.

Then it passes an entry called added to the response that you can handle in your jQuery response .done function.

$printmultiple = intval($printmultiple); //sanitize variable
$result = mysqli_query($con, "SELECT * FROM `salonika` ORDER BY `customid` DESC LIMIT ".$printmultiple);
$rows = mysqli_fetch_array($result);

// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
$data['added'] = $rows;

The data sent back to jQuery is encoded in JSON format.

Finally the JS part would be as follows:

if ( ! data.success) {

    //your code
    $.each(jQuery.parseJSON(data.added), function (index, item) {
        $('#printableArea').append( + " " + item.surname+"<br>");
    $('form').append('<div class="alert alert-success">' + data.message + '</div>'); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download