1

I have a call of an ajax function and I want send a var like:

     <a href="next.php" onclick="sendajax()">reload</a>

from my php page for send this with ajax function POST:

  <script>
 function sendajax(){   
    var xmlObj=new XMLHttpRequest();

   xmlObj.open("POST","miscript.php", true);

   xmlObj.setRequestHeader("content-type","application/x-www-form-urlencoded");

   xmlObj.onreadystatechange = function(){
    if(xmlObj.readyState == 4 && xmlObj.status == 200){
        document.getElementById("response").innerHTML = xmlObj.responseText;
   }
  }
   xmlObj.send('name=Status');
 }
 </script>

How can I add this var to send like?:

  function sendajax(myVar){ 
 ....
  xmlObj.send(myVar);

I'm trying add this, but only send text.

Thanks!

EDIT I think that my question ins't duplicate, because I want send vars from the link clicked, not write this into var like:

    var params = "lorem=ipsum&name=binny";

I know how to write this and pass this, I ask: how to send this var to function and add/concatenate this into string. Without use form.. Thanks!!!!!!!

I think that I explained this correctly. Explained

user3745888
  • 5,443
  • 14
  • 43
  • 85

2 Answers2

0

Usually, I use $.ajax()... Like this:

$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });

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

DZanella
  • 243
  • 1
  • 8
0

Why you dont use jQuery?

You can pass the button as this to the function.

<a href="#" onclick="sendajax(this)">clickme</a>
function sendajax(button) {
    var myVar = $(button).html(); // Example. Take some datas from the button

    $.ajax({
        url: 'miscript.php',
        type: 'POST',
        data: { param1: myVar, param2: 'something_else'} ,
        success:function(dataFromAjaxResponse, typeFromAjaxResponse) {
            // When the request was successful proceed coding here
            // Variable dataFromAjaxResponse is the response (output) from your php site. 
        },
        error: function(dataFromAjaxResponse) {
            console.log(dataFromAjaxResponse);
        }
    });
}
Fabian Picone
  • 1,696
  • 3
  • 20
  • 33