Speicific character in text field with PHP

Hi, I want to setup a form, and use a text field which requires a specific character init - I was first advised to use javascript but bots will be able to by pass that… I am looking to see if there is a php coding that can do this?

This is what I am trying to achieve - 4 + 1 =

<input name=“mathSum” type=“text” id=“mathSum” size=“1” maxlength=“1” />

The answer will of course be 5. I want to do the field so only 5 can be accepted.

If any body has any guidance in this topic that would be fantastic.

Cheers,

Paul

Assuming your posting the form:


if(!isset($_POST['mathSum']) || $_POST['mathSum']!=5) {
    die("That's the wrong answer, try again!");
}

This code checks to see if mathSum has been posted and if the value is 5. If not, it stops the script and tells the user it’s wrong.

Thanks for the reply. That didn’t work, the form I am posting is just an HTML form in a PHP document, nothing complex, extremely simple stuff.

Can you post the form and the part of the code where you validate the form?

Here is the form:

<form name="formcheck" action="/enquiry.php" onsubmit="return formCheck(this);">
	<table align="center" border="0" cellpadding="8" cellspacing="0" width="50%">
		<tr>
		  <td align="left">Name:</td>
		  <td colspan="2" align="left"><input name="Name" type="text" id="Name" size="14" /></td>	
		</tr>
		<tr>
		  <td align="left">Email:</td>
		  <td colspan="2" align="left"><input name="Email" type="text" id="Email" size="14" /></td>
		</tr>
		<tr>
		  <td align="left" valign="top">Enquiry:</td>
		  <td colspan="2" align="left" valign="top"><textarea name="Enquiry" id="Enquiry" style="width:105px;height:75px;" rows="" cols=""></textarea></td>
	    </tr>
		<tr>
		  <td align="left" valign="top">Security:</td>
			<td align="right" valign="top">1 + 4 = <input name="Security" type="text" id="Security" size="1" maxlength="1" /></td>
		</tr>
        <tr>
			<td colspan="3" class="center">
			<div align="right">
			<input type="submit" value="Submit" />		
			<input type="reset" value="Reset" />	
			</div></td>	
		</tr>
	</table>
</form>

I have chnaged the mathSum to security. The form validates in javascript, so I am thinking maybe I should just do that to keep it consistent.

Ah, you’re not POSTing the form, but GETting it.

In the example I gave, try changing $_POST to $_GET. It might work then.

Also, field named “Security”, not mathsum

I know, I changed it accordingly - I am probably going to go for a bit of javascript. It’s not something that is overly important :slight_smile: Thank you both for your help.