IT rekvalifikace s garancí práce. Seniorní programátoři vydělávají až 160 000 Kč/měsíc a rekvalifikace je prvním krokem. Zjisti, jak na to!
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.
Avatar
Petr Vít
Člen
Avatar
Petr Vít:8.12.2017 12:41

Zdravim, potřeboval bych poradit. Jelikož nevládnu PHP jazykem tak jsem si stahnul již vytvořenou šablonu pro odesilani formulare z webu na email. Vse slape v poradku. Formular má v zakladu jen promeny name,email a message. Já bych rád pridal jeste phone. Akorat jakmile se snazim to do kodu natlacit tak prestane fungovat. Muzete se na to nekdo podivat a rict mi kde delam chybu?

<?php
// Check for empty fields
if(empty($_POST['name'])                ||
   empty($_POST['email'])               ||
   empty($_POST['message'])     ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
        echo "No arguments Provided!";
        return false;
   }

$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];

// Create the email and send the message
$to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>

Do if podminky pridavam

empty($_POST['phone']) ||

Pod podminku dam

$phone = $_POST['phone'];

Do $email_body pridam :\n\nPhone: $phone

HTML Kod vypada takto

<form name="sentMessage" id="contactForm" novalidate>
        <div class="row">
          <div class="col-md-6">
            <div class="form-group">
              <input type="text" id="name" class="form-control" placeholder="Name" required="required">
              <p class="help-block text-danger"></p>
            </div>
          </div>
          <div class="col-md-6">
            <div class="form-group">
              <input type="email" id="email" class="form-control" placeholder="Email" required="required">
              <p class="help-block text-danger"></p>
            </div>



          </div>
        </div>
        <div class="form-group">
          <textarea name="message" id="message" class="form-control" rows="4" placeholder="Message" required></textarea>
          <p class="help-block text-danger"></p>
        </div>
        <div id="success"></div>
        <button type="submit" class="btn btn-default">Poslat zprávu</button>
      </form>

Kde pridam

<div class="form-group">
              <input type="tel" id="phonel" class="form-control" placeholder="Phone" required="required">
              <p class="help-block text-danger"></p>
            </div>

Avsak po pridani kódů uz to nefunguje.

Diky za vsechny rady

 
Odpovědět
8.12.2017 12:41
Avatar
ConflictBoy
Člen
Avatar
Odpovídá na Petr Vít
ConflictBoy:8.12.2017 13:05

A nemáš v tom kódu ještě náhodou někde vložený javascript? :)

Nahoru Odpovědět
8.12.2017 13:05
I'm programmer, I have no life :)
Avatar
Peter Sciranka
Tvůrce
Avatar
Odpovídá na Petr Vít
Peter Sciranka:8.12.2017 13:08

Ahoj, prezrel som to len v rýchlosti a v poslednom kóde máš id="phone1", skús dať len id= phone" :)

Editováno 8.12.2017 13:10
Nahoru Odpovědět
8.12.2017 13:08
Act as if it was Impossible to Fail
Avatar
Peter Sciranka
Tvůrce
Avatar
Odpovídá na Petr Vít
Peter Sciranka:8.12.2017 13:19

Zle som to napísal, tak ešte raz :)
Kód, ktorý pridávaš tak v tagu <input> máš id="phone1", skús to zmeniť na id="phone".
Pretože tento id potom zisťuješ v kóde

empty($_POST['phone']);
$phone = $_POST['phone'];

Je nutné aby tieto položky boli rovnaké.

Nahoru Odpovědět
8.12.2017 13:19
Act as if it was Impossible to Fail
Avatar
Petr Vít
Člen
Avatar
Petr Vít:9.12.2017 9:41

Ten phone1 byl pouze preklep tady na foru, ale diky ;) .. Conflict boy mel pravdu je tam i JS.
Ted jsem vsechny kody prepsal a stejne to nechodi. Vidite tam nekdo chybu?

HTML

<form name="sentMessage" id="contactForm" novalidate>
        <div class="row">
          <div class="col-md-6">
            <div class="form-group">
              <input type="text" id="name" class="form-control" placeholder="Name" required="required">
              <p class="help-block text-danger"></p>
            </div>
          </div>
                  <div class="col-md-6">
            <div class="form-group">
              <input type="tel" id="phone" class="form-control" placeholder="Phone" required="required">
              <p class="help-block text-danger"></p>
            </div>
          </div>
          <div class="col-md-6">
            <div class="form-group">
              <input type="email" id="email" class="form-control" placeholder="Email" required="required">
              <p class="help-block text-danger"></p>
            </div>
          </div>
        </div>
        <div class="form-group">
          <textarea name="message" id="message" class="form-control" rows="4" placeholder="Message" required></textarea>
          <p class="help-block text-danger"></p>
        </div>
        <div id="success"></div>
        <button type="submit" class="btn btn-default">Poslat zprávu</button>
      </form>

PHP

<?php
// Check for empty fields
if(empty($_POST['name'])                ||
   empty($_POST['phone'])       ||
   empty($_POST['email'])               ||
   empty($_POST['message'])     ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
        echo "No arguments Provided!";
        return false;
   }

$name = $_POST['name'];
$phone = $_POST['phone'];
$email_address = $_POST['email'];
$message = $_POST['message'];

// Create the email and send the message
$to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\nPhone: $phone\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>

JavaScript

$(function() {

    $("input,textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var name = $("input#name").val();
                        var phone = $("input#phone").val();
            var email = $("input#email").val();
            var message = $("textarea#message").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "././mail/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                                        phone: phone,
                    email: email,
                    message: message
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
            })
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});
 
Nahoru Odpovědět
9.12.2017 9:41
Avatar
Neaktivní uživatel:11.12.2017 5:29

Nefunguje znamena co? Nejaka php chyba nebo js chyba? Kiukam na to a nevidim chybu.

Nahoru Odpovědět
11.12.2017 5:29
Neaktivní uživatelský účet
Avatar
ConflictBoy
Člen
Avatar
Odpovídá na Petr Vít
ConflictBoy:11.12.2017 12:59

Zkoušel jsem si tvůj kód a funguje :) I telefonní číslo došlo.
Nicméně pokud tím nefungováním myslíš to, že ti e-mail nedošel, možná je to tím, že to posíláš na G-mail. Ono posílání e-mailů je trochu věda, aby byl e-mail doručen, chce to mít ideálně DKIM, SPF a další věci, které vyhodnocují důvěryhodnost e-mailu. Google na důvěryhodnosti docela trvá, takže když odešleš několikrát prostý e-mail pomocí PHP funkce mail(), zřejmě to časem vyhodnotí jako spam. Někdy ten e-mail nedojde vůbec. Takže prvně se podívej do Spamu, jestli to neskončilo tam, pokud ne, zkus změnit e-mail třeba na Seznam.cz.

Nahoru Odpovědět
11.12.2017 12:59
I'm programmer, I have no life :)
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 7 zpráv z 7.