1

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...

2 Answers 2

1

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 */ }});
Sign up to request clarification or add additional context in comments.

2 Comments

The problem is the data field... How do I prepare the POST array so that id doesn't loose data... For example if I want to write the following variables a=1 and b=33 and c='šaize kaut kāda un vēl sazin žuķīļņšģīŗ'... Now a and b will be perfect, but c will be cut off at some point most likely loosing just the š ā ē ž ķ and so on... I need it to contain all possible characters and not ignore them as it is doing right now...
I would recommend as well to use jQuery or some other Ajax library.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.