Verify that the field "answer" is correctly completed.
#1
Hello. 


If my thread is in bad forum, please move.

My problem is in form. I downloaded from database questions from "Security Question [ACP]". This to the register page. I need use that my new page. 
Ok, I have these questions, but how verify, that in field (input) "answer" entered the correct answer?

This is code in my script (from member.php):

// Security Question
		$questionbox = '';
		if($mybb->settings['securityquestion'])
		{
			$sid = generate_question();
			$query = $db->query("
				SELECT q.*, s.sid
				FROM ".TABLE_PREFIX."questionsessions s
				LEFT JOIN ".TABLE_PREFIX."questions q ON (q.qid=s.qid)
				WHERE q.active='1' AND s.sid='{$sid}'
			");
			if($db->num_rows($query) > 0)
			{
				$question = $db->fetch_array($query);

				$refresh = '';
				// Total questions
				$q = $db->simple_select('questions', 'COUNT(qid) as num', 'active=1');
				$num = $db->fetch_field($q, 'num');
				if($num > 1)
				{
					eval("\$refresh = \"".$templates->get("member_register_question_refresh")."\";");
				}
				
				eval("\$questionbox = \"".$templates->get("member_register_question")."\";");
	
				$validator_extra = "
				$('#answer').rules('add', {
					required: true,
					remote:{
						url: 'xmlhttp.php?action=validate_question',
						type: 'post',
						dataType: 'json',
						data:
						{
							question: function () {
								return $('#question_id').val();
							},
							my_post_key: my_post_key
						},
					},
					messages: {
						remote: '{$lang->js_validator_no_security_question}'
					}
				});\n";		
		}
		}
		eval("\$formularz = \"".$templates->get("formularz")."\";");

Maybe you have other suggestions?

Use:
My script send on email information from the form. I use "security question" in order to security (not SPAM). Next I need verify correct answer.

Regards.

Sorry if sth not understand, but i wrote in translator..

Reply
#2
On PHP side:
https://github.com/mybb/mybb/blob/featur...p#L249-294

For JS you need to load the Validate code just like the member_register template + include jscripts/question.js.
Reply
#3
Ok, I added this PHP code to file and to template "formularz":
<script type="text/javascript">
$(document).ready(function() {
	{$validator_extra}	
});
</script>
How now...
Destroy666 Wrote:include jscripts/question.js.
...?

Reply
#4
Just load that script on the page in a <script src=""> - it may already be in the member_register_question template IIRC though, don't need to add it then.

Also the code you posted is not complete - need to load <script src="{$mybb->asset_url}/jscripts/validate/jquery.validate.min.js?ver=1804"></script> first
Reply
#5
Hmm... doesn't works...

I loaded this file, but not results.
Maybe I give me file and template.
Template: 
<form action="" method="post" id="formularz">
<input type="text" name="formName" value="Imię i nazwisko" onblur="if(value=='') value = 'Imię i nazwisko'" onfocus="if(value=='Imię i nazwisko') value = ''"/>
<input type="text" name="formEmail" value="E-mail" onblur="if(value=='') value = 'E-mail'" onfocus="if(value=='E-mail') value = ''"/><br />
<textarea name="formText" onblur="if(value=='') value = 'Treść zapytania...'" onfocus="if(value=='Treść zapytania...') value = ''">Treść zapytania...</textarea>
	<br />
	{$questionbox}
<input type="submit" name="submit" value="Wyślij formularz"/>
</form>
<script src="{$mybb->asset_url}/jscripts/validate/jquery.validate.min.js?ver=1804"></script>
<!---<script type="text/javascript" src="{$mybb->asset_url}/jscripts/question.js?ver=1804"></script> -->
<script type="text/javascript">
$(document).ready(function() {
	{$validator_extra}	
});
</script>
And file (not all, because next is code form):
if(empty($_POST['submit'])) {	
// Security Question
		$questionbox = '';
		if($mybb->settings['securityquestion'])
		{
			$question_id = $db->escape_string($mybb->get_input('question_id'));
			$answer = $db->escape_string($mybb->get_input('answer'));
			$query = $db->query("
				SELECT q.*, s.sid
				FROM ".TABLE_PREFIX."questionsessions s
				LEFT JOIN ".TABLE_PREFIX."questions q ON (q.qid=s.qid)
				WHERE q.active='1' AND s.sid='{$question_id}'
			");
			if($db->num_rows($query) > 0)
			{
				$question = $db->fetch_array($query);
				$valid_answers = explode("\n", $question['answer']);
				$validated = 0;
				foreach($valid_answers as $answers)
				{
					if(my_strtolower($answers) == my_strtolower($answer))
					{
						$validated = 1;
					}
				}
				if($validated != 1)
				{
					$update_question = array(
						"incorrect" => $question['incorrect'] + 1
					);
					$db->update_query("questions", $update_question, "qid='{$question['qid']}'");
					$errors[] = "błędna odpowiedź";
				}
				else
				{
					$update_question = array(
						"correct" => $question['correct'] + 1
					);
					$db->update_query("questions", $update_question, "qid='{$question['qid']}'");
				}
				$db->delete_query("questionsessions", "sid='{$sid}'");
			}
			$sid = generate_question();
			$query = $db->query("
				SELECT q.*, s.sid
				FROM ".TABLE_PREFIX."questionsessions s
				LEFT JOIN ".TABLE_PREFIX."questions q ON (q.qid=s.qid)
				WHERE q.active='1' AND s.sid='{$sid}'
			");
			if($db->num_rows($query) > 0)
			{
				$question = $db->fetch_array($query);
				$answer = $question['answer'];
				$zla = "bledna  opdsadasd ";
				if($answer == FALSE) {
					$answer = $zla;
				}
				$refresh = '';
				// Total questions
				$q = $db->simple_select('questions', 'COUNT(qid) as num', 'active=1');
				$num = $db->fetch_field($q, 'num');
				if($num > 1)
				{
					eval("\$refresh = \"".$templates->get("member_register_question_refresh")."\";");
				}
				
				eval("\$questionbox = \"".$templates->get("member_register_question")."\";");
				
				$validator_extra = "
				$('#answer').rules('add', {
					required: true,
					remote:{
						url: 'xmlhttp.php?action=validate_question',
						type: 'post',
						dataType: 'json',
						data:
						{
							question: function () {
								return $('#question_id').val();
							},
							my_post_key: my_post_key
						},
					},
					messages: {
						remote: '{$lang->js_validator_no_security_question}'
					}
				});\n";		
			}			
		}
		eval("\$formularz = \"".$templates->get("formularz")."\";");
}
I copied from member.php.

Reply
#6
I updated code.

This script is displayed on each page, but doesn't works.

In browser console:
Uncaught TypeError: Cannot read property 'nodeName' of null                      jquery:2

Is ignored "member.php" - is not there currently.

Any suggestions? Smile

Reply
#7
1. Why is the PHP validation executed when empty($_POST['submit'])?
2. Why is there no value for action attribute in form?
3. You also need to initiate validation for the whole <form>: $("#formularz").validate(); before adding any rules.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)