简体   繁体   English

使用php和mysql的ajax

[英]ajax using php and mysql

<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 .. ajaxlistpatient.php包含我的sqlquery ..

try using jQuery library for this, because ajax operation is quirky and complicated. 尝试使用jQuery库,因为ajax操作是古怪而复杂的。

Take a look at this document: 看看这个文件:

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

You can't access the DOM like this $('patientlist') . 您不能像这样的$('patientlist')访问DOM。 It has to be either $('.patientlist') or $('#patientlist') 它必须是$('.patientlist')$('#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
            }
        });

});

Even though you havn't tagged Jquery in your question it looks like Jquery is being used for the click event anyway. 即使你没有在你的问题中标记Jquery,看起来Jquery仍然被用于click事件。 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. 确保你可以访问firefox中的firebug,这将有助于你大规模使用,这样你就可以看到发出的ajax调用,并通过firebug控制台查看响应。 Chrome also has a console and debug tools but I prefer firefox/firebug. Chrome还有一个控制台和调试工具,但我更喜欢firefox / firebug。

You could juse use the jQuery ajax method: 您可以使用jQuery ajax方法:

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

Here is the general and simplest syntax of using ajax: 以下是使用ajax的一般和最简单的语法:

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

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

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: 所有回复都是正确的方向,Jquery + php是你最好的选择,为了进一步扩展这些回复,这里有一些以前类似的帖子可能会帮助你有一个参考:

AJAX to call PHP file which removes a row from database? AJAX调用PHP文件从数据库中删除一行?

AND

How to pass jQuery variables to PHP variable? 如何将jQuery变量传递给PHP变量?

are good sample resources, 是很好的样本资源,

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM