简体   繁体   English

如何使用AJAX发布方法从PHP的特定函数中检索JSON

[英]How can I retrieve JSON from PHP's specific function using AJAX post Method

Ok so this is the problem I'm facing right now 好的,这就是我现在面临的问题

Actually I've successfully accessed JSON inside php, but with one condition, there is no specific function defined, and right now, I need to access the JSON from specific PHP function 实际上,我已经成功地在php中访问了JSON,但是在一种情况下,没有定义特定的函数,现在,我需要从特定的PHP函数访问JSON。

So this is my php looks like 所以这是我的PHP看起来像

<?php

if(isset($_GET['func']))
{
    if($_GET['func'] == "add")
       addVisitor();
}
elseif(!empty($_POST['func']))
{
    if($_POST['func']=="retrieve"
        getData()
}

function addvisitor()
{
    $servername = "localhost";
    $username = "root";
    $password = "@123";
    $dbname = "numberofvisit";
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "UPDATE visitor SET count = count +1 WHERE idvisitor = 1 ";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
}

function getData(){
    $servername = "localhost";
    $username = "root";
    $password = "@123";
    $dbname = "rating";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 


    $sql = "SELECT avg FROM feedback WHERE idoveralRating=1";
    $result = $conn->query($sql);

    $pass=mysqli_fetch_assoc($result);

    echo json_encode($pass["avg"]);

    $conn->close();
}
?>

Here is My Javascript 这是我的Javascript

Ok, So Im trying to send request to server to run specific function, by send func=retrieve to addVisit.php and then after run the function,I catch the JSON that the server Echoed, but it seems like it doesn't work like I expected, here is the part that the code that the part doesn't work for me 好的,所以我试图向服务器发送请求以运行特定功能,方法是将func = retrieve发送到addVisit.php,然后在运行该功能后,我捕获了服务器回显的JSON,但似乎无法正常工作我期望,这是该部分不适用于我的代码

var req = new XMLHttpRequest();
req.open("POST", "addVisit.php", true);
req.send("func=retrieve");
var jsonObject = JSON.parse(req.responseText);

So How to access JSON in specific function of PHP using ajax? 那么,如何使用ajax在PHP的特定功能中访问JSON?

PHP is case-sensitiv with its funciton names. PHP的功能名称区分大小写。 You try calling addVisitor() 您尝试调用addVisitor()

...

if($_GET['func'] == "add")
   add**V**isitor();
}

...

But the function name you've defined is: 但是您定义的函数名称是:

function add**v**isitor()

This causes Problems 这会导致问题

Which also is pretty helpful for debugging javascript is to use the developer toolkit, often default in your browser (eg Chrome or Opera has it by default) You can dump your javascript variable, calls, returns and debug them by extending your with something like this 这对于调试javascript也非常有帮助,它是使用开发者工具包(通常在浏览器中默认使用的)(例如,Chrome或Opera在默认情况下具有此工具包)。

console.log(variable_name_goes_here)

try this :- call this getData function on button click 尝试一下:-在单击按钮时调用此getData函数

function getData()
{
var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("demo").innerHTML = xhttp.responseText;
    }
  };
  xhttp.open("POST", "addVisit.php", true);
  xhttp.send();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM