Hi,
I would like to add a simple discount code functionality to my form.
Really a very simple one:
There will be 4 or 5 static codes which can be entered by the user.
The form code should check this field against some pin codes available in the DB.
I don't know how to implement this. I tried to check codes in javascript. But then the secret codes are visible in the source code of the page.
I'd appreciate if anyone tells me where to start from ?
Thx.
I would like to add a simple discount code functionality to my form.
Really a very simple one:
There will be 4 or 5 static codes which can be entered by the user.
The form code should check this field against some pin codes available in the DB.
I don't know how to implement this. I tried to check codes in javascript. But then the secret codes are visible in the source code of the page.
I'd appreciate if anyone tells me where to start from ?
Thx.
Hi Remain,
The simplest way to do the check is to use a DB Read action after the form submits. That works OK but doesn't give instant feedback on the discount.
You could use JavaScript with an AJAX query - that sends the code back to the server to be checked and then updates the form HTML somehow depending on the results.
Or, thirdly, you could you some kind of hashed check in the in-page JavaScript so that only hashed versions of your discount codes are visible in the page HTML.
Bob
The simplest way to do the check is to use a DB Read action after the form submits. That works OK but doesn't give instant feedback on the discount.
You could use JavaScript with an AJAX query - that sends the code back to the server to be checked and then updates the form HTML somehow depending on the results.
Or, thirdly, you could you some kind of hashed check in the in-page JavaScript so that only hashed versions of your discount codes are visible in the page HTML.
Bob
Thanks.
Seems the easiest way is the third one to me. But how can I hash the password to compare ? Is there a function in javascript for that ?
And if I decide to go with the first option (no discount feedback to the user) how can I read the DB ? What is the correct way of reading and comparing ?
About the second option ; can you give me an example javascipt/AJAX query which does that without submitting the form ?
Thanks.
Seems the easiest way is the third one to me. But how can I hash the password to compare ? Is there a function in javascript for that ?
And if I decide to go with the first option (no discount feedback to the user) how can I read the DB ? What is the correct way of reading and comparing ?
About the second option ; can you give me an example javascipt/AJAX query which does that without submitting the form ?
Thanks.
HI remain,
I found a simple hashing function in this StackOverFlow answer and a much fuller discussion in this SOF thread
Bob
I found a simple hashing function in this StackOverFlow answer and a much fuller discussion in this SOF thread
Bob
Hi,
I am sorry; I am not a developer and I didn't understand how to use that hash mechanism.
Should I first hash my string somewhere else , then compare the hashed input with my previously hashed string ?
And what about making the serverside comparison with an AJAX query as you stated in your first answer ?
Can you give me an example for that ?
Thanks.
I am sorry; I am not a developer and I didn't understand how to use that hash mechanism.
Should I first hash my string somewhere else , then compare the hashed input with my previously hashed string ?
And what about making the serverside comparison with an AJAX query as you stated in your first answer ?
Can you give me an example for that ?
Thanks.
I've found an example ajax query here:
https://www.chronoengine.com/forums/posts/t98535/p352187/chronoform-4-a-simple-ajax-form-with-filter-paginator.html
but I didn't understand how it queries the DB
is it here:
if yes, how can I query a custom table ?
thx.
https://www.chronoengine.com/forums/posts/t98535/p352187/chronoform-4-a-simple-ajax-form-with-filter-paginator.html
but I didn't understand how it queries the DB
is it here:
var load_req = new Request({
url: 'index.php?option=com_chronoforms&chronoform=simple_ajax_form&event=on_search_button_click&page_n=' + page,
method: 'get',
if yes, how can I query a custom table ?
thx.
Hi remain,
There are three parts to an AJAX query.
a. Send a chunk of data from the browser back to a URL on the server.
b. On the server read the data, do something with it and send something back to the browser.
c. In the browser look out for something being sent back and do something with it.
I know those are all a bit vague but there are many different 'somethings' that could happen here.
The code that you have found is for doing step a - except that it is for CFv4 and is using the MooTools library.
In CFv5 using jQuery the JavaScript looks more like this:
To manage the server part we add a new event to the form - named 'ajax' here and add a Custom Code action with code like this
The .code code I have added changes the border color of the input box and either clears an invalid entry or makes a valid entry read-only. Note: these are just examples you can do almost anything with appropriate JavaScript there.
You can see my test form here - the three codes in the array above are valid.
Bob
There are three parts to an AJAX query.
a. Send a chunk of data from the browser back to a URL on the server.
b. On the server read the data, do something with it and send something back to the browser.
c. In the browser look out for something being sent back and do something with it.
I know those are all a bit vague but there are many different 'somethings' that could happen here.
The code that you have found is for doing step a - except that it is for CFv4 and is using the MooTools library.
In CFv5 using jQuery the JavaScript looks more like this:
function checkCode() {
var code;
code = jQuery('#code').val();
jQuery('#code').css('border-color', 'silver');
jQuery.ajax({
// send the code back to the server
url: 'index.php',
data: {
'option': 'com_chronoforms5',
'chronoform': 'test_ajax_code_check',
'event': 'ajax',
'tvout': 'ajax',
'code': code
}
})
// do this when data is received back
.done(function( data ) {
if ( data == 'invalid' ) {
jQuery('#code').val('');
jQuery('#code').css('border-color', 'red');
} else {
jQuery('#code').css('border-color', 'green');
jQuery('#code').prop('readonly', true);
}
});
}
The first half of this is sending the data to the server - the second part is handling the reply.
To manage the server part we add a new event to the form - named 'ajax' here and add a Custom Code action with code like this
<?php
$codes = array(
'apple',
'banana',
'orange',
'pear'
);
$reply = 'invalid';
if ( in_array($form->data['code'], $codes) ) {
// code is valid
$reply = 'valid';
}
echo $reply;
?>
This has an array of valid codes, checks to see if the returned code is in the array and returns 'valid' or 'invalid'.
The .code code I have added changes the border color of the input box and either clears an invalid entry or makes a valid entry read-only. Note: these are just examples you can do almost anything with appropriate JavaScript there.
You can see my test form here - the three codes in the array above are valid.
Bob
thanks Bob.
Thanks for all your effort. But I don't get it.
On the example you give, which part is querying the table on my DB ?
I assume that it should be in the second part (new event named ajax)
but I don't see a mysql query there.
So how do I query ?
Thx.
Thanks for all your effort. But I don't get it.
On the example you give, which part is querying the table on my DB ?
I assume that it should be in the second part (new event named ajax)
but I don't see a mysql query there.
So how do I query ?
Thx.
Hi remain,
Nothing in my code uses a database query as you said there were only 4 or 5 valid codes, You can add a DB Read action to the Ajax event though, before the Custom code.
Bob
Nothing in my code uses a database query as you said there were only 4 or 5 valid codes, You can add a DB Read action to the Ajax event though, before the Custom code.
Bob
you are correct. There will be only 4-5 valid codes. But I need to keep them on server so that they are not readable by user.
So a DB query seems reasonable. And it would enable me to add more functionality if needed. (Like counting usage of codes)
So you suggest me to use DB query. Now I need to understand how it is used.
Where can I find the necessary information ?
Thanks.
So a DB query seems reasonable. And it would enable me to add more functionality if needed. (Like counting usage of codes)
So you suggest me to use DB query. Now I need to understand how it is used.
Where can I find the necessary information ?
Thanks.
Hi remain,
The code I have given you is only on the server. I don’t see how it is more secure to add them to a database table in this case.
You need to add the DB Read action before the Custom Code action; set the Conditions box to check the submitted value
Instead of the DB Read you could just add the database query to the Custom Code action, that might be simpler in this case.
Bob
The code I have given you is only on the server. I don’t see how it is more secure to add them to a database table in this case.
You need to add the DB Read action before the Custom Code action; set the Conditions box to check the submitted value
<?php
return array 'code' => $form->data['code'] );
?>
Then in the custom code check to see if a ,match was found - the DB Read action will add it to the $form->data[''] array.
Instead of the DB Read you could just add the database query to the Custom Code action, that might be simpler in this case.
Bob
ok. I got it now.
But I have 2 questions:
1. on the line ('chronoform': 'test_ajax_code_check',) the "test_ajax_code_check" part is the name of the form used , correct ?
2. how can I "add a new event to the form - named 'ajax' " ?
I can't find a way to add an event.There is "add custom code" option but even if this is correct, I don't know where to put it ?
Should I put it under "On Load" section ?
But I have 2 questions:
1. on the line ('chronoform': 'test_ajax_code_check',) the "test_ajax_code_check" part is the name of the form used , correct ?
2. how can I "add a new event to the form - named 'ajax' " ?
I can't find a way to add an event.There is "add custom code" option but even if this is correct, I don't know where to put it ?
Should I put it under "On Load" section ?
ignore my previous post.
I got it. it works
Now I only miss addition of DB read.
I got it. it works
Now I only miss addition of DB read.
"Instead of the DB Read you could just add the database query to the Custom Code action, that might be simpler in this case."
I agree. if it is possible to read the DB in the custom code, I would prefer doing that.
Now, how can I read column X from a table named Y on DB Z ?
I guess that should be easily found through a simple search on Google ?
I agree. if it is possible to read the DB in the custom code, I would prefer doing that.
Now, how can I read column X from a table named Y on DB Z ?
I guess that should be easily found through a simple search on Google ?
Hi remain,
If you Google this site you'll find many examples like this
Bob
If you Google this site you'll find many examples like this
<?php
$db = \JFactory::getDBO();
$query = "
SELECT COUNT(`code`)
FROM `#__some_table`
WHERE `code` = '{$form->data['code']}' ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count > 0 ) {
. . .
Bob
Hi Bob,
I've added DB query to the ajax event. I've also added another ajax event to the form submit success procedure for updating my DB table to track usage of the codes.
Thanks for all your help. I've learnt a lot from you on this thread. You are a very helpful person. Thanks for being here and supplying help...
see you around and have a nice day...
I've added DB query to the ajax event. I've also added another ajax event to the form submit success procedure for updating my DB table to track usage of the codes.
Thanks for all your help. I've learnt a lot from you on this thread. You are a very helpful person. Thanks for being here and supplying help...
see you around and have a nice day...
This topic is locked and no more replies can be posted.