This answer is only a complement to the answer given by @joseFranciscoSotteccani. As he says in his answer, the best thing in your case is to use Ajax
.
The reason for using Ajax
is not a whim. Apart from not wanting an innocent kitten to die, the code that runs on the server must remain on the server, separate from the code that runs on the client. This will allow you to have your code more controlled in each environment apart from making it more maintainable and scalable. Imagine how it would be over time to maintain a code where the HTML
, the CSS
, the JavaScript
and the PHP
were all mixed. How would you know where to find a specific functionality? How do you debug a code with these characteristics quickly and efficiently? If you start a project using bad practices I assure you that the more you grow, the problems and the headaches will grow with you.
Regarding your specific problem, to achieve what you want you should not change your code PHP
almost nothing. You just have to call it using Ajax
and use in JavaScript
the variables that you receive. Just keep in mind that as the variable $folio
you receive within a cycle for
it is best to save all the values returned $items->ask
within a Array
. Here is an example:
PHP Code:
$folios_key = ReceiptData::getAllFolios();
$folios_array = array();
if (count($folios_key) > 0) {
foreach ($folios_key as $items) {
$folio = $items->ask;
$folios_array[] = ($folio == "") ? 1 : $folio + 1;
}
}
echo json_encode($folios_array);
JavaScript code:
function folio_generate () {
$.get("path/a/tu/fichero.php", function (folios) {
alert("Los folios generados son: " + folios.join(", "));
}, "json");
}