0

I'm trying to write a simple function where ajax sends parameters to PHP service and then PHP requests MySQL for data and sends result to ajax function.I dont have much idea about AJAX. Can anybody suggest sample code or links.

7
  • 3
    What is your question, at which point are you stuck? What code do you have? Commented Apr 27, 2011 at 9:59
  • 1
    What are your table structures? What information do you want out of them? What format does the AJAX response need to be in? Have you made an attempt at this yourself and are having problems? Please give us some more information because there isn't enough to attempt to answer this question in any specific way. Commented Apr 27, 2011 at 10:03
  • Thanks for response @Pekka and @GordonM. When a user closes browser without logging out, I want to update his online status to '0' in DB. Here I'm using Flex as front end and PHP as server side language and DB is MySQL. When a user closes browser, I cant communicate with PHP via Flex. Thats why I am trying to send username to PHP service via AJAX. Commented Apr 27, 2011 at 10:32
  • Naveen, I know nothing about Flex. But even if it can send an ajax request just before the browser is closed your question is still undefined. What is the problem you are facing? Or you just have no idea on how to do it and want someone to give you the whole code? Commented Apr 27, 2011 at 10:56
  • @Kakao, I dont have much idea about AJAX. I wrote AJAX function window.onbeforeunload = clean_up; function clean_up() { var flex = document.${application} || window.${application}; ajaxRequest = new XMLHttpRequest(); var queryString = "?UserID=" + "8" + "&LogHistoryId=" + "12284" ; ajaxRequest.open("GET", "ajaxEx.php" + queryString, true); ajaxRequest.send(null); flex.myFlexFunction(); } I think, I have to give a proper path of the php file. Can you tell me how to give the path or where should I place the php file. I think no need of knowledge on Flex. Commented Apr 27, 2011 at 11:34

2 Answers 2

1

For the basics without using a framework, see http://www.w3schools.com/ajax/default.asp

I'd recommend using a JS Library such as jQuery, as this can hugely simplify your code.

Check out how jQuery implements ajax here - http://api.jquery.com/jQuery.ajax/

Sign up to request clarification or add additional context in comments.

Comments

1

I would try jQuery, try this code snippet:

//Listen when a button, with a class of "myButton", is clicked
//You can use any jQuery/JavaScript event that you'd like to trigger the call
$('.myButton').click(function() {
//Send the AJAX call to the server
  $.ajax({
  //The URL to process the request
    'url' : 'page.php',
  //The type of request, also known as the "method" in HTML forms
  //Can be 'GET' or 'POST'
    'type' : 'POST',
  //Any post-data/get-data parameters
  //This is optional
    'data' : {
      'paramater1' : 'value'
      'parameter2' : 'another value'
    },
  //The response from the server
    'success' : function(data) {
    //You can use any jQuery/JavaScript here!!!
      if (data == "success") {
        alert('request sent!');
      }
    }
  });
});

Hope that helps,
spryno724

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.