Required form fields

Hi, very new to PHP coding and struggling with this. I have several PHP forms on a site that need certain fields that are required, usual stuff, name, email address, etc. The form I used has code included in the head that I believe I needs to be adapted to include this function, but as every form script I look at is slightly different I’ve found taking snippets from another to use to this end just doesn’t work. Any assistance greatly appreciated, many thanks.

Code (I believe) I need to modify:

<script> $(function(){ $('#towbar-enquiry-form').validate({ submitHandler: function(form) { $(form).ajaxSubmit({ url: 'process.php', success: function() { $('#towbar-enquiry-form').hide(); $('#contact-form').append("<p class='thanks'>Thank you! Your tow bar fitting enquiry has been sent. We will be in touch shortly.</p>") } }); } }); }); </script>

What is in process.php?

You can do the validation with js though, lots of examples out there. but simple php validation would be something like
[php]
if(!empty($_POST[‘email’)) {
// character validation here
} else {
$err[‘email’] = “Required Field”;
}

if(!$err) {
// sucess message
} else {
$err[‘all’] = “Please check the form for Required Fields”;
}[/php]

Many thanks for the reply.

From what I’d read I believed Javascript validation should be the way to go (possibly in conjunction with PHP validation) so it could be done without reloading the page(?). I checked out several examples, just couldn’t successfully integrate them in to what I already had.

process.php contains the following

[code]<?php
// Get Data
$regno = strip_tags($_POST[‘regno’],“Please enter your Registration Number!”);
$towbartype = strip_tags($_POST[‘towbartype’]);
$towingelectrics = strip_tags($_POST[‘towingelectrics’]);
$wiringkits = strip_tags($_POST[‘wiringkits’]);
$parkingsensors = strip_tags($_POST[‘parkingsensors’]);
$towing = strip_tags($_POST[‘towing’]);
$name = strip_tags($_POST[‘name’],“Please enter your Name!”);
$email = strip_tags($_POST[‘email’],“Please enter your email address!”);
$telephone = strip_tags($_POST[‘telephone’]);
$postcode = strip_tags($_POST[‘postcode’]);

// Send Message
mail( "[email protected]", “Tow Bar Fitting Enquiry”,
“Registration No: $regno\nTow Bar Type: $towbartype\nTowing Electrics: $towingelectrics\nWiring Kits: $wiringkits\nParking Sensors: $parkingsensors\nTowing: $towing\nName: $name\nEmail: $email\nTelephone: $telephone\nPostcode: $postcode\n”,
“From: Trident Towing Tow Bar Fitting Service [email protected]” );
?>[/code]

Well, your process.php is just an email sending code. Looks correct…

Normally, you use Javascript validation if you want to check out entries BEFORE posting it to the PHP processing page. Why? So that you do not waste time jumping to a PHP page and then back to the posting page when a user, let’s say, forgets to enter his/her name. It basically saves a ton of time.

So, yes, you would want to validate entries browser-side. But, you didn’t actually tell us what is not working. I like my PHP, so I usually do it Richei’s way. Either will work.

But, you didn’t actually say what “required form fields” you were having problems with. Not sure what you need help with.

Sponsor our Newsletter | Privacy Policy | Terms of Service