dongliming2416 2019-06-08 08:11
浏览 123

HTML表单发送GET而不是POST(XAMPP,Apache,localhost)

I've looked up other questions with possible solutions to my problem, but they don't seem to work for me. According to the Network console in Firefox, my contact form sends GET when I set the method to POST.

I have checked my HTML code for errors, but can't find any; no unclosed forms, divs, etc. I've checked the syntax for my php, too. I also tried setting the submit button to <button type="submit" formmethod="post" formaction="form-to-email.php" name="submit" class="button">Und los</button>'but it doesn't help, either.

EDIT: Here's my complete HTML code for this page:

  <!DOCTYPE html>
<html lang="" dir="ltr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="description" content="placeholder">
  <meta name="keywords" content="placeholder">
  <meta name="author" content="placeholder">

  <title>MADesign.</title>
  <link rel="author" href="robots.txt" />
  <link rel="author" href="humans.txt" />
  <!-- CSS -->
  <link rel="stylesheet" href="css/maincss.css">
</head>

<body>
  <div id="holder">
    <!-- page header -->
    <div class="bg-image-small">
      <div id="main_menu">
        <header id="page_header">
          <img src="images/mad-logo-300px.png" alt="mad logo" id="mad-logo">
        </header>
        <!-- END page header -->
        <!-- main navigation -->
        <nav id="main-nav">
          <ul>
            <li><a href="index.html">home</a></li>
            <li><a href="index.html#link1">link1</a></li>
            <li><a href="index.html#link2">link2</a></li>
            <li><a href="galerie.html">link3</a></li>
            <li><a href="kontakt.html">kontakt</a></li>
            <li><a href="impressum.html">impressum</a></li>
          </ul>
        </nav><!-- END main navigation -->
      </div><!-- END main menu -->
    </div><!-- END background image -->

    <!-- main content -->
    <main>
      <!-- contact form -->
      <form id="my-form" name="myForm" action="/form-to-email.php" method="post">
        <h2>Let's get in touch.</h2>
        <p>Schick mir eine Email an <a href="mailto:abc@placeholder.de">abc@placeholder.de</a> oder nutze mein Kontaktformular.</p>

        <div class="gender">
          <input type="radio" name="titles" value="male" id="mr"
            <?php if($titles == "male") echo "checked" ?>>
          <label for="titles">Herr</label>
          <input type="radio" name="titles" value="female" id="mrs"
            <?php if($titles == "female") echo "checked" ?>>
          <label for="titles">Frau</label>
          <input type="radio" name="titles" value="nonbinary" id="mx"
            <?php if($titles == "nonbinary") echo "checked" ?>>
          <label for="titles">Hallo</label>
          <input type="radio" name="titles" value="person" id="person"
            <?php if($titles == "person") echo "checked" ?>>
          <label for="titles">Person</label>
          <div class="errormsg">
            <?php echo $errors['titles']; ?>
          </div>
        </div>
        <div class="form-block">
          <label for="usrname">Name:</label><br>
          <input type="text" name="usrname" id="usrname" class="styleinput" size="20" maxlength="30" value="<?php echo htmlspecialchars($usrname) ?>">
          <div class="errormsg">
            <?php echo $errors['usrname']; ?>
          </div>
        </div>
        <div class="form-block">
          <label for="email">Email:</label><br>
          <input type="text" name="email" id="email" class="styleinput" size="20" maxlength="30" value="<?php echo htmlspecialchars($email) ?>">
          <div class="errormsg">
            <?php echo $errors['email']; ?>
          </div>
        </div>

        <div class="user-input form-block">
          <label for="user-input">Nachricht:</label><br>
          <textarea class="styleinput" id="message-me" name="usrmsg" rows="4" cols="50" value="<?php echo htmlspecialchars($usrmsg) ?>"></textarea>
          <div class="errormsg">
            <?php echo $errors['usrmsg']; ?>
          </div>
        </div>
        <button type="submit" formmethod="post" formaction="/form-to-email.php" name="submit" class="button">Und los</button>
        <input type="reset" name="reset" class="button" value="Nochmal neu..." onclick="emptyMsg()">
        <div id="message"></div>
      </form>
      <!-- END contact form -->
    </main>
    <!-- END main content -->
    <!-- footer -->
    <footer>
      <p>&copy2019 placeholder</p>
    </footer><!-- END footer-->
  </div><!-- END holder -->

  <!-- JavaScript and jQuery -->
  <script src="https://code.jquery.com/jquery-3.4.0.min.js" integrity="sha256-BJeo0qm959uMBGb65z40ejJYGSgR7REI4+CW1fNKwOg=" crossorigin="anonymous"></script>
  <script src="js/mainjs.js"></script>
 </body>
