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 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行