0
<html>
<head>
<script>
$('patientlist').click(function showpatient()
{ 
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
   xmlhttp.open("POST","ajaxlistpatient.php",true);
  xmlhttp.send();         
 })
  </script>
  </head>
   <body>
  <form>
  <input type="button" id="patientlist" name="patientlist" value="List Patient"/>
  </form>
 </body>
 </html>

please help, i want to list my patientlist using a button in the same page without reloading my mainpage.

ajaxlistpatient.php contains my sqlquery ..

1
  • Are you using jQuery here? It looks like you are, but somehow you've sort of re-implemented the ajax feature. The documentation for that library is quite good, so it's worth searching through it before creating your own solutions. Commented Sep 5, 2013 at 14:55

6 Answers 6

1

You can't access the DOM like this $('patientlist'). It has to be either $('.patientlist') or $('#patientlist')

Assuming 'patientlist' is a class,

$('.patientlist').click(function (){ 

   console.log("Clicked");  //Check you got here without any problem

   $.ajax({
            type: "POST",
            url: "ajaxlistpatient.php",
            dataType: 'json',
            success: function (data) 
            {
                console.dir(data); 
            },
            error: function (jqXHR,textStatus,errorThrown)
            {
                //Check for any error here
            }
        });

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

6 Comments

still i cant see my result =(
@JohnChristianDeChavez no worries. You just need spend little more time on this. Can you show where is ajaxlistpatient.php is located? Is it placed at the root directory of your project?
yes sir.. i saved your code as use.js in the root so my root folder contains this use.js ajaxlistpatient.php index.php index.php now my code is <script type="text/javascript" src="use.js"></script><html> <head> </head> <body> <form> <input type="button" id="patientlist" name="patientlist" value="List Patient"/> </form> </body> </html>
Cool. so clearly you are missing the Jquery library in your html. Take a look at this learn.jquery.com/about-jquery/how-jquery-works and also go through every single page of w3schools.com/jquery.. Happy Coding!!
oops i forgot to add the library this now my new code for index.php <script type="text/javascript" src="js/bootstrap.js"></script> <script type="text/javascript" src="js/bootstrap-tooltip.js"></script> <script type="text/javascript" src="js/bootstrap-popover.js"></script> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="use.js"></script><html> <head> </head> <body> <form> <input type="button" id="patientlist" name="patientlist" value="List Patient"/> </form> </body> </html>
|
1

try using jQuery library for this, because ajax operation is quirky and complicated.

Take a look at this document:

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

1 Comment

Absolutely. Writing AJAX code by hand is a super bad idea. Use a library or you will get it wrong.
0

Even though you havn't tagged Jquery in your question it looks like Jquery is being used for the click event anyway. So here you go:

$('#patientlist').on('click', function(e){
      e.preventDefault();
      e.stopPropagation();
      $.ajax({
           url : "ajaxlistpatient.php",
           type: "post",
           complete: function(d){
              //do something with the return data'd'
           }
      });
});

You will need to decide what you want to do with the response. Make sure you have access to something like firebug in firefox which will help you out massivly so you can see the ajax call being sent out and see the response via the firebug console. Chrome also has a console and debug tools but I prefer firefox/firebug.

Comments

0

You could juse use the jQuery ajax method:

$('#patientlist').click(function showpatient() {
    $.ajax({
        type: 'POST',
        dataType: "xml",
        url: 'ajaxlistpatient.php',
        success: function (data) {
            $('#myContentContainer').html(data);
        }
    });
});

1 Comment

what do you mean by the container? sorry if beginner
0

Here is the general and simplest syntax of using ajax:

$('patientlist').click(function ()
{

        $.post('ajax_files/ajaxlistpatient.php', {var_id:anyData},
        function(data) 
        {
            $('#yourDataHolder').html(data); 
        });
});

2 Comments

what should i do the dataholder? sorry beginner here
This is the div or span etc where you want to list your data coming from ajax file . e.g, <div id="yourDataHolder"></div>
0

All responses are in the right direction, Jquery+php is your best bet, and to further extend those responses, here are some previous similar posts that might help you to have a reference:

AJAX to call PHP file which removes a row from database?

AND

How to pass jQuery variables to PHP variable?

are good sample resources,

hope this helps.

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.