Captcha validation with javascript?

Status
Not open for further replies.

Keef

Ars Tribunus Militum
2,094
Is there any generic code out there for this? I'm working on a site that already uses securimage from http://www.phpcaptcha.org/. For this particular page all the entry fields except the captcha code are validated through js. Problem is, if the captcha is wrong, all the fields they filled out are lost and they have to do everything over again. Is what I'm asking for a bad idea, and if so is there an alternative way of doing it in php?
 

OldManBrodie

Ars Legatus Legionis
44,321
Subscriptor
My PHP is a bit rusty, but can't you just have the inputs prepopulate with posted data? Something like this:

Code:
<input type='text' name='txtInput1' value='$_POST[txtInput1]' />

That way, if they're loading the page from scratch, it'll prepopulate it with nothing, but if it's posting back to itself, the values will still be there.
 

Keef

Ars Tribunus Militum
2,094
As the code is right now, there are 2 pages that handle the input.
The first page where the user types in the info,
Code:
<form id="foofield" name="foofield" method="post" action="2ndpage.php" onsubmit='return validateMyForm(this)'>
<input name="some_foo" type="text" id="some_foo" maxlength="50" />
which is validated by a .js function. All that really does it make sure the user is putting something in those fields if required and if they're formatted correctly. The .js also looks at the captcha, just to make sure that something, anything was written in before the page is allowed to be submitted.

The second is where the captcha input is actually validated against the code. Its at the top of a nested if statement. Inside, where the rest of the input I want to the DB looks like
Code:
$foo = $sql_session->real_escape_string($_POST['some_foo']);
so I guess my question is, how do I make it so the user doesn't have to type 'foo' all over again when they get kicked back to the first page when they flub the captcha.

Or could it be done/would it be a bad idea to let the .js handle this?
 

andrewme

Senator
7,120
Subscriptor
brodie":2hqvphe4 said:
My PHP is a bit rusty, but can't you just have the inputs prepopulate with posted data? Something like this:

Code:
<input type='text' name='txtInput1' value='$_POST[txtInput1]' />

That way, if they're loading the page from scratch, it'll prepopulate it with nothing, but if it's posting back to itself, the values will still be there.

That. No session storage needed. Although you do need to escape the values for safe insertion into HTML to avoid XSS (and page breakage if the someone non-maliciously inputs HTML).
 

Keef

Ars Tribunus Militum
2,094
andrewme":25yct25f said:
Keef":25yct25f said:
Oh yeah, I googled PHP_SELF and it seems like a few pages don't have nice things to say about it from a security standpoint.

No need to use PHP_SELF. Just set your form's "action" attribute to "?" and the browser will automatically submit the form to the same URL it was loaded from.
Code:
<form id="resident" name="resident" method="post" action='?' onsubmit='return validateMyForm(this)'>
...
doesn't work for me, just refreshes the page, maybe my syntax is wrong?
 
Status
Not open for further replies.