Hi,
here the promised example:
In my survey I have a hidden question of type "short text" with this javascript to do the ajax call:
Code:
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
$.post('https://www.myServerr.com/myFolder/getCode.php',function(data) {
$('#question{QID} input[type="text"]').val(data)
});
$('#question{QID}').hide();
);
</script>
In my database I created a table "voucher" with three fields:
1. id (autoincrement)
2. vouchercode (varchar(20))
3. status (Tinyint) This as flag, if the code was already used
And now the php file "getCode.php"
(only as example, depends on your way of connecting and library (mysqli, PDO, ...)
1. Connect to the database
2. query the table:
"SELECT vouchercode from voucher WHERE status=0 limit 1" (So you get always only one row with the first not used vouchercode)
3, Save the vouchercode to $myCode=
4. Update the table; set the status of this vouchercode to "used" =1)
"UPDATE voucher SET status=1 WHERE vouchercode=$myCode";
5. echo the vouchercode
echo $myCode;
which is really the next one in the database.
Joffm