Jump to content


Photo

ajax call to php problem


  • Please log in to reply
10 replies to this topic

#1 tinks

tinks

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 19 December 2010 - 11:00 PM

hi,

I am currently working on linux env.
I have PHP, JS and MySQL.

I am exploring ajax to use.

I tried w3shools and the example was working fine with windows.. please see link:
http://www.w3schools...ax_database.asp

However, this same example does not work in my firefox browser in linux. I am currently on VMware.

I tried this code because my code also does not work when httpRequest calls my php file.

Can you help me whats wrong with this please ?
thanks.

I tried calling PHP without using xmlhttprequest in linux and it was working fine.
I dont know whats wrong...when i use async call it does not work.

thanks much,
tinks






#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 20 December 2010 - 11:36 PM

Could you provide a link to your code on the web so we can play with it and see what's going wrong?

#3 tinks

tinks

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 21 December 2010 - 08:12 PM

Could you provide a link to your code on the web so we can play with it and see what's going wrong?



hi,

its basically just this example i am trying out..

http://www.w3schools...ax_database.asp

i just modified it a bit for my local config..

im attaching the two files (sample.html; getuser.php)

thanks a lot,
tinks

Attached Files



#4 tinks

tinks

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 21 December 2010 - 09:43 PM

additional info:

i installed the firebug and it says:

uncaught exception: [Exception... "Component returned failure code: 0x805e000a [nsIXMLHttpRequest.open]" nsresult: "0x805e000a (<unknown>)" location: "JS frame :: file:///C:/wamp/www/sample.html :: showUser :: line 32" data: no]


i read about this error and said something about AdBlock..i checked my add-ons and i dont even have adBlock installed..

what could possibly be wrong. im really stuck with this..

thanks



#5 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 22 December 2010 - 12:11 AM

Ahh yes. It appears as though you have wamp installed, which is a good thing. To fix your issue, you should just need to make sure you are loading the page from http://localhost/sample.html, and not file:///C:/wamp/www/sample.html.

AJAX requests aren't allowed from "local files", you need to run them from a server. So make sure wamp is running, you're using 'localhost' as your domain, and you should be good. You'll also need to change the AJAX request url inside of sample.html to refer to root instead of your c: drive.

#6 tinks

tinks

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 22 December 2010 - 12:56 AM

Hi!!

thanks a lot. instead of just writing the filename since they are on the same location, I
added http://localhost/filename.php, it can now open the php file that ajax is calling..thanks a lot.

however, i still have a problem with the result of the query, firebug gives out this error:

-------------------------------------------------------
Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>C:\wamp\www\getuser.php
-----------------
this is the whole php file:

<?php$q=$_GET["q"];$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db("gdg", $con);$sql="SELECT * FROM useraccount WHERE id = '".$q."'";$result = mysql_query($sql);echo "<table border='1'>
<tr>
<th>UserID</th>
<th>Name</th>
</tr>";(it says the error is here--->)while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['userID'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";mysql_close($con);
?>















why is it not allowed?


thanks a lot for the help,
tinks



#7 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 23 December 2010 - 12:16 AM

That's not a problem with the AJAX call, it's a problem with the PHP, or more specifically the MySQL/database.

See: http://www.pixel2lif...showtopic=36853 for debugging tips.

#8 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 23 December 2010 - 05:29 PM

It may just be older versions of PHP, but I know from my own experiences with having apache/php/mysql running on my own computer that PHP is very particular about sql queries. Try taking the $sql out and just using result like this:

$result = mysql_query("SELECT * FROM useraccount WHERE id = '$q'");

That error you're receiving is telling you that there is a problem with your $result query. Make sure that the table name that you're selecting the information from is the correct table name as well.

Edited by JoeyMagz, 23 December 2010 - 05:30 PM.


#9 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 28 December 2010 - 04:39 PM

Joey, you're kind of right. Different versions of mysql do handle queries differently. However, what you're suggesting is equivalent to what he had. Storing a string in a variable and passing it to a function is the same as passing a string constant to a function.

Since you brought up the issue though, the issue could be that he doesn't have backticks around the field names in the query. Unless he posts the results of what he gets from the link i posted, i can't say for sure though.

#10 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 28 December 2010 - 05:34 PM

Well, I don't know if others have run into the same issues, but on my computer I cannot do queries like this:
<?php $result = @mysql_query("SELECT * FROM blah WHERE username = ' ".$username." ' ");
I only put the spaces there to show I had the single quotes.

I have to instead do queries like this:
<?php $result = @mysql_query("SELECT * FROM blah WHERE username = '$username'");


It's a very strange issue and it took me a while to figure out the first time I ran into the problem. Another problem I have when running a webserverr on my computer is when I try something like this:
<?php @mysql_query("INSERT INTO blah VALUES($one, $two, $three, $four)");

I am instead forced to do this:
<?php @mysql_query("INSERT INTO blah VALUES(\"$one\", \"$two\", \"$three\", \"$four\")");


#11 tinks

tinks

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 03 January 2011 - 03:51 AM

hi guys thanks a lot for your replies, got it to work already :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users