为什么我无法通过AJAX调用进入php页面?

时间:2017-12-03 15:40:45

标签: php html ajax

下面给出了我的HTML和PHP代码,我能够在txt中收到mail.value但在AJAX调用期间摇摇欲坠,它甚至没有输入错误。

有人可以帮我纠正错误,这样我就可以传递AJAX调用并将值发送到php页面。

在登录页面上有一段相同的代码可供我使用,但它不适用于newaccount页面。

HTML code:

<script src="jquery-3.2.1.js"></script>
<script>

function check()
    {
       var mail=document.getElementById("mail");
       var password=document.getElementById("password");
       var First_name=document.getElementById("First_name");
       var Last_name=document.getElementById("Last_name");
       var st_number=document.getElementById("st_number");
       var st_name=document.getElementById("st_name");
       var zip=document.getElementById("zip");
       var Phone=document.getElementById("phone");
       var Gender=document.getElementsByClassName("gender");
       var age=document.getElementById("age");
       var account=document.getElementById("account");
       document.getElementById("txt").innerHTML=mail.value;

        $.ajax({
url: "newacc.php?",
data: { 
    "account": account.value,
    "Email": mail.value,
    "Pass": password.value,
    "FirstName": First_name.value,
    "LastName": Last_name.value,
    "Phone":Phone.value,
    "age": age.value,
    "Gender": Gender.value,
    "st_number": st_number.value,
    "st_name": st_name.value,
    "zip": zip.value
},
    cache: false,
    type: "POST",
    success: function(response) {
     document.getElementById("txt").innerHTML= "this is it";

    },
    error: function(xhr) {
      document.getElementById("txt").innerHTML= "No";
    }
});


  }


</script>

PHP代码:

<?php
echo "Check";
$servername ="localhost";
$username = "root";
$password ="root";
$dbname ="pitt_store";
$conn = new mysqli($servername, $username, $password,$dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected so successfully";
$account = $_POST["account"];
echo "The address is $account";
$email = $_POST["Email"];
echo "The email is $email";
$password = $_POST["Pass"];
echo "The Password is $password";
$first_name = $_POST["FirstName"];
echo "The FNAME is $first_name";
$last_name = $_POST["LastName"];
echo "The LNAME is $last_name";
$phone = $_POST["Phone"];
echo "The phone number is $phone";
$age = $_POST["age"];
echo "The age is $age";
$gender = $_POST["Gender"];
echo "The gender is $gender";
$st_number = $_POST["st_number"];
echo "The street number is $st_number";
$st_name = $_POST["st_name"];
echo "The street name is $st_name";
$zip = $_POST["zip"];
echo "The zip is $zip";

/*$sql= "INSERT INTO Customer(first_name,last_name,email,password) VALUES('$first_name'
,'$last_name','$email','$password')";
if ($conn->query($sql) == TRUE)
{
  echo "Record added successfully";
} 
else 
{
  echo "Error adding record: " . $conn->error;
}*/
$conn->close();
?>

1 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为我的一个参数给出了一个未定义的值。