1032

Javascript array for AJAX POST send

Question:

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

Answer1:

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

<a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">http://api.jquery.com/jQuery.ajax/</a>

Instead of doing all that you'd simply do:

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

Answer2:

create <strong>custom parameter</strong> 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 <strong>content</strong>.

Recommend

  • algorithm to iterate through fixed size positive integer lists with the same sum
  • Post YouTube video into a Facebook Event Wall
  • decimal separator oracle
  • Set focus to newly added row in ui-grid
  • What SynchronizationContext does Task.ContinueWith use?
  • About Object.hashcode() and collisions
  • Recommended way to handle problems/errors in algorithms
  • Font family does not change in the output
  • Loss increases after restoring checkpoint
  • difference between document.cookie and res.cookie
  • Zero downtime deployment Nodejs application
  • Does anyone know about Kephas?
  • Salesforce consuming XML and display data in Visualforce report
  • PHP Append to Excel file
  • Showing image on a acro text field position
  • Multiple canvases (pages) in Fabric.js
  • GTK3 + OpenGL for windows: multiple GtkGLArea (ie. OpenGL contexts)
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • How to define a custom accuracy in Keras to ignore samples with a particular gold label?
  • C++ Is “const void” As Return Value More Const-Correct Than “void”? [duplicate]
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • Annotate objects in a queryset with next and previous object ids
  • I am consuming a WCF service that requires headers from a .NET 2 website. How can I programmatically
  • how to run ejabberd with Erlang on Heroku?
  • How to use Streams api peek() function and make it work?
  • How to specify generic type when the type is only known at runtime?
  • How to decleare char *const argv[] in swift [duplicate]
  • How to encrypt Connectionstring written in web.config from codebehind?
  • How to get rgb from transparent pixel in js
  • Grails - How to implement a foreign key relationship not using an id column?
  • Capture SIGFPE from SIMD instruction
  • Using Service Component Runtime
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • Firebase: How to read from external DB?
  • WPF custom control and direct content support
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • Create/delete users from text file using Bash script
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • How to check if object is null in Java?