简体   繁体   English

联系表格指向contact.php,但我不希望它重定向。 (发送 email 确实有效)

[英]Contact form directs to contact.php, but i dont want it do redirect. (sending email DOES work)

Dont worry, I have already searched a lot of places and tried to watch other forums and videos.别担心,我已经搜索了很多地方并尝试观看其他论坛和视频。 Im not trying to make it easy.我不想让它变得容易。

When I submit a form on my page it redirect to www.mypag.com/contact.php .当我在我的页面上提交表单时,它会重定向到www.mypag.com/contact.php Im trying to use AJAX so that the form will be sent and the page doesnt redirect.我正在尝试使用 AJAX 以便发送表单并且页面不会重定向。 Thank you in advance..先感谢您..

                <form id="contact-form" method="post" action="contact.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">Fornavn *</label>
                                <input id="form_name" type="text" name="name" class="form-control" placeholder="Dit fornavn *" required="required" data-error="Fornavn kræves.">
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="form_lastname">Efternavn *</label>
                                <input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Dit efternavn *" required="required" data-error="Efternavn kræves.">
                                <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="Din email *" required="required" data-error="Gyldig email kræves.">
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="form_number">Dit nummer </label>
                                <input id="form_number" type="text" name="number" class="form-control" placeholder="Dit telefonnummer (ikke et krav) ">
                                <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">Besked *</label>
                                <textarea id="form_message" name="message" class="form-control" placeholder="Din besked *" rows="4" required="required" data-error="Vær venlig og skriv en besked før du sender."></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="Send besked">
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>

This is my JS这是我的 JS

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

$('#contact-form').on('submit', function (e) {

    if (!e.isDefaultPrevented()) {
        var url = "index.html";

        // POST values in the background the the script URL
        $.ajax({
            type: "POST",
            url: 'https://andreas.xn--hjorhy-fya.dk/',
            data: $(this).serialize(),
            success: function (data) {
              
                var messageAlert = 'alert-' + data.type;
                var messageText = data.message;


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

               
                if (messageAlert && messageText) {
                    $('#contact-form').find('.messages').html(alertBox);
                    $('#contact-form')[0].reset();
                }
            }
        });
        return false;
    }
})

This is my PHP这是我的 PHP

<?php

    $from = 'Demo contact form <demo@domain.com>';
    $sendTo = 'Demo contact form <MYEMAILHERE>';
    $subject = 'New message from contact form';
    $fields = array('name' => 'Name', 'surname' => 'Surname', 'need' => 'Need', 'email' => 'Email',                 
    'message' => 'Message');
    $okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
    $errorMessage = 'There was an error while submitting the form. Please try again later';
    error_reporting(E_ALL & ~E_NOTICE);
    try
    {
        if(count($_POST) == 0) throw new \Exception('Form is empty');
        $emailText = "You have a new message from your contact         
    form\n=============================\n";
        foreach ($_POST as $key => $value) {
            if (isset($fields[$key])) {
                $emailText .= "$fields[$key]: $value\n";
            }
        }
        $headers = array('Content-Type: text/plain; charset="UTF-8";',
            'From: ' . $from,
            'Reply-To: ' . $from,
            'Return-Path: ' . $from,
        );
        mail($sendTo, $subject, $emailText, implode("\n", $headers));
        $responseArray = array('type' => 'success', 'message' => $okMessage);
    }
    catch (\Exception $e)
    {
        $responseArray = array('type' => 'danger', 'message' => $errorMessage);
    }
    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) ==         
    'xmlhttprequest') {
        $encoded = json_encode($responseArray);
        header('Content-Type: application/json');
        echo $encoded;
    }
    // else just display the message
    else {
        echo $responseArray['message'];
    }

Thanks in advance for everyone that makes time to help me or redirect me to somewhere that might fix this problem..提前感谢所有抽出时间帮助我或将我重定向到可能解决此问题的地方的人。

*** EDIT *** *** 编辑 ***

This is my whole HTML page, with all the scripts etc. As for now I've moved some scripts before my form, but that didnt help.这是我的整个 HTML 页面,包含所有脚本等。至于现在我已经在我的表单之前移动了一些脚本,但这没有帮助。 Im currenty looking for through the logs as plesk web host edition doesnt provice developer console.. only the SSH server does (which i for some reason cant access) As for now, the message is still sent and I recieve them, but Im redirected to contact.php (I dont want a redirect, I want to stay on the same page because I only have 1 page on my website)我目前正在通过日志寻找 plesk web 主机版不提供开发者控制台.. 只有 SSH 服务器可以(由于某种原因我无法访问)至于现在,消息仍然发送并且我收到它们,但我重定向到contact.php(我不想要重定向,我想留在同一页面上,因为我的网站上只有一页)

  <head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Andreas Hjorhøy Rasmussen</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="all,follow">
<link rel="stylesheet" href="vendor/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="vendor/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab:400,700%7CRoboto:400,700,300">
<link rel="stylesheet" href="vendor/owl.carousel/assets/owl.carousel.css">
<link rel="stylesheet" href="vendor/owl.carousel/assets/owl.theme.default.css">
<link rel="stylesheet" href="vendor/animate.css/animate.css">
<link rel="stylesheet" href="css/style.default.css" id="theme-stylesheet">
<link rel="stylesheet" href="css/custom.css">
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script><![endif]-->

  <!--here i have my whole sebsite.. information, pictures, blog etc -->
  
  <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>

  <!-- Kontakt-->
  <section id="contact" data-animate="bounceIn" class="contact-section contact">
      <div class="row" data-animate="fadeInUp">
          <div class="col-md-12">
              <div class="heading text-center">
                  <h2>Kontakt mig</h2>
              </div>
          </div>
          <div class="col-md-8 mx-auto" data-animate="fadeInUp">
              <form id="contact-form" method="post" action="contact.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">Fornavn *</label>
                                  <input id="form_name" type="text" name="name" class="form-control" placeholder="Dit fornavn *" 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">Efternavn *</label>
                                  <input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Dit efternavn *" 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="Din 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_number">Dit nummer </label>
                                  <input id="form_number" type="text" name="number" class="form-control" placeholder="Dit telefonnummer (ikke et krav) ">
                                  <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">Besked *</label>
                                  <textarea id="form_message" name="message" class="form-control" placeholder="Din besked *" rows="4" required="required" data-error="Please, leave us a message."></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="Send besked">
                          </div>
                      </div>
                  </div>
              </form>
          </div>
      </div>
  </section>

    <!-- Had to remove footer because of personal links -->
  <!-- JavaScript files-->
  <script src="vendor/jquery/jquery.min.js"></script>
  <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="vendor/jquery.cookie/jquery.cookie.js"></script>
  <script src="vendor/owl.carousel/owl.carousel.min.js"></script>
  <script src="vendor/waypoints/lib/jquery.waypoints.min.js"></script>
  <script src="vendor/jquery.counterup/jquery.counterup.js"></script>
  <script src="js/front.js"></script>

You have 2 copies of jQuery in your page.您的页面中有 2 个 jQuery 副本。 This can cause errors, conflicts and other unexpected behaviour, especially if they are not the same version of jQuery.这可能会导致错误、冲突和其他意外行为,尤其是当它们与 jQuery 的版本不同时。 And even if it didn't, it's inefficient and unnecessary.即使没有,它也是低效且不必要的。

I suggest removing the second one ( vendor/jquery/jquery.min.js ).我建议删除第二个( vendor/jquery/jquery.min.js )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM