开发者

I want to send multiple piece of data using a single variable to a database (using JS, PHP and AJAX)

开发者 https://www.devze.com 2023-01-30 20:16 出处:网络
I am not aware of the proper terminology or jargon with programming, so forgive me if this question is unclear. To make if more clear here is the code I have written so far. I will explain the problem

I am not aware of the proper terminology or jargon with programming, so forgive me if this question is unclear. To make if more clear here is the code I have written so far. I will explain the problem in more detail after the code:

I use this code to retrieve data from 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;
    });
}

Request to PHP file (containing mySQL calls):

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();
}

So, the issue is that I need to store individual event names, ids and statuses and the code as it stands now. I can output them to a page individually, but cannot send them to the PHP file individually. How do I do this (presuming it is possible)?


Use jQuery for your ajax call to your php script.

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

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

}

In your php script just check your $_POST variable and serialize() or json_encode() it and pop it into your database.

To get your data back just unserialize() or json_decode() it.


You can join eventnames with comma(,) ,and when reading those data to save into database you can delimit comma(,), means remove commas from that and store each event name to database.. This is what i understand according to your problem..

0

精彩评论

暂无评论...
验证码 换一张
取 消