duanou8504 2018-02-15 11:54
浏览 72
已采纳

PHP联系表单提交按钮打开索引html

I am making a custom contact form via html5 and php. I have got the form looking like I want and am trying to check if the values entered in the fields are working. I am print_r($_POST) to display the arrays.

When clicking the submit button it is not displaying the array but opening the index.html file instead?

The code is as follows...

Contact template calling in the form php file

<?php
/**
 * Template Name: contact
*/
get_header();

if (have_posts()) : 
   while (have_posts()) : the_post();
      get_template_part('form');
   endwhile; 
else: 
   echo '<p>No Content found</p>';
endif;
?>
</body>
     Template part form.php (html layout)

    <?php include('form_process.php'); ?>
    <div class='grey'>    
        <div class="container-contact">  
            <form id="contact" action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
              <div class='contact-logo'></div>
              <h3>Contact the Devon Food Movement</h3>
              <fieldset>
                  <input placeholder="Your name" type="text" tabindex="1" name="name" autofocus>
                  <span class="error"><?= $name_error ?></span>
              </fieldset>
              <fieldset>
                  <input placeholder="Your Email Address" type="text" name="email" tabindex="2" >
              </fieldset>
              <fieldset>
                  <textarea placeholder="Type your Message Here...." name="message" tabindex="3" ></textarea>
              </fieldset>
              <fieldset>
                  <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
              </fieldset>
          </form>
       </div>
    </div>

And this is the following form being called in form.php = (form_process.php)

<?php
    print_r($_POST);

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

    //form is submitted with POST method
    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 (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["message"])) {
        $message = "";
      } else {
        $message = test_input($_POST["message"]);
      }

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

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

    }

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

Why is the submit button opening index.html?

I would make a snippet but dont know how to do this with multiple templates being called in?

Thanks.

UPDATE OF form_process.php file after removing the invalid variables where there are no longer input boxes holding those values

        print_r($_POST);

        // define variables and set to empty values
        $name_error = $email_error = "";
        $name = $email = $message = "";

        //form is submitted with POST method
        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 (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["message"])) {
            $message = "";
          } else {
            $message = test_input($_POST["message"]);
          }

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

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

        }

        function test_input($data) {
          $data = trim($data);
          $data = stripslashes($data);
          $data = htmlspecialchars($data);
          return $data;
        }
  • 写回答

1条回答 默认 最新

  • dougui4325 2018-02-15 12:20
    关注

    Instead of using $_SERVER['PHP_SELF'] in your template file's form action, use full path to your form_process.php file.

    $_SERVER['PHP_SELF'] returns:

    The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/foo/bar.php would be /foo/bar.php.

    Edit:

    Here is some solution for you to not redirects to another page:

    Template part form.php (html layout):

    <?php include('form_process.php'); ?>
    <div class='grey'>
        <div class="container-contact">
            <form id="contact" method="post">
                <div class='contact-logo'></div>
                <h3>Contact the Devon Food Movement</h3>
                <fieldset>
                    <input placeholder="Your name" type="text" tabindex="1" name="name1" autofocus>
                    <span class="error"><?= $name_error ?></span>
                </fieldset>
                <fieldset>
                    <input placeholder="Your Email Address" type="text" name="email" tabindex="2" >
                </fieldset>
                <fieldset>
                    <textarea placeholder="Type your Message Here...." name="message" tabindex="3" ></textarea>
                </fieldset>
                <fieldset>
                    <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
                </fieldset>
            </form>
        </div>
    </div>
    

    form_process.php file:

    <?php
    print_r($_POST);
    
    // define variables and set to empty values
    $name_error = $email_error = $phone_error = $url_error = "";
    $name = $email = $phone = $message = $url = $success = "";
    
    //form is submitted with POST method
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name1"])) {
            $name_error = "Name is required";
        } else {
            $name = test_input($_POST["name1"]);
            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                $name_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["message"])) {
            $message = "";
        } else {
            $message = test_input($_POST["message"]);
        }
    
        if ($name_error == '' and $email_error == '' and $phone_error == '' and $url_error == '' ){
            $message_body = '';
            unset($_POST['submit']);
            foreach ($_POST as $key => $value){
                $message_body .=  "$key: $value
    ";
            }
    
            $to = 'vladi@clevertechie.com';
            $subject = 'Contact Form Submit';
            if (mail($to, $subject, $message)){
                $success = "Message sent, thank you for contacting us!";
                $name = $email = $phone = $message = $url = '';
            }
        }
    
    }
    
    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码