使用ajax生成一个post,然后将另一个带有post的ajax发送给另一个



我正在尝试在热敏打印机上打印,在执行查询并获得某些数据后,我将这些数据发送到隐藏的输入,问题是我不知道如何将第二个ajax帖子插入并使用到我的文件php中进行打印,我使用的是Mike42 ECPOS库。你能为我指路吗?

function calcCredits(){
/* variables from inputs */
var last_credit= parseInt(document.getElementById("studentcredits").value);
var namesec= document.getElementById("studentqr").value;
var credits= parseInt(document.getElementById("money").value);
var total_pay = credits * 35;
var ttl_credits = last_credit + credits;
/* Beggin Ajax */
$.ajax({
url:'addcredits.php',
type:'post',
data:{namesec:namesec,credits:credits},
success:function(data){
$.ajax({
url: 'ticket.php',
type: 'POST',
success: function(response){
alert('Printing....');
} 
}); 
}
});
}

这是执行搜索并显示结果的php

<?php

require 'database.php';
$con = new Database();
$pdo = $con->conectar();
$campo = $_POST["campo"];
$sql = "SELECT * FROM students WHERE student_qr LIKE ? OR student_secondname LIKE ? ORDER BY student_secondname ASC LIMIT 0, 10";
$query = $pdo->prepare($sql);
$query->execute([$campo . '%', $campo . '%']);
$html = "";
$data_query['result'] = $html;
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$html .= "
<div class="container-fluid ">  
<div class="row">
<div class="col-lg-12">
<table class="table table-hover">
<tr >
<th scope='col'>Grupo</th>
<th scope='col'>Nombre</th>
<th scope='col'>Créditos</th>
</tr>
<tr>
<td id='list_id'>".$row["student_group"]."</td>
<td id='list_id'>".$row["student_name"]." ".$row["student_secondname"]." ".$row["student_lastname"]."</td>
<td id='list_name'>".$row["student_credits"]."</td> 
<td id='list_credits' style="cursor: pointer" onclick="mostrar('" .$row["student_qr"] ."'), mostrar2('" .$row["student_credits"] ."')"><button type='button' class='btn btn-warning' id= 'show' onclick='show()' >Añadir Creditos</button></td>
</tr>
</table>
</div>
</div>
</div>

";
}
echo json_encode($html, JSON_UNESCAPED_UNICODE);

这是用来打印的

<?php

require __DIR__ . '/ticket/autoload.php'; 
use Mike42EscposPrinter;
use Mike42EscposEscposImage;
use Mike42EscposPrintConnectorsWindowsPrintConnector;
$nombre_impresora = "POS"; 
$connector = new WindowsPrintConnector($nombre_impresora);
$printer = new Printer($connector);
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->setJustification(Printer::JUSTIFY_LEFT);
$printer->text("Producto Galletasn");
$printer->text( "2  pieza    10.00 20.00   n");
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->text("Muchas gracias por su compran");
$printer->feed(3);
$printer->close();
?>

很抱歉,如果结构不好,我还在练习,谢谢

在评论和贡献对我帮助很大之后,我注意到我的错误是将信息转发到另一个文件,而打印可以在第一个插入php中处理,所以我添加了票证代码,就这样。

这是ajax函数

function calcCredits(){

$.ajax({
/* Se envian datos por POST a PHP */
url:'addcredits.php',
type:'post',
dataType: 'json',
data:{
studentqr:$('#studentqr').val(),
credits:$('#credits').val(),
},         
})
.always(function(){
console.log("complete");
});
}   

然后,php插入

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
include("conection.php");
include("ticket/autoload.php");
$credits = mysqli_real_escape_string($con, $_POST['credits']);
$studentqr = mysqli_real_escape_string($con, $_POST['studentqr']);

$stmt = $con->prepare("UPDATE students 
SET student_credits = (student_credits + ?) 
WHERE student_qr = ?");
$stmt->bind_param("is", $_POST['credits'], $_POST['studentqr']);
$stmt->execute();
$insert_query = $con->prepare("INSERT INTO historical_credits (id_student, credits_paid)
SELECT id_student, ?
FROM students
WHERE student_qr = ?");
$insert_query->bind_param("is", $_POST['credits'], $_POST['studentqr']);
$insert_query->execute();

use Mike42EscposPrinter;
use Mike42EscposEscposImage;
use Mike42EscposPrintConnectorsWindowsPrintConnector;
$nombre_impresora = "Termica"; 
$connector = new WindowsPrintConnector($nombre_impresora);
$printer = new Printer($connector);
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->setJustification(Printer::JUSTIFY_LEFT);
$printer->text("Producto Galletasn");
$printer->text( "2  pieza    10.00 20.00   n");
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->text("Muchas gracias por su compran");
$printer->feed(3);
$printer->close();
$archivoActual = $_SERVER['PHP_SELF'];
header("refresh:2;url= + $archivoActual ");
mysqli_close($con);
?>

非常感谢您抽出时间

最新更新