Ajax成功函数无法正常工作

时间:2017-02-09 09:55:12

标签: javascript ajax

我制作了一个简单的ajax / php表单,但由于某种原因,我的成功函数无法正常工作。我仍然收到电子邮件,所以我想条件是真的,但没有出现,提交按钮没有被阻止。这是我的代码:

int main() {
   Register<int,int>(State::Dead());
   Register<int,int>(State::Alive());
   return 0;
}
function myFunction() {
          var name = document.getElementById("name").value;
          var message = document.getElementById("message").value;
          var company = document.getElementById("company").value;
          var phone = document.getElementById("phone").value;
          
        // Returns successful data submission message when the entered information is stored in database.
        var dataString = 'name1=' + name + '&message1=' + message + '&company1=' + company + '&phone1=' + phone;
          if (name == '' || message == '' || company == '' || phone == '') {
             document.getElementById("error").style="display: block; color: red;";
        } else {
          
          
        // AJAX code to submit form.
          $.ajax({
            type: "POST",
            url: "email.php",
            data: dataString,
            cache: false,
            success: function() {
              document.getElementById("success").style="display: block; color: green;";
            }
          });
        }
        return false;
      }

  1. 列表项

2 个答案:

答案 0 :(得分:2)

您不能将样式属性设置为<class name="Trail" table="sample"> <id name="upc" type="long"> <column name="upc" /> <generator class="identity" /> </id> <property name="ProductName" type="string"> <column name="product name" not-null="true" /> </property> <property name="Producturl" type="string"> <column name="producturl" not-null="true" /> </property> <join table="Example"> <key column="productname"></key> <property name="price" type="double" > <column name="price"/> </property> </join> 的字符串。单独设置每个样式(.style.display,。style.color,...)或使用

el.style

答案 1 :(得分:1)

这是你的最终代码,对我来说很好

<!DOCTYPE HTML>
 <html>

<head>

  <meta charset="utf-8">

  <title>AJAX + PHP форма</title>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

  <style type="text/css">

    .input_group {

      display:inline-block;
      padding: 5px;
      width:100%;
      text-align: center;

    }

    form {

      width: 50%;

    }

    #send_message {
      text-align: center;
    }

  </style>


</head>

<body>

  <form id="contact" action="">

    <fieldset>

      <legend>AJAX + PHP форма</legend>

        <div class = "input_group">

          <label for="name" id="name_label">Имя</label> <br/>

          <input type="text" name="name" id="name" size="50" value="" class="text-input" required = "required"/>

        </div>

        <br/>

        <div class = "input_group">

          <label for="company" id="company_label">Компания</label> <br/>

          <input type="text" name="company" id="company" size="50" value="" class="text-input" required = "required" />

        </div>

        <br/>

        <div class = "input_group">

          <label for="phone" id="phone_label">Телефон</label> <br/>

          <input type="text" name="phone" id="phone" size="50" value="" class="text-input" required = "required" />

        </div>

        <br/>

        <div class = "input_group">

          <label for="msg_text" id="msg_label">Запрос</label> <br/>

          <textarea rows="6" cols="51" name="question" id="message" required = "required"></textarea>

        </div>

        <div class = "input_group">

          <input type="button" onclick="myFunction()" id="submit" value="Отправить" />

        </div>

      </fieldset>

  </form>

  <h2 style="display:none;" id ="error">Заполните все поля!</h2>
  <h2 style="display:none;" id="success">Message sent!</h2>

<script>
    function myFunction() {
      var name = document.getElementById("name").value;
      var message = document.getElementById("message").value;
      var company = document.getElementById("company").value;
      var phone = document.getElementById("phone").value;

    // Returns successful data submission message when the entered information is stored in database.
    var dataString = 'name1=' + name + '&message1=' + message + '&company1=' + company + '&phone1=' + phone;
      if (name == '' || message == '' || company == '' || phone == '') {
         document.getElementById("error").style="display: block; color: red;";
    } else {

    // AJAX code to submit form.
      $.ajax({
        type: "POST",
        url: "demo.php",
        data: dataString,
        cache: false,
        success: function(data) {
            alert(data)
          $('#success').css({display: 'block', color: 'green'});
        }
      });
    }
    return false;
  }
</script>

这是演示php文件

<?php
print_r($_REQUEST);
 ?>

只需更新按钮类型提交到按钮