vendredi 31 juillet 2020

Add checkboxes to php contact form

I would like to add checkboxes to a contact form, but not getting it to work. How do i get it to send in the email?

I have this form from a previous contact form and would like to adapt it so that it handles checkboxes which are sent to the email address. Is there a way to change the php to process which items are selected and which are not?

Html

      <form class="contact-page" method="post" action="/mail.php">
      <div class="form-group">
          <input name="name" type="text" class="form-control" placeholder="Name" required>
      </div>
      <div class="form-group">
          <input name="email" type="email" class="form-control" placeholder="Email" 
      required>
      </div>
      <div class="form-group">
          <input name="phone" type="text" class="form-control" placeholder="Phone" required>
      </div>
      <input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
      <label for="vehicle1"> I have a bike</label><br>
      <input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
      <label for="vehicle2"> I have a car</label><br>
      <input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
      <label for="vehicle3"> I have a boat</label><br><br>
      <div class="form-group">
          <textarea name="message" class="form-control" rows="6" style="height: 150px;" 
       placeholder="Message" required></textarea>
      </div>
      <div class="form-group form-check">
          <input type="checkbox" class="form-check-input" id="exampleCheck1">
          <label class="form-check-label" for="exampleCheck1" required>I confirm that ???? 
       can store my details</label>
      </div>
     <input type="submit" class="btn btn-light" value="Submit">
      </form>

PHP

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    # FIX: Replace this email with recipient email
    $mail_to = "test@test.com";

    # Sender Data
    $name = str_replace(array("\r","\n"),array(" "," ") , strip_tags(trim($_POST["name"])));
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $phone = trim($_POST["phone"]);
    $message = trim($_POST["message"]);

    if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($phone) OR 
    empty($message)) {
        # Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo '<p class="alert alert-warning">Please complete the form and try again.</p>';
        exit;
    }

    $ip = $_SERVER['REMOTE_ADDR'];
    $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify? 
    secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
    $responseKeys = json_decode($response,true);

    if(intval($responseKeys["success"]) !== 1) {
      echo '<p class="alert alert-warning">Please check the the captcha form.</p>';
    } else {
        
        $subject = 'Enquiry From Website ' . $_SERVER['HTTP_HOST'];
        # Mail Content
        $content = "Name: $name\n";
        $content .= "Email: $email\n\n";
        $content .= "Phone: $phone\n";
        $content .= "Message:\n$message\n";


        $body .= 'Selected Projects: ' . $selectedProjects;
        # email headers.
        $headers = "From: $name <$email>";

        # Send the email.
        $success = mail($mail_to, $subject, $content, $headers);
        header('Location: thank-you');
        if ($success) {
        header('Location: thank-you');
        } else {
            # Set a 500 (internal server error) response code.
            http_response_code(500);
            echo '<p class="alert alert-warning">Oops! Something went wrong, we couldnt send 
     your message.</p>';
        }
    }

    } else {
    # Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo '<p class="alert alert-warning">There was a problem with your submission, please 
    try again.</p>';
    }

     ?>



Aucun commentaire:

Enregistrer un commentaire