将HTML文件更改为PHP后出错

时间:2017-11-20 20:00:07

标签: php html

我将我的html文件更改为php以便在表单中显示php错误,我不确定我是否想添加或减少php / html标签。我原样离开了。当我在浏览器中打开我的表单时,它向我显示以下错误。 :

  

注意:未定义的变量:第165行的C:\ xampp \ htdocs \ BootstrapLandinPage \ index.php中的name_error

     

注意:未定义的变量:第170行的C:\ xampp \ htdocs \ BootstrapLandinPage \ index.php中的lastname_error

     

注意:未定义的变量:第175行的C:\ xampp \ htdocs \ BootstrapLandinPage \ index.php中的phone_error

等。

基本上每个输入都显示错误。

我的index.php:

<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title>Bootstrap 101 Template</title>

        <!-- Bootstrap -->
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link rel="stylesheet" href="font-awesome-4.7.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="css/style.css">
        <link rel="stylesheet" href="css/animate.min.css">
        <!-- 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.3/html5shiv.min.js"></script>
          <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>

        <!--NAVIGATION-->

        <div id="myNavbar" class="navbar navbar-default navbar-fixed-top" role="navigation">
            <div class="container">
                <div class="navbar-header navbar-right">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>

                    <a href="#" class="navbar-brand ">Hello Dolly</a>
                </div>
                <div class="navbar-collapse collapse" id="mainpanel">
                    <ul class="nav navbar-nav ">
                        <li><a href="#header">dolly</a></li>
                        <li><a href="#services">holly</a></li>
                        <li><a href="#pricing">holly</a></li>
                        <li><a href="#team">holly</a></li>
                        <li><a href="#client">holly</a></li>
                        <li><a href="#contact">holly</a></li>
                    </ul>
                </div>
            </div>
        </div>

        <!--- Header ---->
 <section class="parallax">
                <div class="parallax-inner">
        <div id="header" class="header">
            <div class="container">
                <div class="row">
                    <div class="col-md-6 wow bounceInLeft navbar-right">
                        <h1 class="text-right" id="main-title">dolly </h1>
                        <h2 class="text-right" id="changed-title">dolly</h2><br><br>
                        <!---   <button class="btn btn-lg btn-primary navbar-right">dolly</button> ---->

                    </div>

                </div>
            </div>
        </div>
                </div>
 </section>

            <!-- [CONTACT] -->
           <section class="inspiration" id="three">
                <div class="overlay">
                    <div class="container">
                        <div class="row">
                            <article class="col-md-12 text-center">
                                <div class="intermediate-container">
                                    <div class="heading">
                                        <h2>יש לכם שאלה? צרו איתי קשר</h2>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-3 col-sm-3"></div>
                                        <div class="col-md-6 center-block col-sm-6 ">
                                            <form id="mc-form" action ="send.php" method="POST">
                                                <div class="form-group col-xs-12 ">
                                                    <label for="name" hidden >שם פרטי</label>
                                                    <input type="text" name="name" id="name" class="cv form-control" value="<?= $name ?>" placeholder="שם פרטי" >
                                                     <span class="error"><?= $name_error ?></span>
                                                </div>
                                                <div class="form-group col-xs-12 ">
                                                    <label for="lastName" hidden>שם משפחה</label>
                                                    <input type="text" name="lastName" id="lastName" class="cv form-control" value="<?= $lastName ?>" placeholder="שם משפחה" >
                                                     <span class="error"><?= $lastname_error ?></span>
                                                </div>
                                                <div class="form-group col-xs-12 ">
                                                    <label for="phone" hidden>טלפון</label>
                                                    <input type="text" name="phone" id="phone" class="cv form-control" value="<?= $phone ?>" placeholder="טלפון" >
                                                     <span class="error"><?= $phone_error ?></span>
                                                </div>
                                                <div class="form-group col-xs-12 ">
                                                    <label for="email" hidden>דואר אלקטרוני</label>
                                                    <input type="email" name="email" id="email" class="cv form-control" value="<?= $email ?>" placeholder="דואר אלקטרוני" >
                                                     <span class="error"><?= $email_error ?></span>
                                                </div>
                                                <div class="form-group col-xs-12 ">
                                                    <label for="subject" hidden>נושא</label>
                                                    <input type="text" name="subject" id="subject" class="cv form-control" value="<?= $subject?>" placeholder="נושא" >
                                                </div>
                                                <div class="form-group col-xs-12 ">
                                                    <label for="message" hidden>הודעה</label>
                                                    <textarea name="message" id="message" class="cv form-control message"  placeholder="השאירו את הודעתכם פה" rows="4" cols="50"></textarea>
                                                </div>
                                                <input type="submit" id="submit-button" class="btn btn-custom-outline " value="שלח" >
                                                <br>
                                                <div class="success"><?= $success ?></div>
                                                <!--<span class="error"></span> -->
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </article>
                        </div>
                    </div>
                </div>
           </section>
            <!-- [/CONTACT] -->




        <!-- Optional JavaScript -->
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" ></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" ></script>
        <script src="js/bootstrap.min.js" ></script>
           <!-- [ SLIDER SCRIPT ] -->  
        <script type="text/javascript" src="js/SmoothScroll.js"></script>
        <script src="js/script.js" ></script>
    </body>
</html>

send.php:

// define variables and set to empty values
$name_error = $lastname_error = $email_error = $phone_error = "";
$name = $lastName = $email = $phone = $message = $subject = $success = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $name_error = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Zא-ת ]*$/",$name)) {
      $name_error = "Only letters and white space allowed"; 
    }
  }

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["lastName"])) {
    $lastname_error = "Name is required";
  } else {
    $lastname = test_input($_POST["lastName"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Zא- ]*$/",$lastName)) {
      $lastname_error = "Only letters and white space allowed"; 
    }
  }


  if (empty($_POST["email"])) {
    $email_error = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $email_error = "Invalid email format"; 
    }
  }

  if (empty($_POST["phone"])) {
    $phone_error = "Phone is required";
  } else {
    $phone = test_input($_POST["phone"]);
    // check if e-mail address is well-formed
    if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
      $phone_error = "Invalid phone number"; 
    }
  }

if (empty($_POST["subject"])) {
    $subject = "";
  } else {
    $subject = test_input($_POST["subject"]);
  }

  if (empty($_POST["message"])) {
    $message = "";
  } else {
    $message = test_input($_POST["message"]);
  }

  if ($name_error == '' and $email_error == '' and $phone_error == '' and $lastname_error == '' ){
      $message_body = '';
      unset($_POST['submit']);
      foreach ($_POST as $key => $value){
          $message_body .=  "$key: $value\n";
      }

      $to = 'ilonasemyweb@gmail.com';
      $subjectm = 'Contact Form Submit';
      if (mail($to, $subjectm, $message)){
          $success = "Message sent, thank you for contacting us!";
          $name = $lastName = $email = $phone = $message = $subject = '';
      }
  }

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

1 个答案:

答案 0 :(得分:0)

您需要做的是:

  1. index.php中:检查是否定义了这些变量。如果没有,那么就没有错误,因此不会显示任何错误,就像第一次进入时一样。
  2. send.php中:正确发送回错index.php。这可以使用require_once方法完成。
  3. 因此,在检查所有发送的数据是否有错误后写:

    if(/* there were errors */)
      require_once("path/to/index.php");
    

    然后,这将获得index.php,&#34的所有文本;将其放在index.php&#34;中的那个位置。并继续口译。使用它,send.php中定义的所有变量现在也在index.php中定义(当然也保留它们的值)。但请注意,新网址现在将以send.php结尾,除非您稍稍处理。