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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 安装 opencv4nodejs 报错
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!