如何使用ajax Post方法在没有jQuery的情况下提交用户输入

时间:2018-05-26 08:32:58

标签: javascript php html ajax post

我正在尝试使用ajax post方法而非jQuery 制作提交表单的订阅者表单。我写了一个代码,但它没有正常工作,我认为我在发送POST数据时做错了。这是我迄今为止尝试过的代码。

    <div id="form" style="max-width:477px">

    <div class="imgContainer" id="myImageContainer">
<span onclick="document.getElementById('myLogin').style.display='none'" class="close" title="Close">&times;</span>

        </div>
        <div class="loginInfo" id="myLoginInfo">

          <label for="Full Name">
<b>Full Name</b>
</label>
<spam class="error" style="color:red">
<i id="nameErr"></i>
</spam>
          <input type="text" placeholder="Enter full name" name="name" id="name">

          <label for="Email"><b>Email</b></label>
<spam class="error" style="color:red">
<i id="emailErr"> </i>
</spam>

<input type="text" placeholder="Enter your Email" name="email" id="email">

          <button onclick="verify()">Subscribe</button>

        </div>


      </div>
    </div>


    <script>
    function verify(){
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            myObj = JSON.parse(this.responseText);
            document.getElementById("email").innerHTML = myObj.email;
            document.getElementById("emailErr").innerHTML = myObj.emailErr;
            document.getElementById("name").innerHTML = myObj.name;
            document.getElementById("nameErr").innerHTML = myObj.nameErr;

        }
    };
    xmlhttp.open("GET", "subs.php", true);
    var data="email=" + document.getElementById("email").value + "&name="+document.getElementById("name").value;

    xmlhttp.send(data);
    }
    </script>

并且PHP页面看起来像这样(使用HTML时它工作正常&lt; Form method =&#34; post&#34;&gt;

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $name =  $emailErr = $nameErr="";

function test($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}


  if (empty($_POST["email"])) {
      $emailErr=" *This Field is Required";
  }else{
    $email=test($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)){$emailErr = " *Invalid email format";}

 if (strlen($email)>60){$emailErr=" *Email cannot be larger than 60 character";}

  }

 if (empty($_POST["name"])) {
      $nameErr=" *This Field is Required";
  }else{
      $name = test($_POST["name"]);
      $name = preg_replace('/\s\s+/', ' ', $name);

if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  $nameErr = " *Only letters and white space allowed"; 
}
      if (strlen($name)>60)

  {$nameErr=" *Name cannot be larger than 60 character";}
 }
$json['email'] = $email;
$json['emailErr'] = $emailErr;
$json['name'] = $name;
$json['nameErr'] = $nameErr;
$myJSON = json_encode($json);

echo $myJSON;
}

当我点击订阅按钮时,它会给我空值而不是显示错误消息。

2 个答案:

答案 0 :(得分:0)

 xmlhttp.open("GET", "subs.php", true);

应该是

 xmlhttp.open("POST", "subs.php", true);

如果PHP代码需要POST请求。

答案 1 :(得分:0)

建议你发送带有Axios的ajax比Jquery和纯javascript更简单明了