AJAX不传输数据

Bas*_*sse 1 php ajax jquery

我想将数据从一个页面传输到另一个页面.我有2个页面:hostSettings.php和test.php

好的,这是我的test.php它包括一个提交按钮和ajax/jquery脚本

<html>
  <head>
    <link href="CSS/style.css" type="text/css" rel="stylesheet" />
    <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    <script>

      $(document).ready(function() {
        $('#button').on('submit', function(e) {
          e.preventDefault();
          var test = "Hallo Welt!";
          $.ajax({
            url: "hostSettings.php",
            type: "POST", 
            data: { test : test },
            success: function (response) {
              console.log("data transmitted: " + response);
            },
            error: function(jqXHR, textStatus, errorThrown) {
              alert("Es ist ein Fehler aufgetreten!\n" + textStatus + "\n" + errorThrown);
              console.log(textStatus, errorThrown);
            }
          }); 

        });
      });
    </script>
  </head>

  <body>
    <font size="4">Test-Site</font>
    <hr>
    <?php include ("menu.html");?><br><br>

    <form method="POST" action="hostSettings.php">
      <input id="button" value="TEST" type="submit">
    </form>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

hostSettings.php:

    <html>
  <head>
    <link href="CSS/style.css" type="text/css" rel="stylesheet" />
    <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
  </head>

  <body>
    <font size="4">Hosts speichern</font><hr>
    <?php include ("menu.html");?><br><br>
    <br><br><br>

    <p><center>
      <h3>Diese Seite befindet sich momentan im Aufbau..</h3>
      <form action="index.php">
        <input type="submit" value="Zurück zum Index">
      </form>
    </p></center>

    <?php
      if( $_SERVER['REQUEST_METHOD']=='POST' ){
        if(isset($_POST["test"])) {
          echo $_POST["test"];
       }
      }
    ?>

  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

为什么它不会将数据从test.php传输到hostSettings.php :(

Pup*_*pil 6

submit 事件不是为提交按钮设计的.

你需要在表格上解雇它.

无论你的火事件submitform或对clickbutton.

改变这个:

$('#button').on('submit', function(e) {   
Run Code Online (Sandbox Code Playgroud)

至:

$('#button').on('click', function(e) {
Run Code Online (Sandbox Code Playgroud)

<input type="submit" value="Zurück zum Index">
Run Code Online (Sandbox Code Playgroud)

至:

<input type="button" value="Zurück zum Index">
Run Code Online (Sandbox Code Playgroud)

  • 总是喜欢一个解释清楚的答案;-) (2认同)