I'm a newbie to this, and I'm sorry if this is a horrifying question, but I'm trying to add a function to one of the fields in the form.
I have basically done everything through the phpFormGenerator, uploaded the script to my webserver and everything works like a charm. But I would like to add a control check to one the text fields in it. I found a code for this on the web, and its supposed to check if the user submits a correct Swedish personal number or not, containing 10 digits. So if the user submits a correct personal number there shouldn't be any problem by submitting the form, but if it's not a correct personal number it should display an error message, like "Wrong personal number!".
<?
function checkPnr($pnr)
{
if (strlen($pnr) != 10) die("Wrong personal number! Submit personal number without a dash, 10 digits in total.");
Yes this is something you can easily add to your form. The processor.php file is where you would add this code. Putting the code near the top ensures that nothing else occurs before the check is validated. Any additional help you need would require much more detail on specifics of your form and how you want to handle the notification that a code passes or fails.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for your quick and accurate reply! It worked. I.e. no error messages.
But I want it to be tied to a certain field. Fieldbox_2 in this case. And if its a correct personal number (true) it should go to confirm.html and a thank-you-message. Otherwise (false) it could use the alert function "Please correct the errors. Fields marked with an asterisk (*) are required" - (just to make it as easy as possible.)
If this is to much programming, or too difficult to accomplish in general, I need to find another way. But Im very happy about any help I can get.
I'm a newbie to this, and I'm sorry if this is a horrifying question, but I'm trying to add a function to one of the fields in the form.
I have basically done everything through the phpFormGenerator, uploaded the script to my webserver and everything works like a charm. But I would like to add a control check to one the text fields in it. I found a code for this on the web, and its supposed to check if the user submits a correct Swedish personal number or not, containing 10 digits. So if the user submits a correct personal number there shouldn't be any problem by submitting the form, but if it's not a correct personal number it should display an error message, like "Wrong personal number!".
<?
function checkPnr($pnr)
{
if (strlen($pnr) != 10) die("Wrong personal number! Submit personal number without a dash, 10 digits in total.");
$n = 2;
// Calculate the checksum
for ($i=0; $i<9; $i++)
{
$tmp = $pnr[$i] * $n;
($tmp > 9) ? $sum += 1 + ($tmp % 10) : $sum += $tmp;
($n == 2) ? $n = 1 : $n = 2;
}
// Add the last digit (checksum), the result should be still tens, return true/false
return !( ($sum + $pnr[9]) % 10);
}
if (checkPnr("7410291234"))
{
echo "Correct personal number!";
}
else
{
echo "Wrong personal number!";
}
?>
The questions is if this is easy to add to my existing form or not? And if so, how and where can I add this code?
Any help is much appreciated.
Yes this is something you can easily add to your form. The processor.php file is where you would add this code. Putting the code near the top ensures that nothing else occurs before the check is validated. Any additional help you need would require much more detail on specifics of your form and how you want to handle the notification that a code passes or fails.
Thanks for your quick and accurate reply! It worked. I.e. no error messages.
But I want it to be tied to a certain field. Fieldbox_2 in this case. And if its a correct personal number (true) it should go to confirm.html and a thank-you-message. Otherwise (false) it could use the alert function "Please correct the errors. Fields marked with an asterisk (*) are required" - (just to make it as easy as possible.)
If this is to much programming, or too difficult to accomplish in general, I need to find another way. But Im very happy about any help I can get.
Here is my processor.php
<?php
$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
// Checkbox handling
function checkPnr($pnr)
{
if (strlen($pnr) != 10) die("Wrong personal number! Submit personal number without a dash, 10 digits in total.");
$n = 2;
// Calculate the checksum
for ($i=0; $i<9; $i++)
{
$tmp = $pnr[$i] * $n;
($tmp > 9) ? $sum += 1 + ($tmp % 10) : $sum += $tmp;
($n == 2) ? $n = 1 : $n = 2;
}
// Add the last digit (checksum), the result should be still tens, return true/false
return !( ($sum + $pnr[9]) % 10);
}
if (checkPnr("7410291234"))
{
echo "Correct personal number!";
}
else
{
echo "Wrong personal number!";
}
$field_4_opts = $_POST['field_4'][0];
include("config.inc.php");
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
$query = "INSERT into `".$db_table."` (field_1,field_2,field_3,field_4) VALUES ('" . $_POST['field_1'] . "','" . $_POST['field_2'] . "','" . $_POST['field_3'] . "','" . $_POST['field_4'] . "')";
mysql_query($query);
mysql_close($link);
include("confirm.html");
?>
You'll find my form.html and confirm.html at goranjoelsson/se.form.form/html (replace / with . and vice versa)
Many thanks for your help.
Can you send me a number that should pass the check?
Ok, sent you a message just now.
Nothing yet.
Weird. I sent it to your sourceforge-address. Anyway, I can paste it in here, the numbers are made up.
Here is a number which should work: 0808080808
This one shouldn’t: 0808080807
And again, many thanks for your help!