每当我点击表单上的提交按钮时,它会将我重定向到我的php链接,这是空白的,为什么?

So I tried making my own contact form and that kind of worked but I really wanted to add some cool features like telling users when they didn't fill in a required form and telling them when they submitted the form correctly. I got this code from this site. Whenever I click the submit button it takes me to my PHP file link and the page is blank! I want to be able to stay on the same page and have the success/error messages appear not to be redirected. Sorry, I'm a bit new to web development.

BOTH FILES ARE .PHP!!!

Here's the code:

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
    $subject = $_POST['subject'];
        $from = 'Plugin Request Form'; 
        $to = 'theslimeslayer04@gmail.com'; 
        $subject = 'New Plugin Request';

        $body = "From: $name
 E-Mail: $email
 Subject: $subject
 Message:
 $message";

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }
    if(!$_POST['subject']) {
      $errSubject = 'Please enter a subject';
    }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="description" content="My very first website.">
  <meta name="author" content="Reprevise">
  <meta name="keywords" content="My development portfolio.">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Reprevise | Home</title>

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

  <link rel="stylesheet" type="text/css" href="assets/css/layout.css">
  <link rel="stylesheet" type="text/css" href="assets/css/theme.css">

  <link rel="shortcut icon" href="assets/images/favicon.ico" type="image/x-icon">
  <link rel="icon" href="assets/images/favicon.ico" type="image/x-icon">

</head>

<body>

  <nav class="nav" id="homelink">
    <div class="container">
      <div class="logo">
        <img class="sitelogo" src="assets/images/logo.jpg" alt="logo">
      </div>
      <ul class="menu">
        <li><a href="#homelink">Home</a></li>
        <li><a href="#aboutlink">About</a></li>
        <li><a href="#contact">Contact</a></li>
      </ul>
    </div>
  </nav>

  <header>
    <div class="title-wrapper">
      <h1 class="header-title">reprevise's dev studio</h1>
    </div>
  </header>

  <div class="aboutsection" id="aboutlink">
    <div class="headingtitle-wrapper">
      <h1>About Reprevise</h1>
    </div>
    <div class="about-paragraph">
      <p style="text-align: left;">Benjamin, otherwise known as Benjamin has been a developer of all different types of programming languages for about 3 years now. He has knowledge of Lua, Java, HTML, CSS, JS, C#, and Python. Ben loves to make Minecraft plugins for all sorts of
        clients for free! Below is a contact form in which you can hit him up if you have a plugin request.</p>
    </div>
  </div>

  <div class="conform-container">
    <div id="contact">
      <div class="conform-heading-wrapper">
        <h1>Contact Form</h1>
        <h4>Send me plugin requests here.</h4>
      </div>
      <form class="form-horizontal" role="form" method="post" action="form-handler.php">
        <div class="form-group">
          <label for="name" class="col-sm-2 control-label">Name</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
            <?php echo "<p class='text-danger'>$errName</p>";?>
          </div>
        </div>
        <div class="form-group">
          <label for="email" class="col-sm-2 control-label">Email</label>
          <div class="col-sm-10">
            <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
            <?php echo "<p class='text-danger'>$errEmail</p>";?>
          </div>
        </div>
        <div class="form-group">
          <label for="subject" class="col-sm-2 control-label">Subject</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="subject" name="subject" placeholder="Really Important Plugin" value="<?php echo htmlspecialchars($_POST['subject']); ?>">
            <?php echo "<p class='text-danger'>$errSubject</p>";?>
          </div>
        </div>
        <div class="form-group">
          <label for="message" class="col-sm-2 control-label">Message</label>
          <div class="col-sm-10">
            <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
            <?php echo "<p class='text-danger'>$errMessage</p>";?>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-10 col-sm-offset-2">
            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-10 col-sm-offset-2">
            <?php echo $result; ?>
          </div>
        </div>
      </form>
    </div>
  </div>

  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

</body>

</html>
dongwopu8210
dongwopu8210 只是谷歌“如何制作ajax联系表”
一年多之前 回复
doujindou4356
doujindou4356 我相信谷歌可以。
一年多之前 回复
dongya5893
dongya5893 那么有人可以推荐一个在PHP中创建联系表单的好教程吗?
一年多之前 回复
dongyan6503
dongyan6503 这是问题中的第一个代码块。您可以看到他们填充$response的位置,在下一个代码块中,您可以看到他们希望神奇地出现在哪里。
一年多之前 回复
dsfdsf21312
dsfdsf21312 这包含处理表单和创建响应的逻辑据我所知它可能包含404.没有任何代码位用文件名命名。
一年多之前 回复
dqwh1203
dqwh1203 它包含处理表单和创建响应的逻辑。但是他们假设在提交表单后会显示$response的值。他们不了解HTTP生命周期。
一年多之前 回复
doubaran2438
doubaran2438 这个页面中的内容是form-handler.php吗?AKA表格动作......
一年多之前 回复
doumou5109
doumou5109 你很困惑这一切是如何运作的。PHP在服务器端执行。一旦加载该页面,就会生成内容,就是这样。在加载页面后,您需要使用Ajax和Javascript来更新该内容。您需要阅读客户端和服务器端编程之间的区别是什么?
一年多之前 回复
dth34653
dth34653 它在HTML文件中得到了回应。
一年多之前 回复
duanluan3651
duanluan3651 在处理表单后,您决不尝试输出任何内容。您所做的只是将内容放在变量中,但您永远不会输出它。
一年多之前 回复

1个回答

The answer to your question is found inside of the PHP script in your post.

If there are no errors, send the email

That contact form is designed to send emails only. It's not designed to dump out an HTML response to the page.

However, you can add it easily at the bottom of the .PHP file. This example simply uses the basic php_include command, which loads another file. You could add standard HTML markup into that file.

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
} else {
   php_include('.../some/path/to/a/file.php');
}
    }
?>
...

But what you really want, is something like this. The idea is that you're not leaving the form, when the <input type="submit"> button is pressed...

<form class="form-horizontal" role="form" onSubmit="return false;">

Then you can use JavaScript to show people some...

cool features like telling users when they didn't fill in a required form and telling them when they submitted the form correctly

Wikipedia has a good overview of how AJAX technology works. Once you understand how Ajax engines work, then you may want to look into using jQuery.

The jQuery $.post() method is an easy way to send form data from a page over to the server using AJAX. Another simple example to use is $.get(). For a more advanced example, see the $.ajax() method. There is also a pretty comprehensive API of pre-built methods to peruse.

Hopefully, that answers your original question & points you into the right direction of learning how to start becoming an Ajax programmer. Good luck with your contact form! :)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