开发者

Javascript, AJAX and PHP Help

开发者 https://www.devze.com 2023-03-27 00:28 出处:网络
I have the following table in a MYSQL database: Messages MessageId (PK) int(10) - auto_inc Message varchar(100)

I have the following table in a MYSQL database:

Messages
MessageId (PK) int(10) - auto_inc
Message varchar(100)

I have the following PHP that echos a particular message:

<?php

//..connect to database
$query = "SELECT Message FROM Messages WHERE MessageId = '1'";
$result = mysql_query($query);
$num = mysql_num_rows( $result );
if ($num == 1){
  $row = mysql_fetch_assoc($result);
  ech开发者_开发知识库o json_encode($row);
}else{
  echo('Invalid');
}

?>

Can anyone advise me on how best to integrate jQuery in order to allow the document browser window to write the response...


If you use jQuery, you can easily use jQuery.getJSON()[DOCS] as follows:

$.getJSON('mypage.php', function(data) { 
   //Do somewith with JSON data
});

For normal Javascript, use

var xmlhttp;
if(window.XMLHttpRequest)
   xmlhttp = new XMLHttpRequest();
else//IE5, IE6
   xmlhttp = ActiveXObject("Microsoft.XMLHTTP");

xmlhttp.onreadystatechange = function()
{
   if(xmlhttp.readyState == 4 && xmlhttp.Status == 200)
   {
      var response = JSON.parse(xmlhttp.responseText);
      //Do something with JSON Data
   }
};
0

精彩评论

暂无评论...
验证码 换一张
取 消