0
I have an AJAX form that only shows a captcha to the user when the form is sent more than 2 times.
I save those attempts on $_SESSION
with PHP, but as the form is sent via AJAX captcha will only appear when the user refreshes the page even though he has sent the form more than 2 times.
This is because I’m not updating the headers I believe. How would I do this with AJAX? By sending the form
that data be updated?
@Edit:
In my contact.php
I do the following to set the requests
if ($_SERVER['REQUEST_METHOD'] == 'POST') $_SESSION['attemptsSendEmail'] += 1;
if ($_SESSION['attemptsSendEmail'] >= 2) {
if(!isset($ret->captcha)){
http_response_code(401);
exit('Captcha errado, bloqueado pelo anti-spam');
}
if (isset($ret->captcha) && $ret->captcha !== $_SESSION['captcha']) {
http_response_code(401);
exit('Captcha errado, bloqueado pelo anti-spam');
}
}
And here the validation in Javascript
waghcwb.query('#contact-form').onsubmit = function(event) {
event.preventDefault();
var btn = waghcwb.query('#contact-form button[type="submit"]');
waghcwb.post('contact/contact.php', {
name: waghcwb.query('input[name="name"]').value,
email: waghcwb.query('input[name="email"]').value,
message: waghcwb.query('textarea[name="message"]').value,
captcha: (waghcwb.query('input[name="captcha"]')) ? waghcwb.query('input[name="captcha"]').value : null
}, function() {
btn.style.backgroundColor = '#34DB42';
btn.style.borderColor = '#34DB42';
btn.setAttribute('class', 'button success');
btn.innerHTML = '<img src="img/check.svg" alt="Enviado com sucesso" title="Enviado com sucesso">';
waghcwb.query('input[name="captcha"]').className = '';
if (waghcwb.query('#captchaImg')) waghcwb.query('#captchaImg').src = '/captcha/captcha.php?' + new Date().getTime();
waghcwb.query('input[name="name"]').value = '';
waghcwb.query('input[name="email"]').value = '';
waghcwb.query('textarea[name="message"]').value = '';
if (waghcwb.query('input[name="captcha"]')) waghcwb.query('input[name="captcha"]').value = '';
}, function() {
btn.style.backgroundColor = '#DB3434';
btn.style.borderColor = '#DB3434';
btn.setAttribute('class', 'button error');
btn.innerHTML = '<img src="img/times.svg" alt="Erro ao enviar os dados" title="Erro ao enviar os dados">';
},
function() {
var input_captcha = waghcwb.query('input[name="captcha"]'),
captcha = waghcwb.query('#captchaImg');
input_captcha.placeholder = 'O código de segurança não confere.';
input_captcha.className = 'error';
input_captcha.value = '';
if (captcha) captcha.src = '/captcha/captcha.php?' + new Date().getTime();
});
};
Post what you have done in JS. If you count the attempts use the
var
to show the captcha.– Papa Charlie
You can increment the Sessions variable in PHP each time ajax calls it. Type
$_SESSION['visitas']+=1;
inside the PHP file that ajax calls– Sergio
@Sergio, I already do this in PHP, the problem is that sending by Ajax to each request this is not updated on the page.
– waghcwb
@Jader, I added the codes. If I just reload the form element with a new AJAX request the $_SESSION data is updated?
– waghcwb
@waghcwb if everything is all right with cookies (which pass the Session ID) or the Session ID is passed correctly by the url (php usually does this automagically) should work yes...
– Jader A. Wagner