user663184
user663184

Reputation: 11

Javascript array for AJAX POST send

Here is the deal... I need to make an AJAX save script. I have a whole system built on php and every action needs a refresh... I'm trying to minimize the refresh count by using AJAX ... I can't seem to find a way how to send a WYSIWYG editor output without loss to the PHP script...

  if (window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
}
else{
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
function save(){
    xmlhttp.open('POST','action.php',true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", document.getElementById('output').value.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(document.getElementById('output').value);
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status==200){
            $('#ajaxresult').css('opacity', 0.1);
            $('#ajaxresult').stopAll().pause(1000).fadeTo(400,1);
            $('#ajaxresult').stopAll().pause(3000).fadeTo(400,0, function(){$(this).hide();});
            document.getElementById('ajaxresult').innerHTML=xmlhttp.responseText;
        }
    }
}

While this script works fine I can't seem to find the way what kind of array to give the send option... what is the syntax or is there something I don't know?

BTW I'm a beginner in JS...

Upvotes: 1

Views: 646

Answers (2)

Abhishek Suthar
Abhishek Suthar

Reputation: 674

create custom parameter in the javascript code like below:

   var jspNameParam = "content="+escape(document.getElementById('output').value);
    function myFunction() {
        if (xmlhttp) {
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                /*  want to accsess some data written from action.php */
                }   
            };          
            xmlhttp.open("POST", "action.php", true);   
            xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlhttp.send(jspNameParam);
        }
    }

Now in action.php you will get whole content with the parameter name content.

Upvotes: 1

Justin Thomas
Justin Thomas

Reputation: 5848

I'd look into using jQuery and it's Ajax library:

http://api.jquery.com/jQuery.ajax/

Instead of doing all that you'd simply do:

$.post({url: 'action.php',data: output,success: function() { /* do something here */ }});

Upvotes: 1

Related Questions