- Posts: 6
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Limit one CNPJ (like EIN) per participant (VALIDATION DB)
- WbTOne
- Topic Author
- Offline
- New Member
I need to validate a field of my survey: CNPJ. It's a unique value per business on my form, like a EIN.
I want to limit just one register of business with same CNPJ number, example:
At first registry: 92.664.028/0001-41 VALIDATION: OK
If user try to register again as a new register: 92.664.028/0001-41 VALIDATION: ERROR!
Thank you very much!!
- holch
- Offline
- LimeSurvey Community Team
- Posts: 11639
- Thank you received: 2737
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
- WbTOne
- Topic Author
- Offline
- New Member
- Posts: 6
- Thank you received: 0
I need help, not delete please.
Tks
- Joffm
- Offline
- LimeSurvey Community Team
- Posts: 12881
- Thank you received: 3965
since LS is a survey tool each respondent or each questionnaire is absolutely independent.
Meaning LS has no access to the answers of other respondents.
You have to use an ajax call to query the database.
Create a question of type "short free text" and include this snippet in the question text (source code mode).
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ $.post('https://www.myServer.com/myPath/getCount.php' , { sid: "{SID}"} ,function(data) { $('#question{QID} input[type="text"]').val(data) }); $('#question{QID}').hide(); }); </script>
The php file now has to query the database and return the number of your used IDs (0 or 1).
The way you query the database is up to you.
Either with direct access to the LS database or by the API.
Now you have the result in this question.
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
- WbTOne
- Topic Author
- Offline
- New Member
- Posts: 6
- Thank you received: 0
My php is:
if ($db) { $ac_cnpjval = "%".$_GET['cnpjval']."%"; $query = "SELECT id, 195818X4X52 FROM gfrm_survey_195818 WHERE 195818X4X52 LIKE :cnpjval"; $result = $db->prepare($query); $result->bindValue(":cnpjval",$ac_cnpjval); $result->execute(); // Fetch data and store in array $row = $result->fetch(); if ($row['id'] != NULL) { echo "<pre>"; echo "Duplicated value - id: " . $row['id']; echo "</pre>"; echo "<br>"; } else { echo "The value is unique, OK!."; } }
Now, I need to get the typed field HTML after typed and send ajax requisition to validate it, real-time!
It's possible?
Thank you!!
- Joffm
- Offline
- LimeSurvey Community Team
- Posts: 12881
- Thank you received: 3965
1. As you see, the ajax call sends a $_POST-parameter, so maybe you should also receive it by $_POST
2. You should only echo a "0" or "1", which is displayed in the short text question. This you can use to give a message to the respondent and to screenout.
3. I missed in my example to post the question ID of the text entry field and the entered value.
So now the call is:
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ $.post('https://www.myServer.com/myPath/getCount.php' , { sid: "{SID}", qid: "602X9024", vv: "{Q1}"} ,function(data) { $('#question{QID} input[type="text"]').val(data) }); // $('#question{QID}').hide(); }); </script>
"602X9024": group and question ID of CNPJ question (Q1) (has to be adapted)
And the php is like that:
<?php // database connection $SID = $_POST["sid"]; $QID = $SID."X".$_POST["qid"]; $value = $_POST["vv"]; $count = DB::queryFirstField("SELECT count(*) FROM lime_survey_".$SID." WHERE ".$QID."=".$value." and submitdate is not NULL"); if ($count>0) {$count=1;} echo $count; // Either 0 or 1 is returned ?>
LSS:
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
- tpartner
- Offline
- LimeSurvey Community Team
- Posts: 10099
- Thank you received: 3589
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
- DenisChenu
- Offline
- LimeSurvey Community Team
- Posts: 13597
- Thank you received: 2487
I think we can use a new Expression Manager functiontpartner wrote: Not much use here, but in version 4.x, custom Twig extensions containing PHP will be available so we should be able to roll this all into a custom question theme without an AJAX call or a remote PHP file.
See the push request : github.com/LimeSurvey/LimeSurvey/pull/1320
And see related feature fixed : bugs.limesurvey.org/view.php?id=11589
Then with a
statCountIf(self.sgqa, self.NAOK) == 0
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
- jelo
- Offline
- Platinum Member
- Posts: 5033
- Thank you received: 1257
Interesting. The workarounds with external PHPscripts are often begging for getting hacked/misused.tpartner wrote: Not much use here, but in version 4.x, custom Twig extensions containing PHP will be available so we should be able to roll this all into a custom question theme without an AJAX call or a remote PHP file.
I wasn't aware that PHP code will be allowed in custom twig extensions in LS4.
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
- tpartner
- Offline
- LimeSurvey Community Team
- Posts: 10099
- Thank you received: 3589
Yes, by definition the extensions are PHP files.jelo wrote:
Interesting. The workarounds with external PHPscripts are often begging for getting hacked/misused.tpartner wrote: Not much use here, but in version 4.x, custom Twig extensions containing PHP will be available so we should be able to roll this all into a custom question theme without an AJAX call or a remote PHP file.
I wasn't aware that PHP code will be allowed in custom twig extensions in LS4.
Here is a quote from the comments in the sample extension:
NOTE 2: A twig extension is PHP code, so it can do anything on the server. So user should trust you to upload your twig extension.
* So if your goal is to sell your Survey / Question Theme on LimeStore, you'd rather use twig code as much as you can.
I'm interested in trying it with API calls but currently it's broken (at least I cannot get it to work) - bugs.limesurvey.org/view.php?id=15446
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
- WbTOne
- Topic Author
- Offline
- New Member
- Posts: 6
- Thank you received: 0
Thanks for all your suport!! @Joffm @tpartner @DenisChenu @jelo
I hope this question can helps community.
Now, my JS, with addScriptToQuestion plugin, is:
$("#answer195818X4X52").keyup(function () { var total_chars = 18; if ($('#answer195818X4X52').val().length == total_chars) { $.post('http://url/script.php', { sid: "{SID}", qid: "4X52", vv: "{Q1}" }, function (data) { $('#question{QID} input[type="text"]').val(data) }); // $('#question{QID}').hide(); } });
My php is:
$SID = $_POST["sid"]; $QID = $SID."X".$_POST["qid"]; $value = $_POST["vv"]; $count = DB::queryFirstField("SELECT count(*) FROM lime_survey_".$SID." WHERE ".$QID."=".$value." and submitdate is not NULL"); if ($count>0) {$count=1;} echo $count;
but, when I type all characters and the ajax is executed, is returned error 500 on php script.
In browser console, the variables are:
sid: 195818
qid: 4X52
vv: <span class='em-expression em-haveerror' ><span class='em-error'><span title='Undefined variable' class='em-var em-var-error' >Q1</span></span></span>
UPTDATE:
If I update {Q1} var to {P003} (P003 is my question code) the 'vv' var is null:
vv:
Thanks again
- WbTOne
- Topic Author
- Offline
- New Member
- Posts: 6
- Thank you received: 0
I got it!! With the essential help of you my friends!
==== JS: ==== $("#answer{SID}X{GID}X{QID}").keyup(function () { var that = this; var value = $(this).val(); var total_chars = 18; if ($('#answer{SID}X{GID}X{QID}').val().length == total_chars) { $.post('http://uri.com/script.php', { sid: "{SID}", qid: "{GID}X{QID}", vv: value}, function (data) { if (data == 1) { $('#question{QID}').removeClass("input-error").addClass( "input-error" ); $('#vmsg_{QID} div').removeClass("ls-em-error").removeClass("text-danger").addClass("ls-em-error").addClass("text-danger"); $('#vmsg_{QID} div').show().html("<span class='fa fa-exclamation-circle' aria-hidden='true'> </span> CNPJ exists. Please, try again."); } else { $('#question{QID}').removeClass("input-error"); $('#vmsg_{QID} div').removeClass("ls-em-error").removeClass("text-danger"); $('#vmsg_{QID} div').hide(); } }); } });
==== PHP: ==== $dbUser = ''; $dbPassword = ''; $dbServer = ''; $dbName = ''; $pdo = new PDO("mysql:host=$dbServer;dbname=$dbName", $dbUser, $dbPassword); $SID = $_POST["sid"]; $QID = $SID."X".$_POST["qid"]; $value = $_POST["vv"]; $sql = "SELECT COUNT(*) FROM gfrm_survey_$SID WHERE $QID='$value' and submitdate is not NULL"; $statement = $pdo->prepare($sql); $statement -> execute(); $statement -> execute(array($num_rows)); $num_rows = $statement->fetchColumn(); echo $num_rows;