</html>

This is the PHP (saved in the same directory as kontakt.php):

<?php

$titles = $usrmsg = $usrname = $email = "";
$errors = array("email"=>"", "usrname"=>"", "usrmsg"=>"", "titles"=>"");

if(isset($_POST["submit"])){

  //check Email
  if(empty($_POST["email"])){
    $errors["email"] = "Bitte Email Adresse angeben.";
  } else {
    $email = $_POST["email"];
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
      $errors["email"] = "Die Email Adresse sollte gültig sein.";
    }
  }

 //check name
  if(empty($_POST["usrname"])){
    $errors["usrname"] = "Wie heißt du?";
  } else {
    $usrname = $_POST["usrname"];
    $usrname = filter_var($usrname, FILTER_SANITIZE_STRING);
    if(!preg_match("/^[a-zA-Z\s]+$/", $usrname)){
      $errors["usrname"] = "Sorry! Der Name darf nur Buchstaben und Leerzeichen enthalten.";
    }
  }

  //check message
   if(empty($_POST["usrmsg"])){
     $errors["usrmsg"] = "Hier sollte etwas Text stehen. Muss ja nicht viel sein.";
   } else {
     $usrmsg = $_POST["usrmsg"];
     $usrmsg = filter_var($usrmsg, FILTER_SANITIZE_STRING);
     }

  //check titles
    $titles = $_POST["titles"];
    if ($titles==NULL) {
      $errors["titles"] = "Welche Ansprache darf ich verwenden?";
    }
   }
?>

EDIT: Here's the JS for client-side form validation (it's not a finished version, some validations are still missing/may get changed, but so far it's working as it should):

  var myForm = document.forms.myForm;
  var message = document.getElementById('message');

  myForm.onsubmit = function() {

    //get IDs for the title
    var mr = document.getElementById('mr');
    var mrs = document.getElementById('mrs');
    var mx = document.getElementById('mx');
    var pers = document.getElementById('person');
    //get ID for the textarea
    var usrInput = document.getElementById('message-me');

    if (myForm.usrname.value == "") {
      message.innerHTML = "Moment! Wie heißt du?"
      return false;
    } else if (usrInput.value == "") {
      message.innerHTML = "Das Nachrichtenfeld sollte nicht leer sein..."
      return false;
    } else if (email.value == "") {
      message.innerHTML = "Wie lautet deine Email Adresse?"
      return false;
    } else if (usrInput.value.length < 10) {
      //check min length of textarea
      message.innerHTML = "Die Nachricht sollte etwas länger sein..."
      return false;
    } else if (!mr.checked &&
      !mrs.checked &&
      !mx.checked &&
      !pers.checked) {
      message.innerHTML = "Welche Ansprache darf ich verwenden?"
      return false;
    } else {
      message.innerHTML = "";
      return true;
    }

I'm using XAMPP (Apache) to test this via localhost.

How can I get the form to send POST, not GET? Did I overlook syntax errors, typos or are there errors with my variables that I can't find? Thx for any input.

  • 写回答

1条回答 默认 最新

  • dongpu1331 2019-06-08 10:18
    关注

    This happens sometimes. I don't think I am experienced enough to give you a perfect solution but here's a bunch of try outs you can implement:

    1. Try to write the "post" keyword in capitals (like "POST").
    2. Sometimes the xampp server does not reflect changes even after you save and refresh. Try refreshing with (ctrl+F5). This imposes a hard refresh causing the xampp server to reload all the resourses.
    3. Try restarting the xampp server.

    Please revert if anything was helpful....

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题