Topic: Contact form status returns different if successful or invalid validation

tommyhutcheson free asked 6 years ago


Hello I have setup a contact form using the below link: https://mdbootstrap.com/components/bootstrap-contact-form/     I would like to add the checkbox validation option. I am quite new to this and am not sure whether to but the PHP code presented in the tutorial. I have added the HTML:
<div class="form-check">
    <input type="checkbox" class="form-check-input" name="updates" id="updates" value="1">
    <label for="updates" class="form-check-label">Notify me about new updates</label>
</div>

and the JS line:

formData = {
    'name'     : $('input[name=name]').val(),
    'email'    : $('input[name=email]').val(),
    'subject'  : $('input[name=subject]').val(),
    'message'  : $('textarea[name=message]').val(),
    'updates'  : $('input:checkbox[name=updates]').is(':checked')
};

but the PHP code I am unsure about, I also want it to display an error message if the checkbox isn't checked but it doesn't look like this will do it by default ?

I wasnt sure if you could combine the:

if ($message === ''){
 print json_encode(array('message' => 'Message cannot be empty', 'code' => 0));
 exit();
}

with the code provided :


if(isset( $_POST['updates']))
 $updates = $_POST['updates'];


my first attempt was this below, adding the $updates = $_POST['updates']; to the top of the php and then adding the
if(isset( $_POST['updates']))
 $updates = $_POST['updates']; 

after the message if statement.


<?php $name = $_POST['name']; $surname = $_POST['surname']; $email = $_POST['email']; $message = $_POST['message']; $subject = $_POST['subject']; $updates = $_POST['updates']; header('Content-Type: application/json'); if ($name === ''){ print json_encode(array('message' => 'Name cannot be empty', 'code' => 0)); exit(); } if ($surname === ''){ print json_encode(array('message' => 'Surname cannot be empty', 'code' => 0)); exit(); } if ($email === ''){ print json_encode(array('message' => 'Email cannot be empty', 'code' => 0)); exit(); } else { if (!filter_var($email, FILTER_VALIDATE_EMAIL)){ print json_encode(array('message' => 'Email format invalid.', 'code' => 0)); exit(); } } if ($subject === ''){ print json_encode(array('message' => 'Subject cannot be empty', 'code' => 0)); exit(); } if ($message === ''){ print json_encode(array('message' => 'Message cannot be empty', 'code' => 0)); exit(); } if(isset( $_POST['updates'])) $updates = $_POST['updates']; $content="From: $name nSurname: $surname nEmail: $email nMessage: $message"; $recipient = "tommyhutcheson@gmail.com"; $mailheader = "From: $email rn"; mail($recipient, $subject, $content, $mailheader) or die("Error!"); print json_encode(array('message' => 'Email successfully sent!', 'code' => 1)); exit(); ?>

Thank you 

Tommy

     

Dawid Adach pro answered 6 years ago


Dear Tommy,
    'updates'  : $('input:checkbox[name=updates]').is(':checked')
Will send only checked checkboxes. Therefore if customer select it, you will get it on PHP side, if not - this variable will be empty so you can check it and show warning accordingly. Dawid

tommyhutcheson free commented 6 years ago

Okay, I thought the JS was pretty straightforwards, however I am still struggling :( when I add the check box line : $('input:checkbox[name=updates]').is(':checked') It stops the email form from working and no validation comes through. Then after I get that straightforward bit working I am unsur eon what I add in the PHP to get the validation to display the checkbox is required if not checked. I gather this code below is needed but what line should it go on? (isset( $_POST['updates'])) $updates = $_POST['updates']; I had thought I may require something similar to this: if ($updates === ''){ print json_encode(array('message' => 'Check box required', 'code' => 0)); exit(); } But I am not sure, do you have a completed email form with validation available I may need it to advance ? though I am not far off as my email form works fine it just doesn't validate on the checkbox. I appreciate the support and I am sorry I am such a noob. Tommy

Dawid Adach pro commented 6 years ago

Dear Tommy, please use following function: var_dump($_POST); in your php file to see what is beeing sent from JS > PHP. You can also open a network tab i Developers tools of your browser and see request/response there.

tommyhutcheson free commented 6 years ago

Hi Dawid Thank you for the quick reply I have added the var_dump($)POST though I am not sure where it displays. I have opened the developer tab and noticed that when I press the send button I receive an error in the console: email.js:37 Uncaught SyntaxError: Unexpected string contact.html:224 Uncaught ReferenceError: validateForm is not defined at HTMLAnchorElement.onclick (contact.html:224) onclick @ contact.html:224 I will try and host my contact form and send you the link as I am sure that will help. Regards Tommy

tommyhutcheson free commented 6 years ago

That didnt take as long as I thought it might, here is the email form I am working on https://www.mapitude.uk/contact.html

Dawid Adach pro commented 6 years ago

It clearly says that you are trying to call function: validateForm() which doesn't exist. Make sure that it exist or that js file where it is present is beeing imported correctly. Yo also have some broken imports on the website which should be fixed.


Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: General Bootstrap questions
  • MDB Version: -
  • Device: -
  • Browser: -
  • OS: -
  • Provided sample code: No
  • Provided link: No
Tags