How do I assign variables in objects?
Autore: Peter W.I want to create a dialog box in html to assign a variable containing a url and then use that variable in the QR Code and Hover button to let the user scan or call that url.
The html code would look something like this:
<script>
var url = prompt('Please enter the url?');
</script>
I then want to dynamically use that variable as the Link URL in the QR Code object and as the target in the Hover Button object.
The purpose is to let users enter a promotion code in the dialog and that code is added to a base url as a variable and the user is then presented to a QR code to scan with their smartphone, or for those visiting the website with their smartphone, then can click on the button to be sent to the url with the promotion code as part of the link.
In simpler terms, how do I insert a variable as a parameter in these objects?
Hello Peter,
The best to create a variable and to use it in all website is to create a cookie with the value and to read this cookie when necessary. it's works fine, I use it.
To generate your cookie (HTML object)
var my_value = "demo";
document.cookie = 'afs_cookie='+ my_value + '; path=/';
You can check your cookie creation by a Chrome Inspect
To read your cookie (HTML Object).
<script>
function getCookie(name)
{
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? value[1] : null;
}
var my_cookie = getCookie('afs_cookie');
</script>
And if you need to generate a QR code with this variable I suggest to create your own code to generate this QR code and to reuse your cookie value.
Here an example of code to generate a QR code.
https://www.mathweb.fr/euclide/2020/11/04/qr-code-html-et-php/
Enjoy!
Axel
Upload the QRcode library from here and install it into the same directory of your PHP file to generate the QRCode.
https://www.243tech.com/comment-generer-un-qr-code-en-php/
And create your PHP file like:
<?php
include('phpqrcode/qrlib.php'); //On inclut la librairie au projet
$lien='https://www.afsoftware.fr' // Vous pouvez modifier le lien selon vos besoins
QRcode::png($lien, 'image-qrcode.png'); // On crée notre QR Code
?>
<html>
<p>
<img src="image-qrcode.png">
</p>
</html>
Your QR code is generated:
Enjoy!
Axel
I had a typo that maybe made my post un-understandable
Should be:
In a very simple term: You cannot
But you can use custom code to create the QRcode and then use your url (the prompted URL) as variable in the code.
Maybe you could use code like this: https://github.com/grizzly/jquery.qrcode
Autore
Thanks for the help and I also asked another expert who solved the problem for me. In the end, I was able to use a variable in the QR and Hover objects by some clever programming. If you want to see the final version, fo to this web page and click on the green button: https://app.gadgetheaven.net/order-promotion.php
It implements cookies and parameters when caling the home page. If no parameter or cookie has been set, then a dialogue appears, asking for the promotion code.
This is the code used to make it work:
<script src="https://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js"></script>
<script>
$(document).ready(function() {
var baseurl = 'https://app.cell-wellbeing.com/'
var cpcode = Cookies.get('cwb_cookie');
var pcode = '';
if (!pcode) {
if (!cpcode) {
pcode = prompt('Please enter your promotion code (just the word itself)?');
} else {
pcode = cpcode;
}
}
if (pcode && !cpcode) {
// set cookie for 60 days
Cookies.set('cwb_cookie', pcode, {expires: 60, path: ''});
}
pcode = baseurl + pcode; // pcode now has full url
$('#pluginAppObj_70_09 img').attr('src', '//api.qrserver.com/v1/create-qr-code/?data=' + pcode + '&size=200x200&color=0-0-0&bgcolor=255-255-255&margin=1');
$('a.pluginAppObj_70_10-button').attr('href', pcode);
});
</script>