Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,548 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,226 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Beginner question about getting information from a database

 
Reply to this topicStart new topic

Beginner question about getting information from a database

cygnusX
post 28 Jul, 2008 - 10:23 AM
Post #1


D.I.C Head

**
Joined: 19 May, 2007
Posts: 143


My Contributions


I want to retrive information from a database with AJAX/PHP.I don't know how to make the returned from the database information available to the javascript code,there is xmlHTTP.responseText variable,i don't know how to use it.Basically my question is: how to make the information available to to responseText?

JavaScript code
CODE
<script>
   function getInformation()
   {
    var xmlHttp;
    try
      {
         xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      try
      {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
     }
    }
    xmlHttp.onreadystatechange=changeContent();
    xmlHttp.open("GET","bla.php",true);
    xmlHttp.Send(null);
   }
  
   function changeContent()
{
if (xmlHttp.readyState==4)
{ alert(xmlHttp.responseText);
  document.getElementById("div1").innerHTML=xmlHttp.responseText;
}
}
</script>

PHP code
CODE
<?php

  $connection = mysql_connect('localhost','root','');
  if(!mysql_select_db('products'))
  {
   exit(mysql_error());
  }
  $result = mysql_query('SELECT * FROM boots',$connection) or die(mysql_error());

  while($products = mysql_fetch_array($result))
  {

  }
  mysql_close($connection);
?>
User is offlineProfile CardPM

Go to the top of the page


BetaWar
post 28 Jul, 2008 - 10:45 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,266



Thanked 38 times

Dream Kudos: 775
My Contributions


You have to echo/ print out the information you want the javascript to be able to get to. All it (the js) does is take the output from the file and use that as responseText, that is the reason you can have an ajax query used on any type of file (html, txt, etc.).

Try this:

CODE

while($products = mysql_fetch_array($result)){
  echo $products['TABLE FIELD NAME'];
}


Hope that helps.

This post has been edited by BetaWar: 28 Jul, 2008 - 10:45 AM
User is online!Profile CardPM

Go to the top of the page

cygnusX
post 28 Jul, 2008 - 11:04 AM
Post #3


D.I.C Head

**
Joined: 19 May, 2007
Posts: 143


My Contributions


Yeah,probably you're right and i'll try that but i can't right now because the readyState is not '4' and the code in if block in the 'changeContent' function is never executed.The 'changeContent' function is executed,i checked that so that's not the problem.It's something with the value of 'readyState' How can i see what's the readyState and what's wrong with it?I tried to put it in alert function to see it but nothing happens then.

This post has been edited by cygnusX: 28 Jul, 2008 - 11:05 AM
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 28 Jul, 2008 - 09:44 PM
Post #4


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,266



Thanked 38 times

Dream Kudos: 775
My Contributions


Try changing this xmlHttp.onreadystatechange=changeContent(); to this xmlHttp.onreadystatechange=changeContent;.

This: xmlHttp.onreadystatechange=changeContent(); has the function run and whatever it returns is set to the variable xmlHttp.onreadystatechange (which I am pretty sure you can't set). So the function is only being run a single time.

Hope that helps.
User is online!Profile CardPM

Go to the top of the page

cygnusX
post 29 Jul, 2008 - 03:29 AM
Post #5


D.I.C Head

**
Joined: 19 May, 2007
Posts: 143


My Contributions


No,that doesn't work also.I cannot understand that,the function(changeContent) is executed but there is no responseText.Thank you for your help anyway.
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 29 Jul, 2008 - 06:39 AM
Post #6


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,266



Thanked 38 times

Dream Kudos: 775
My Contributions


What does your whole php file look like? Something tells me that you aren't echoing the data out to the page.
User is online!Profile CardPM

Go to the top of the page

cygnusX
post 29 Jul, 2008 - 08:16 AM
Post #7


D.I.C Head

**
Joined: 19 May, 2007
Posts: 143


My Contributions


Here:
CODE

<?php

  $connection = mysql_connect('localhost','root','');
  if(!mysql_select_db('products'))
  {
   exit(mysql_error());
  }
  $result = mysql_query('SELECT * FROM boots',$connection) or die(mysql_error());

  while($products = mysql_fetch_array($result))
  {
   echo $products['size'];
   echo $products['price'];
  }
  mysql_close($connection);
?>
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 29 Jul, 2008 - 08:43 AM
Post #8


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,266



Thanked 38 times

Dream Kudos: 775
My Contributions


Okay, the PHP doesn't look to be the problem so I went through error checking your javascript.

Here was the issue. You declared xmlHttp as a variable inside of a function making it so no other function could use it without redeclaring it (at which point is would overwrite things it was already doing). So here is a working stript:

CODE
<script>
function http(){
    try
      {
         xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      try
      {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
     }
    }
  return xmlHttp;
}

   var xmlHttp = http();

   function getInformation()
   {
    xmlHttp.onreadystatechange = changeContent;
    xmlHttp.open("GET","HTML_editor.HTML",true);
    xmlHttp.Send(null);
   }
  
function changeContent(){
  if (xmlHttp.readyState==4){
    document.getElementById("d").innerHTML=xmlHttp.responseText;
  }
}
getInformation();
</script>


Hope that helps.
User is online!Profile CardPM

Go to the top of the page

cygnusX
post 29 Jul, 2008 - 09:11 AM
Post #9


D.I.C Head

**
Joined: 19 May, 2007
Posts: 143


My Contributions


Yeah,now it's ok,i can get the response from the server but the response is just the PHP script code.I mean i'm not getting the results of echo-es only(i suppose that is the normal behaviour)but the whole source code from the php script.
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 29 Jul, 2008 - 09:30 AM
Post #10


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,266



Thanked 38 times

Dream Kudos: 775
My Contributions


Now that doesn't sound normal. It must have something to do with the permissions set on the file or how php is set up on your server...

Maybe try placing the full URL of your php file in the ajax call and see if that works.

Not sure why it is acting up...
User is online!Profile CardPM

Go to the top of the page

cygnusX
post 29 Jul, 2008 - 11:26 AM
Post #11


D.I.C Head

**
Joined: 19 May, 2007
Posts: 143


My Contributions


QUOTE(BetaWar @ 29 Jul, 2008 - 09:30 AM) *

It must have something to do with the permissions set on the file or how php is set up on your server...

Yeah,maybe.

Ok,when i go to the 'www' directory of the server and open the PHP file in the browser directly from there the result is the same,the page contains the source code of the file.So,the address in the address bar of the browser is "file:///C:/wamp/www/products.php".But when i open the same file by typing "http://localhost/products.php" in the address bar everything is ok.So,yeah,maybe it's something with the permissions.Or maybe something with the PHP settings on the server.Unofrtunatelly i'm very new to web programming and do not know what to change.I doubt that PHP is not properly installed because as i said it works in case if i open the file by typing http://localhost/blabla.php.
I've readed many tutorials about this topic(retrieving data from a database with AJAX/PHP) and none of them helps me finding what's wrong with my code.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/7/08 10:25PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month