I have a js function that make some ajax post and a php handler that parse the $_POST and do something. I would use in js and php the same constants. For example if i put some define in php:
define('done','1',TRUE);
I would like to have it on js too. Are there some way to have some constants declared one time and usable in all this 2 languages?
Define your constants in PHP but output them to your javascript by either writing them to an index.php master file (or similar) or fetch them by ajax
Use get_defined_constants to retrieve all constants and parse that to json
ie (I am without environment at the moment and unable to test this)
<script type="text/javascript">
var constants = <?php echo json_encode(get_defined_constants()); ?>
</script>
Have a look at the documentation on get_defined_constants how to retrieve your personal constants, http://php.net/manual/en/function.get-defined-constants.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With