Form field validation against a MySQL...

User 2736027 Photo


Registered User
68 posts

Yup.... fully aware. But I know that Eric is still very active, and the original solution was his creation. I was hoping his memory cells were still active also, and "we" could give it another go.

If not, I could submit another request? :cool:

Thanks...
User 187934 Photo


Senior Advisor
20,193 posts

Hi Norm,
I looked and the old form needed some updates do to the age.
Seeing your actual form will make it easier to help.;)
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2736027 Photo


Registered User
68 posts

Hi Eric...
Thanks for jumping back on the train!! :D

I don't have a form yet, I am trying to wrap my mind around how I should proceed first.

The scenario is this: Our hobby club has about 100 members. I already have a form to collect names/addresses, etc to create a new membership, and that works quite well.

Each year members will renew their membership, and we collect their annual dues. So, my initial thought might be to first create a simple 'qualification' form to select "is this a Renewal, or New Membership" Radio buttons AND request EITHER their first and last name, OR their email address.

If the selection was for a New Membership, then branch to my existing form, while passing the SESSION variables to my current form.

If the selection was for a Renewal, then branch to a yet to be created renewal form (or, I may even be able to re-use my existing "new membership" form). In this case the SESSION variables would be used to look up a MySQL database, and load the member's information into the renewal form for verification. If any changes needed to be made, then I could over-write the correct details, and update the member's profile.

That's why when I saw your 'old' validation script, I thought perhaps this would be a good place to start.

Any guidance on how to proceed would be appreciated. I am okay with HTML and PHP, but not so much with javascript.

Norm
User 187934 Photo


Senior Advisor
20,193 posts

Here's redo on first and last name validation
My first and last name is the only one in the database.
https://ericrohloff.com/coffeecup/ccfor … irst_last/
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2736027 Photo


Registered User
68 posts

Hi Eric,

That looks great! I tested the form with two different names. When I retried with my name (and yours) which had been stored, it confirmed that the database already had such a record. That's what I need, exactly!

I would be happy to look at your redo, and to integrate it into my process!! A big Thanks for doing this!! :D

Regards,
Norm
User 187934 Photo


Senior Advisor
20,193 posts

Put this code near the bottom of your form. I did mine after export and placed it just above the </body> tag.
Adjust the first_name and last_name to your actual input names if different.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
var jQ = $.noConflict(true);
jQ(document).ready(function() {
jQ('#fb-submit-button').attr("disabled", true);

jQ('input[name="first_name"], input[name="last_name"]').on('change', function() {
var first_name = jQ('input[name="first_name"]').val();
var last_name = jQ('input[name="last_name"]').val();
var errorInfo = jQ("#errorInfo");

if (first_name != '' & last_name != '') {

jQ.ajax({

type: "POST",
data: {
'first_name': first_name,
'last_name': last_name
},
url: "formcheck_first_last.php",
beforeSend: function() {
errorInfo.html("<font color='blue'>Checking for member...</font>");
},
success: function(data) {

if (data != "0") {

errorInfo.html("<font color='red'>Name already exists.</font>");
jQ('#fb-submit-button').attr("disabled", true);
} else {
errorInfo.html("<font color='green'>Name doesn't exist.</font>");
jQ('#fb-submit-button').attr("disabled", false);
}
}
});

}
});
});
</script>

Add this code to your form where you want the error message to appear.
<div id="errorInfo" align="left"></div>

Take this code and create a .php page named form_check_first_last.php . Save it to the same location that your form html page is actually at. Adjust the first_name and last_name to your actual input names. Also adjust the validation to your actual mysql table name.
<?php
require_once('pdo_connect.php');
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
if($first_name && $last_name)
{
$sql= "SELECT first_name, last_name FROM validation where first_name='$first_name' AND last_name='$last_name'";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$msg = $stmt->rowCount();
}
echo $msg;
?>

Take the code below and create a .php page named pdo_connect.php and save it to the same location that your form html page is at. Adjust the YourDataBaseName, YourUserName, YourPassword to your correct credentials.
<?php
$database = 'mysql: myHost =localhost; dbname=YourDataBaseName;charset=utf8';
$username = 'YourUserNmae';
$password = 'YourPassword';
try {
$pdo = new PDO($database, $username, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>

Helpful hint.
Create your form and submit it before you alter it. This way it will create your table and data structure.
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2736027 Photo


Registered User
68 posts

Many thanks, Eric!! Now, I have a project to work on in the next day or so!

I will let you know how I make out...

Norm
User 2736027 Photo


Registered User
68 posts

Works perfectly !!

I installed your script as suggested, and I can now successfully do a look-up to my MySQL database and verify if a member's record is present (or, not). Of course, I just needed to substitute my name variables, but that was all.

I have what it takes now, to complete my 'renewal' form, so a big Thanks Eric, for your contribution. I really appreciate your willingness to help.

One last question... Since I added the script manually to the form, this step will be required should I modify the form using WFB program. Could inserting an HTML object "somewhere" in the form eliminate that extra process?

Regards,
Norm

User 187934 Photo


Senior Advisor
20,193 posts

Thanks Norm and you are very welcome. Yes you can use a html element to insert the js script near the bottom of your form in the form builder application itself.
You can also do the same to insert the error info html div.
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2736027 Photo


Registered User
68 posts

Thanks again, Eric...

I inserted the scripts using HTML objects, and I am happy to see that they work as you recommended. Now, no more patching by hand. Outstanding!

Regards,
Norm

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.