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í.

Diskuze: Kontaktný formulár-po kliknutí na odoslať sa nič nedeje

Aktivity
Avatar
Tomáš Michálek:24.4.2022 17:24

Zdravím .
Som začiatočník a robil som stránku pre našu malú firmu. Kamarát ma požiadal , či by som tam nepridal niečo ako cenovú ponuku a keďže v PHP neviem robiť tak som všetky kódy skopíroval zo stránky a pridal si ich tam . Poprekladal som si texty a pomenil emaily .
Chyba je v tom , že keď kliknem na "Pošli správu " tak sa nič nestane .
Budem veľmi rád ak mi s tým pomôžete.
Vopred ďakujem .

PHP

<?php
/*
 *  CONFIGURE EVERYTHING HERE
 */

// an email address that will be in the From field of the email.
$from = '<[email protected]>';

// an email address that will receive the email with the output of the form
$sendTo = ' <[email protected]>';

// subject of the email
$subject = 'Nová správa -Cenová ponuka';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Tel.číslo', 'need' => 'Služba', 'email' => 'Email', 'message' => 'Bližšie info');

// message that will be displayed when everything is OK :)
$okMessage = 'Žiadosť bola odoslaná, ozvem sa vám hneď ako to bude možné';

// If something goes wrong, we will display this message.
$errorMessage = 'Prosím vyplnte všetky polia a skúste to znova';

/*
 *  LET'S DO THE SENDING
 */

// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

try
{

    if(count($_POST) == 0) throw new \Exception('Kolonka je prázdna');

    $emailText = "Máte novú správu z cenovej ponuky\n=============================\n";

    foreach ($_POST as $key => $value) {
        // If the field exists in the $fields array, include it in the email
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    // All the neccessary headers for the email.
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );

    // Send email
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}


// if requested by AJAX request return JSON response

// else just display the message
 else {
    echo $responseArray['message'];
}

JS

$(function () {

    // init the validator
    // validator files are included in the download package
    // otherwise download from http://1000hz.github.io/bootstrap-validator

    $('#contact-form').validator();


    // when the form is submitted
    $('#contact-form').on('submit', function (e) {

        // if the validator does not prevent form submit
        if (!e.isDefaultPrevented()) {
            var url = "mail.php";

            // POST values in the background the the script URL
            $.ajax({
                type: "POST",
                url: url,
                data: $(this).serialize(),
                success: function (data)
                {
                    // data = JSON object that contact.php returns

                    // we recieve the type of the message: success x danger and apply it to the
                    var messageAlert = 'alert-' + data.type;
                    var messageText = data.message;

                    // let's compose Bootstrap alert box HTML
                    var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';

                    // If we have messageAlert and messageText
                    if (messageAlert && messageText) {
                        // inject the alert to .messages div in our form
                        $('#contact-form').find('.messages').html(alertBox);
                        // empty the form
                        $('#contact-form')[0].reset();
                    }
                }
            });
            return false;
        }
    })
});

HTML

<!doctype html>
<html lang="sk">
<head>
    <title>Cenová ponuka</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <link href='custom.css' rel='stylesheet' type='text/css'>
</head>

<body>

    <div class="container">

        <div class="row">

            <div class="col-xl-8 offset-xl-2 py-5">

                <h1>Žiadosť o cenovú ponuku

                </h1>

                <p class="lead">This is a demo for our tutorial dedicated to crafting working Bootstrap contact form with PHP and AJAX background.</p>


                <form id="contact-form" method="post" action="mail.php" role="form">

                    <div class="messages"></div>

                    <div class="controls">

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="form_name">Meno a Priezvisko*</label>
                                    <input id="form_name" type="text" name="name" class="form-control" placeholder="Zadaj svoje meno a priezvisko *" required="required" data-error="Firstname is required.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="form_lastname">Telefonné číslo *</label>
                                    <input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Zadaj telefonne číslo*" required="required" data-error="Lastname is required.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="form_email">Email *</label>
                                    <input id="form_email" type="email" name="email" class="form-control" placeholder="Zadaj email *" required="required" data-error="Valid email is required.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="form_need">Prosím vyber o akú službu ide *</label>
                                    <select id="form_need" name="need" class="form-control" required="required" data-error="Please specify your need.">
                                        <option value=""></option>
                                        <option value="Request quotation">Request quotation</option>
                                        <option value="Request order status">Request order status</option>
                                        <option value="Request copy of an invoice">Request copy of an invoice</option>
                                        <option value="Other">Other</option>
                                    </select>
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="form_message">Informácie o pojekte *</label>
                                    <textarea id="form_message" name="message" class="form-control" placeholder="Bližšie informácie o projekte" rows="4" required="required" data-error="Prosím zanechajte správu"></textarea>
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <input type="submit" class="btn btn-success btn-send" value="Pošli správu">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <p class="text-muted">
                                    <strong>*</strong> Späť na hlavnú stránku
                                    <a href="index.html" target="_blank">Hlavná stránka</a>.</p>
                            </div>
                        </div>
                    </div>

                </form>

            </div>
            <!-- /.8 -->

        </div>
        <!-- /.row-->

    </div>
    <!-- /.container-->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.9/validator.min.js" integrity="sha256-dHf/YjH1A4tewEsKUSmNnV05DDbfGN3g7NMq86xgGh8=" crossorigin="anonymous"></script>

     <script src="contact.js"></script>
</body>

</html>

Zkusil jsem:

Chci docílit:

 
Odpovědět
24.4.2022 17:24
Avatar
Peter Mlich
Člen
Avatar
Peter Mlich:24.4.2022 21:07

Zapni si error_reporting, aby ti zobrazoval chyby.
A ikdyz zadna nenastane, stale php nemusi umet mail odeslat, protoze nemas v php / na serveru aktivovany mail server.

mail($sendTo, $subject, $emailText, implode("\n", $headers)); -- tady se to spojuje treva pres PHP_EOL a ne "\n"

A, pak je tu moznost, ze tvuj server ma odesilani mailu zakazane. Ale, muzes to mozna odesilat pres nejaky jiny. Nebo to ma limitovane na 10 maiuy za hodinu. Proste je tam more dalsich moznych pricin.

Doporucuji najmout si cloveka, ktery php umi. Cena byva tak 1000 za kazdou zapocatou hodinu. V takhle rozsahlem kodu muzes mit tech chyb mraky. Zkus nejdriv nejaky jednoduchy formular, ktery odesle mail.
Jedine, co pro tebe muzu udelat, ze si zapnu ty error reporting a necham vypsat chyby. Ale, kdyz ti je vypisi, budes si to umet spravit? Bezhlave kopirovani je naprd :)

Kod, ktery si zkus nejdrive, je primo v dokumentaci php php.net/mail Prepis si tam samozrejme adresy from / to reply=from

<?php
// Example #2 Sending mail with extra headers.
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>
 
Nahoru Odpovědět
24.4.2022 21:07
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 2 zpráv z 2.