duadpnld426905 2014-11-24 21:45
浏览 76

如何在我的Bootstrap模式窗口中插入PHP

I found and adapted a php script that processes the content of a contact form and sends and/or display alerts that would appear on a Bootstrap 3 modal window. I manage to make work my script and display my errors, as well as to display my modal onload with non php in it, but as soon as I try to include my php code into the modal to display the errors within the modal, nothing displays.

I tried many ways : PHP in HTML, HMTL in PHP… And no combination worked. This is where I arrived so far :

<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>My title</title>

    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">

    <!-- Fonts -->
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
   <link href="css/animate.css" rel="stylesheet" />
    <!-- Squad theme CSS -->
    <link href="css/style.css" rel="stylesheet">
   <link href="color/default.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

  </head>
    <body>

<?php

      // Clean up the input values
      foreach($_POST as $key => $value) {
         if(ini_get('magic_quotes_gpc'))
            $_POST[$key] = stripslashes($_POST[$key]);

         $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
      }

      // Assign the input values to variables for easy reference
      $name = $_POST["name"];
      $email = $_POST["email"];
      $message = $_POST["message"];

      // Test input values for errors
      $errors = array();
      if(strlen($name) < 2) {
         if(!$name) {
            $errors[] = "Vous devez indiquer votre nom.";
         } else {
            $errors[] = "Votre nom doit faire au moins deux caractères de long.";
         }
      }
      if(!$email) {
         $errors[] = "Vous devez renseigner votre email.";
      } else if(!validEmail($email)) {
         $errors[] = "Merci de fournir une adresse email valide.";
      }
      if(strlen($message) < 10) {
         if(!$message) {
            $errors[] = "Vous devez saisir votre message.";
         } else {
            $errors[] = "Merci de laisser un message d'au moins dix caractères de long.";
         }
      }
      if($errors) {
                     // Output errors and die with a failure message
                    $errortext = "";
                    foreach($errors as $error) {
                    $errortext .= "<li>".$error."</li>";
                    }
                    die("
                        <div class=\"modal fade\" id=\"fail-prompt\">
                          <div class=\"modal-dialog\">
                            <div class=\"modal-content\">
                              <div class=\"modal-header\">
                                <button type=\"button\" class=\"close\" data-dismiss=\"modal\"><span aria-hidden=\"true\">&times;</span><span class=\"sr-only\">Close</span></button>
                                <h4 class=\"modal-title\">Votre message n'a pas pu être envoyé.</h4>
                              </div>
                              <div class=\"modal-body\">
                                 <p>Votre message n'a pas pu être envoyé</p>
                                 <ul>". $errortext ."</ul>
                              </div>
                              <div class=\"modal-footer\">
                                <button type=\"button\" class=\"btn btn-primary\">Retour vers le formulaire</button>
                              </div>
                            </div>
                          </div>
                        </div>");

            // Send the email
            $to = "myemail";
            $subject = "Contact Form: $name";
            $message = "$message";
            $headers = "From: $email";

            mail($to, $subject, $message, $headers);

            // Die with a success message
            die("<div class=\"modal fade\" id=\"fail-prompt\">
                  <div class=\"modal-dialog\">
                     <div class=\"modal-content\">
                     <div class=\"modal-header\">
                        <button type=\"button\" class=\"close\" data-dismiss=\"modal\"><span aria-hidden=\"true\">&times;</span><span class=\"sr-only\">Close</span></button>
                        <h4 class=\"modal-title\">Message envoyé avec succès !</h4>
                     </div>
                     <div class=\"modal-body\">
                        <p>Votre message a été envoyé avec succès !</p>
                     </div>
                     <div class=\"modal-footer\">
                        <button type=\"button\" class=\"btn btn-primary\">Retour au site</button>
                     </div>
                     </div>
                  </div>
               </div>");

            // A function that checks to see if
            // an email is valid
            function validEmail($email)
            {
               $isValid = true;
               $atIndex = strrpos($email, "@");
               if (is_bool($atIndex) && !$atIndex)
               {
                  $isValid = false;
               }
               else
               {
                  $domain = substr($email, $atIndex+1);
                  $local = substr($email, 0, $atIndex);
                  $localLen = strlen($local);
                  $domainLen = strlen($domain);
                  if ($localLen < 1 || $localLen > 64)
                  {
                     // local part length exceeded
                     $isValid = false;
                  }
                  else if ($domainLen < 1 || $domainLen > 255)
                  {
                     // domain part length exceeded
                     $isValid = false;
                  }
                  else if ($local[0] == '.' || $local[$localLen-1] == '.')
                  {
                     // local part starts or ends with '.'
                     $isValid = false;
                  }
                  else if (preg_match('/\\.\\./', $local))
                  {
                     // local part has two consecutive dots
                     $isValid = false;
                  }
                  else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
                  {
                     // character not valid in domain part
                     $isValid = false;
                  }
                  else if (preg_match('/\\.\\./', $domain))
                  {
                     // domain part has two consecutive dots
                     $isValid = false;
                  }
                  else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                             str_replace("\\\\","",$local)))
                  {
                     // character not valid in local part unless 
                     // local part is quoted
                     if (!preg_match('/^"(\\\\"|[^"])+"$/',
                         str_replace("\\\\","",$local)))
                     {
                        $isValid = false;
                     }
                  }
                  if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
                  {
                     // domain not found in DNS
                     $isValid = false;
                  }
               }
               return $isValid;
            }
      ?>

</body>

        <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>-->
        <script src="js/jquery.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <!-- Custom Script-->
        <script type="text/javascript">
        $(window).load(function(){
            $('#fail-prompt').modal('show');
            backdrop: 'static';
            keyboard: false;
        });
        </script>
</html>
  • 写回答

2条回答 默认 最新

  • douchun5976 2014-11-24 21:59
    关注

    PHP don't create a modal, because for lack includes of styles, js... Recommended "create a modal" using ajax.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?