Jump to content
Sign in to follow this  
kuci

Formularz kontaktowy PHP + Recaptcha + Upload pliku

Recommended Posts

Cześć, macie może jakiś formularz w który będzie wysyłał takie elementy jak:

 

Imie i Nazwisko, Mail, Temat Wiadomości, Treść Wiadomośći

 

Dodatkowo:

Recaptcha oraz możliwość dodania załącznika

 

Sam formularz kontaktowy to mam, z recaptcha to jeszcze jest tutaj:

https://github.com/jonmbake/bootstrap3-contact-form

Wstaw inne media

ale jeszcze potrzeba mi upload pliku.

 

Za pomoc, odpowiedzi nagradzam a ewentualnego zbawce opłacę.  POZDRO

Share this post


Link to post
4 godziny temu, TrzyRazyZero napisał:

 

Ugryzłem to z innej strony,

skorzystałem z tego lecz, do tego upload

 

wygląda to tak:

index.html

Spoiler

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="A contact form using the Bootstrap 3 framework.">
    <meta name="author" content="">
    <link rel="shortcut icon" href="../../assets/ico/favicon.png">
    <title>Bootstrap 3 Contact Form</title>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" >
    <link rel="stylesheet" href="assets/vender/intl-tel-input/css/intlTelInput.css">
    <!-- EXTRACT ADDITIONAL STYLING HERE =======> -->
    <style>
      .container {
        width: auto;
        max-width: 800px;
      }
      .form-group {
        margin-bottom: 8px;
      }
      #feedbackForm {
        font-size: 12px;
      }
    </style>
    <!-- <======= UP TO HERE -->
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="assets/vender/bootstrap/assets/js/html5shiv.js"></script>
      <script src="assets/vender/bootstrap/assets/js/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <!-- EXTRACT FORM HERE =======> -->
    <div class="container">
      <div id="contact_form" class="row">
        <div class="col-md-12">
          <h2>Contact Us</h2>
          <form role="form" id="feedbackForm"  data-toggle="validator" data-disable="false" enctype="multipart/form-data">
            <div class="form-group">
              <label class="control-label" for="name">Name *</label>
              <div class="input-group">
                <input type="text" class="form-control" id="name" name="name" placeholder="Enter your name" required/>
                <span class="input-group-addon"><i class="glyphicon glyphicon-unchecked form-control-feedback"></i></span>
              </div>
              <span class="help-block" style="display: none;">Please enter your name.</span>
            </div>
            <!-- UNCOMMENT HERE IF YOU WANT TITLE, COMPANY, WEBSITE OR PHONE FIELDS - you must also uncomment values in $fields_req in sendmail.php
            <div class="form-group">
              <label class="control-label" for="title">Title</label>
              <input type="text" class="form-control optional" id="title" name="title" />
            </div>
            <div class="form-group">
              <label class="control-label" for="company">Company</label>
              <input type="text" class="form-control optional" id="company" name="company" />
            </div>
            <div class="form-group">
              <label class="control-label" for="website">Website</label>
              <input type="url" class="form-control optional" id="website" name="website" />
            </div>
            <div class="form-group">
              <label class="control-label" for="phone">Phone</label>
              <input type="tel" class="form-control optional" id="phone" name="phone" placeholder="Enter your phone (Optional)"/>
              <span class="help-block" style="display: none;">Please enter a valid phone number.</span>
            </div>
            -->
            <div class="form-group">
              <label class="control-label" for="reason">Reason for Contact *</label>
              <div class="input-group">
              <input type="text" class="form-control" id="reason" name="reason" placeholder="Enter your reason" required/>
              <span class="input-group-addon"><i class="glyphicon glyphicon-unchecked form-control-feedback"></i></span>
              </div>
              <span class="help-block" style="display: none;">Please enter a valid e-mail address.</span>
            </div>
            <div class="form-group">
              <label class="control-label" for="email">Email Address *</label>
              <div class="input-group">
                <input type="email" class="form-control" id="email" name="email" placeholder="Enter your email" required/>
                <span class="input-group-addon"><i class="glyphicon glyphicon-unchecked form-control-feedback"></i></span>
              </div>
              <span class="help-block" style="display: none;">Please enter a valid e-mail address.</span>
            </div>
            <div class="form-group">
              <label class="control-label" for="message">Message *</label>
              <div class="input-group">
                <textarea rows="5" cols="30" class="form-control" id="message" name="message" placeholder="Enter your message" required></textarea>
                <span class="input-group-addon"><i class="glyphicon glyphicon-unchecked form-control-feedback"></i></span>
              </div>
              <span class="help-block" style="display: none;">Please enter a message.</span>
            </div>
            <div class="form-group">
              <div class="g-recaptcha" data-sitekey="6Lfc1BcUAAAAAGi6v62pFyBNWIKrxPdCrPh7EeWG"></div>
              <span class="help-block" style="display: none;">Please check that you are not a robot.</span>
            </div>
            <span class="help-block" style="display: none;">Please enter a the security code.</span>
            <div class="form-group">
           <label class="control-label" for="attach">Załącznik</label>
          <input type="file" id="attach" class="optional" name="attach" />
            </div>
            <button type="submit" id="feedbackSubmit" class="btn btn-primary btn-lg" data-loading-text="Sending..." style="display: block; margin-top: 10px;">Send Feedback</button>

          </form>
        </div><!--/span-->
      </div><!--/row-->
      <hr>
    </div><!--/.container-->
    <!-- <======= UP TO HERE -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    <script src="assets/vender/intl-tel-input/js/intlTelInput.min.js"></script>
    <script src="assets/js/contact-form.js"></script>
    <script src="https://www.google.com/recaptcha/api.js"></script>
  </body>
</html>

 

 

library/sendmail.php

Spoiler

<?php
  /**
   * Sets error header and json error message response.
   *
   * @param  String $messsage error message of response
   * @return void
   */
  function errorResponse ($messsage) {
    header('HTTP/1.1 500 Internal Server Error');
    die(json_encode(array('message' => $messsage)));
  }

  /**
   * Pulls posted values for all fields in $fields_req array.
   * If a required field does not have a value, an error response is given.
   */
  function constructMessageBody () {
    $fields_req =  array("name" => true, "email" => true, "message" => true);
    $message_body = "";
    foreach ($fields_req as $name => $required) {
      $postedValue = $_POST[$name];
      if ($required && empty($postedValue)) {
        errorResponse("$name is empty.");
      } else {
        $message_body .= ucfirst($name) . ":  " . $postedValue . "\n";
      }
    }
    return $message_body;
  }

  header('Content-type: application/json');

  //do Captcha check, make sure the submitter is not a robot:)...
  $captcha_url = 'https://www.google.com/recaptcha/api/siteverify';
  $captcha_header = 'Content-type: application/x-www-form-urlencoded';
  $captcha_post_data = http_build_query(array('secret' => '6Lfc1BcUAAAAAEEslaSrZHY7drPgKdsuX19j8rTS', 'response' => $_POST["g-recaptcha-response"]));
  // prefer cURL over #file_get_contents...
  if (function_exists('curl_init')) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $captcha_url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($captcha_header));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $captcha_post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = json_decode(curl_exec($ch));
    curl_close($ch);
  } else {
    $opts = array('http' =>
      array(
        'method'  => 'POST',
        'header'  => $captcha_header,
        'content' => $captcha_post_data
      )
    );
    $context  = stream_context_create($opts);
    $result = json_decode(file_get_contents($captcha_url, false, $context, -1, 40000));
  }


  if (!$result->success) {
    errorResponse('reCAPTCHA checked failed! Error codes: ' . join(', ', $result->{"error-codes"}));
  }
  //attempt to send email
  $messageBody = constructMessageBody();
  require './vender/php_mailer/PHPMailerAutoload.php';
  $mail = new PHPMailer;
  $mail->CharSet = 'UTF-8';
 

$mail->Sender = 'mail@gmail.com';
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->addAddress('mail@gmail.com');

  $mail->Subject = $_POST['reason'];
  $mail->Body  = $messageBody;
if (isset($_FILES['attach']) && $_FILES['attach']['error'] == UPLOAD_ERR_OK)
{
    $mail->AddAttachment($_FILES['attach']['tmp_name'], $_FILES['attach']['name']);
}

  //try to send the message
  if($mail->send()) {
    echo json_encode(array('message' => 'Your message was successfully submitted.'));
  } else {
    errorResponse('An unexpected error occured while attempting to send the email: ' . $mail->ErrorInfo);
  }
?>

 

 

Wszystko pięknie działa, recaptcha smiga, dodaje załącznik wysyłam ALE ZAŁĄCZNIK NIE POJAWIA SIĘ W MAILU. co tu robić..

  • Like 1

Share this post


Link to post

A co zwraca ta linijka?

<?php
$mail->AddAttachment($_FILES['attach']['tmp_name'], $_FILES['attach']['name']);

Sprawdź.

Share this post


Link to post
56 minut temu, Salva napisał:

A co zwraca ta linijka?


<?php
$mail->AddAttachment($_FILES['attach']['tmp_name'], $_FILES['attach']['name']);

Sprawdź.

tylko jak

Share this post


Link to post

Dobra, zreyzgnowałem z AJAXA po prostu  wywołałem to przez PHP wysyła działa, jak wdrążę projekt to wrzucę ten kod niech ludzie mają.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Create a New Account. It’s free and easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×
×
  • Create New...