如何间接调用php文件的ajax与post类型,并获得json返回然后解析到表


How to indirect call php file by ajax with post type and get json return then parse it to table

我有2个php文件,一个是"Db_function.php"answers"index.php", "index.php"将在"Db_function.php"中进行函数调用。我想通过ajax调用"index。php"这是我的代码

Html文件

<Html>
  <body>
      <script type="text/javascript" src="js/index.js"></script>
      <input type="button" value="Get" onclick="get()">
  </body>
</html> 

Js文件

function get() {
    $.ajax({ url: 'index.php',
             data: {tag:'get_backup', email:'mail@mail.com', password:'123'},
             type: 'post',
             datatype:'json'
             success: function(output) {
                          alert(output);
                      }
    });
}

index . php

/*with connect value*/
if ($tag == 'get_backup'){
            // store user
              $email = $_POST['email'];
              $password = $_POST['password'];
            $user = $db->get_backup($email, $password);

Db_funtion.php

public function get_backup($email, $password) {
        $result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
        // check for result 
        $no_of_rows = mysql_num_rows($result);
        if ($no_of_rows > 0) {
            $result = mysql_fetch_array($result);
            $salt = $result['salt'];
            $encrypted_password = $result['encrypted_password'];
            $hash = $this->checkhashSSHA($salt, $password);
            // check for password equality
            if ($encrypted_password == $hash) {
                // user authentication details are correct
                $response = array();
                $result2 = mysql_query("SELECT * FROM contact_store WHERE (email ='$email' AND backup_name='$email')") or die(mysql_error());
                if (mysql_num_rows($result2) > 0) {
    // looping through all results
    // contact_stores node
    $response["contact_stores"] = array();
  while ($row = mysql_fetch_array($result2)) {
        // temp user array
        $contact_store = array();
        $contact_store["backup_name"] = $row["backup_name"];
        $contact_store["email"] = $row["email"];
        $contact_store["data"] = $row["data"];
        // push single contact_store into final response array
        array_push($response["contact_stores"], $contact_store);
    }
   // success
    $response["success"] = 1;
    // echoing JSON response
    echo json_encode($response);
} else {
    // no contact_stores found
    $response["success"] = 0;
    $response["message"] = "No contact_stores found";
    // echo no users JSON
    echo json_encode($response);
}
            }
        } else {
            // user not found
            return false;
        }
    }

Json返回如下

{"contact_stores":[{"backup_name":"nhi@gmail.com","email":"nhi@gmail.com","data":"[]"}],"success":1}

但是我的问题是当我在html文件中按下get按钮时,什么都没有发生。有什么需要帮助的吗?

您似乎没有定义您在输入元素中提供的get()函数。

试试这个HTML:

<input id="get" type="button" value="Get">
$('#get').click(function() {
    $.ajax({ url: 'index.php',
        data: {tag:'get_backup', email:'mail@mail.com', password:'123'},
        type: 'post',
        datatype:'json'
        success: function(output) {
            alert(output);
        }
    })
});