- Posts: 2
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Validate text input from database
- wilfredor
- Topic Author
- Offline
- New Member
Less
More
10 years 6 months ago #100551
by wilfredor
Validate text input from database was created by wilfredor
Greetings to all members of this forum.
This is my first post in this forum, I'm new in LimeSurvey development area, however, I possess knowledge of Jquery, javascript and php.
My question is focused on the validation of a simple text field. Validation would be as follows:
1. The user enters an ID in a question of simple text field.
2. The ID entered by the user is looked up in a table in a database-not limesurvey
3. In case the ID is found in the table database, the field is successfully validated
This is my first post in this forum, I'm new in LimeSurvey development area, however, I possess knowledge of Jquery, javascript and php.
My question is focused on the validation of a simple text field. Validation would be as follows:
1. The user enters an ID in a question of simple text field.
2. The ID entered by the user is looked up in a table in a database-not limesurvey
3. In case the ID is found in the table database, the field is successfully validated
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10109
- Thank you received: 3595
10 years 6 months ago #100553
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Validate text input from database
I think you'll need to fire an AJAX call to a remote script that accesses your ID. database and returns true/false. (probably on keyup or change of the text input)
Depending on the returned result, you could toggle a hidden question and then use conditions, relevance or quotas to control further access to the survey.
Depending on the returned result, you could toggle a hidden question and then use conditions, relevance or quotas to control further access to the survey.
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- wilfredor
- Topic Author
- Offline
- New Member
Less
More
- Posts: 2
- Thank you received: 0
10 years 6 months ago #100555
by wilfredor
Replied by wilfredor on topic Validate text input from database
I fully understand what needs to be done, however. My specific question is the way to do this is good practice. I mean, what files to edit.
Thank you very much.
Thank you very much.
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10109
- Thank you received: 3595
10 years 6 months ago #100557
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Validate text input from database
You can place the code for the AJAX call in the question source or in template.js.
manual.limesurvey.org/Workarounds:_Manip....29_in_LimeSurvey.3F
manual.limesurvey.org/Workarounds:_Manip....29_in_LimeSurvey.3F
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: wilfredor
The topic has been locked.
- noob
- Offline
- New Member
Less
More
- Posts: 5
- Thank you received: 0
9 years 1 month ago - 9 years 1 month ago #118310
by noob
Replied by noob on topic Validate text input from database
i followed your recomandation and was able to write a few lines:
ok now i need to know how to toggle a hidden object that was created by limesurvey and not with javascript and how to check this value like if!=0 with conditions
please help me somebody its urgent and im not very experienced in coding
my id.php file returns 0 and 1
Code:
<script type="text/javascript" charset="utf-8"> function checkID(str) { if (str.length == 0) { document.getElementById("check").innerHTML = ""; return; } else { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("check").innerHTML = xmlhttp.responseText; document.getElementById("check1").value = xmlhttp.responseText; } } xmlhttp.open("GET", "id.php?q=" + str, true); xmlhttp.send(); } } </script> <p><b>Start typing a name in the input field below:</b></p> First name: <input type="text" onkeyup="checkID(this.value)"> <p>Check: <span id="check"></span></p> <br> <input type="hidden" id="check1" value=""/>
ok now i need to know how to toggle a hidden object that was created by limesurvey and not with javascript and how to check this value like if!=0 with conditions
please help me somebody its urgent and im not very experienced in coding
my id.php file returns 0 and 1
Last edit: 9 years 1 month ago by noob.
The topic has been locked.
- noob
- Offline
- New Member
Less
More
- Posts: 5
- Thank you received: 0
9 years 1 month ago - 9 years 1 month ago #118312
by noob
Replied by noob on topic Validate text input from database
now it looks like this, i think this way is better.
this time the code should be on the first page and the only question. i want to avoid users getting behind this section without a correct answer... the first part checks user input with id.php file. existing id returens 1, else 0. now i need to add the subit event but this doesnt work. and it is necessary to store the existing ID input in thelimesurvey database. the whole prucedure has to work for every login.
i would appreciate your help!
thanks
id.php
this time the code should be on the first page and the only question. i want to avoid users getting behind this section without a correct answer... the first part checks user input with id.php file. existing id returens 1, else 0. now i need to add the subit event but this doesnt work. and it is necessary to store the existing ID input in thelimesurvey database. the whole prucedure has to work for every login.
Code:
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script> $(document).ready(function() { $("#1").keyup(function() { var txt = $("#1").val(); $.post("id.php", {id: txt}, function(result) { $("#2").html(result); $("#3").val(result); }); }); $("form#11").submit(function() { if ($("#3 input.text").val() == "" || $("#3 input.text").val() == 0) { alert("false"); return false; } else { return true; } }); }); }); </script> </head> <body> <form id="11"> <input type="text" id="1"> <p>c: <span id="2"></span></p> <input type="text" id="3" value=""/> <button type="submit" id="movenextbtn" value="movenext" name="movenext" accesskey="n" class="submit button">Weiter</button> </body> </html>
i would appreciate your help!
thanks
id.php
Code:
<?php // ID $a[] = "1111"; $a[] = "2222"; $a[] = "3333"; $q = $_REQUEST["id"]; $match = 0; foreach($a as $name) { if ($q !== "") { if ($q === $name) { $match = 1; break; } else { $match = 0; } } } if ($match == 0){ echo "0"; }else{ echo "1"; } ?>
Last edit: 9 years 1 month ago by noob.
The topic has been locked.
- Ben_V
- Offline
- Platinum Member
Less
More
- Posts: 1128
- Thank you received: 329
9 years 1 month ago #118313
by Ben_V
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Replied by Ben_V on topic Validate text input from database
It could be intersant for you to have a quick look at
this recent post
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The following user(s) said Thank You: noob
The topic has been locked.
- noob
- Offline
- New Member
Less
More
- Posts: 5
- Thank you received: 0
9 years 1 month ago #118315
by noob
Replied by noob on topic Validate text input from database
hey ben thanks for your reply
it helps me improoving my php file. but for now this is not my hardest trouble. the "autocompletition" works so far. but the second part, connecting the return of matching with the submit button of the form causes trouble in fact the script doesnt work after inserting
i think i do not understand the principle bihind the different { } sections. i tried to figure it out with alert commands but without success.any idea whats wrong with this part? the submit button has to be the last one from limesurvey to allow/deny survey progress... thanks a lot!
it helps me improoving my php file. but for now this is not my hardest trouble. the "autocompletition" works so far. but the second part, connecting the return of matching with the submit button of the form causes trouble in fact the script doesnt work after inserting
Code:
$("form#11").submit(function() { if ($("#3 input.text").val() == "" || $("#3 input.text").val() == 0) { alert("false"); return false; } else { return true; } }); });
The topic has been locked.
- noob
- Offline
- New Member
Less
More
- Posts: 5
- Thank you received: 0
9 years 1 month ago - 9 years 1 month ago #118318
by noob
Replied by noob on topic Validate text input from database
ok i made it
this works without limesurvey.
i store true / false return from my php file in a hidden input and check the submit button.
but in limesurvey it does not work...
in source of question with id 5 i paste <script> .. </script> and the input with id check_01 without whitespace between tags
the script doesnt run at all usually the 0 of var x appears in input id=check_01. it shows me if i did any sysntax mistakes and thats not the case without limesurvey...
anay idea why?
thanks
this works without limesurvey.
i store true / false return from my php file in a hidden input and check the submit button.
but in limesurvey it does not work...
in source of question with id 5 i paste <script> .. </script> and the input with id check_01 without whitespace between tags
the script doesnt run at all usually the 0 of var x appears in input id=check_01. it shows me if i did any sysntax mistakes and thats not the case without limesurvey...
anay idea why?
thanks
Code:
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script> $(document).ready(function() { var x = 0; $("#check_01").val(x); $("#question5").keyup(function() { var txt = $("#question5").val(); $.post("id.php", {id: txt}, function(result) { $("#check_01").val(result); }); }); $("form#limesurvey").submit(function() { if( $("#check_01").val() == 0 || $("#check_01").val() == "" ) { alert("void"); return false; } else { alert("true"); return true; } }); }); </script> </head> <body> <form id="limesurvey" action="test2.html" method="post"> <input type="text" id="check_01" value=""/> <input type="text" id="question5" value=""/> <button type="submit" id="movenextbtn" >Weiter</button> </form> </body> </html>
Last edit: 9 years 1 month ago by noob.
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10109
- Thank you received: 3595
9 years 1 month ago #118319
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Validate text input from database
I don't understand why you are including jQuery and creating a new form. The LimeSurvey screen/group is already a form and jQuery is also already included. Surely you can use LimeSurvey elements (text questions) as your inputs.
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- noob
- Offline
- New Member
Less
More
- Posts: 5
- Thank you received: 0
9 years 1 month ago #118320
by noob
Replied by noob on topic Validate text input from database
hey! first i found the error in {id: txt} now its { id: txt }
the script is like this everthing else was just to understand how it works sorry for confusion.
my question is like this ID:5
Textarea: "Innput your ID"
pressing the source button and insert
i hope to accomplish this:
1: force a input of existing ID (id.php checks true false and send it back than i could display true/false)
2: input in question5 has to be stored in database of survey
3: submit button of form#limesurvey returns true if id.php returns true and my idea was to do this by checking a hidden input (id=check_01)
im totally inexperienced and this is my first try and me best idea to do it.
the script is like this everthing else was just to understand how it works sorry for confusion.
my question is like this ID:5
Textarea: "Innput your ID"
pressing the source button and insert
Code:
<input type="hidden" id="check_01" value=""/> <script> $(document).ready(function() { var x = 0; $("#check_01").val(x); $("#question5").keyup(function() { var txt = $("#question5").val(); $.post("id.php", { id: txt }, function(result) { $("#check_01").val(result); }); }); $("form#limesurvey").submit(function() { if( $("#check_01").val() == 0 || $("#check_01").val() == "" ) { alert("void"); return false; } else { alert("true"); return true; } }); }); </script>
i hope to accomplish this:
1: force a input of existing ID (id.php checks true false and send it back than i could display true/false)
2: input in question5 has to be stored in database of survey
3: submit button of form#limesurvey returns true if id.php returns true and my idea was to do this by checking a hidden input (id=check_01)
im totally inexperienced and this is my first try and me best idea to do it.
The topic has been locked.