Im making a web interface for a RFID reader and trying to make it so there is a button to push then it waits for the user to scan a card and retrieves the UID of the card. I was thinking of using ajax to call a php script which calls the python script, but it doesnt wait for the card to be scanned. Heres the ajax:
<script type = "text/javascript">
function scancard () {
$.ajax( { type : 'GET',
data : { },
url : 'scancard.php',
success: function (data) {
var card_id = document.getElementById('card_id');
card_id.value = data;
},
error: function (xhr) {
alert( "error");
}
});
}
</script>
And the button html:
<button onclick="scancard()">Scan Card</button>
Testing with a simple echo cmd it fills in the field but then refreshes the page and the field is blank again.
The next problem I have is when I put in this for the php script to call the python code:
<?php
exec('sudo python scancard.py', $output, $return);
echo $return;
?>
It doesnt wait for the python code to finish before it does the echo command. The python code has a while loop in it that is waiting for a RFID card to be read and then returns the uid code.
# Lots of configuration and setup stuff above this...it works when run via command line
# Main loop to detect cards and read a block.
while True:
uid = pn532.read_passive_target()
if uid is None:
continue
else:
if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B,
[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]):
continue
uid = '0x{0}'.format(binascii.hexlify(uid))
sys.exit(uid)
I might be going about this in a roundabout way as well so if there is a better way to do this, please let me know