php - 我想使用单个变量将多条数据发送到数据库(使用 JS、PHP 和 AJAX)

标签 php javascript mysql ajax

我不知道编程的正确术语或行话,所以如果这个问题不清楚,请原谅我。在这里更清楚的是我到目前为止编写的代码。我将在代码后更详细地解释问题:

我使用此代码从 Facebook 检索数据:

function rsvpEvent() {
    FB.api('/me/events/', function(response) {
        for (var i=0; i<=2; i++) {
            eventname=response.data[i].name;
            eventid=response.data[i].id;
            eventstatus=response.data[i].rsvp_status;
            eventInfo(eventname,eventid,eventstatus);
            strEvent=strEvent + "You are " + 
                eventstatus + " " + eventname + ", EID: " + eventid + "<br/>";
        }

        document.getElementById('rsvpEvent').style.display = "block";
        document.getElementById('rsvpEvent').innerHTML = strEvent;
    });
}

请求 PHP 文件(包含 mySQL 调用):

function eventInfo(eventname,eventid,eventstatus) {             
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }

    else {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("eventInfo").innerHTML=xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET","event_info.php?eventName=" + eventname + "&eid=" + eventid +  
        "&rsvp_status=" + eventstatus,true);
    xmlhttp.send();
}

所以,问题是我需要存储各个事件名称、ID 和状态以及现在的代码。我可以将它们单独输出到一个页面,但不能将它们单独发送到 PHP 文件。我该怎么做(假设这是可能的)?

最佳答案

将 jQuery 用于对 php 脚本的 ajax 调用。

$.post('/somefile.php', myJSobjectOrArr, function(data) {

// return something from your script to say it succeeded or not...
        console.log(data);

}

在您的 php 脚本中,只需检查您的 $_POST 变量和 serialize() 或 json_encode() 并将其弹出到您的数据库中。

要取回您的数据,只需反序列化 () 或 json_decode() 即可。

关于php - 我想使用单个变量将多条数据发送到数据库(使用 JS、PHP 和 AJAX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4467228/

相关文章:

php - 我应该使用 WordPress 还是定制?

javascript - 从数据库加载时网页挂起,并且大数据未显示在 asp.net mvc5 的图表上

javascript - parseInt 替代方案

mysql - 优化MySQL查询,

php - MYSQL 和 PHP 对结果进行分组

php - 模拟对象后缺少函数?

javascript - Jquery:表单验证不起作用

javascript - Bootstrap 4 警告框关闭按钮未对齐

php - 使用下拉菜单更新数据库而不使用提交按钮

php - 使用 HttpURLConnection 使用 php 脚本将数据从 Android POST 到 MYSQL