I have been trying for days to get this contact form working on my site. I have rewritten it several times and tried many different things over the days. I just now tried putting it into a class structure. I am a beginner at best with php
class form { function validationFalse(){ echo '<div id="sentMessage">There was a problem. Please check your form and try again.</div>'; return false; }
function startForm(){ //get the wordpress database information global $data; //if the submit button is pressed on the form if(isset($_POST['sendMessage'])){ $contactName = strip_tags($_POST['contactName']); if( preg_match('/^[a-zA-Z ]+$/', $contactName) == 'false') { //if the contact name doesn't validate, call the error function $this->validationFalse(); } $surname = strip_tags($_POST['surname']); $email = $_POST['contactEmail']; $city = strip_tags($_POST['contactCity']); $messages = strip_tags($_POST['message']); //check to see if the variables are empty, if they are, just exit
the problem is that the validationFalse function is suppose to be called if the contactName does not match the preg_match statement. The function does get called but the rest of the code runs as well and it is not suppose to. So when you pres submit, the form will send, you will see the success message and also the error message. It should not do that.
Using the negation operator (!) in front of the preg_match() function will effectively accomplish what you are trying right now...the issue is preg_match() wont return a true or false STRING...which it seems you are checking for, hence, it is returning true and completing the rest of the class functions.
It might look a little chopped up. Can you help me clean it up and get it working?
right now, it runs all functions when I press the submit button. Still calls the error message, and still sends the form and gives the success message
To this line instead:
Using the negation operator (!) in front of the
preg_match()function will effectively accomplish what you are trying right now...the issue ispreg_match()wont return a true or false STRING...which it seems you are checking for, hence, it is returning true and completing the rest of the class functions.validationFalse()method won't stop yourstartForm()method. You have to put in thestartForm()method.