2

I want to get data from mysql database using php and jquery ajax. 'process.php' is the php file which connects to database and get mysql data. It works when it is run separately, but when called using ajax it doesn't work. Can someone please help to correct error? Here is my html file:

<html>
<head>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 function showRoom(){
 $.ajax({
 type:"POST",
 url:"process.php",
 data:{action:showroom},
 success:function(data){
 $("#content").html(data);
 }
 });
 }
 showRoom();
});
</script>
</head>
<body>
<div id="content"></div>
</body>
</html>

Here is my process.php file

<?php
$link=mysqli_connect("localhost","root","raspberry","homebot");
if (mysqli_connect_errno())
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
$action=$_POST["action"];
if($action=="showroom"){
 $query="SELECT * FROM user";
 $show=mysqli_query($link,$query) or die ("Error");
 while($row=mysqli_fetch_array($show)){
 echo "<li>$row['name']</li>";
 }
}
?>
Jeff B
9,14220 gold badges68 silver badges146 bronze badges
asked Mar 25, 2014 at 18:47
5
  • 1
    What doesn't work? Do you get an error? What is it? What debugging have you done? Commented Mar 25, 2014 at 18:47
  • I dont get an error.But nothing is printed.Printing from mysql database is expected ($row[name]) Commented Mar 25, 2014 at 18:54
  • Add if(!mysqli_num_rows($show)) echo "no result" before while. Maybe the query doesn't return anything? Or try to put name in quotes. Commented Mar 25, 2014 at 19:01
  • added.but still same result Commented Mar 25, 2014 at 19:06
  • Don't use arrays in strings: replace echo "<li>$row['name']</li>"; with echo "<li>".$row['name']."</li>"; Commented Mar 25, 2014 at 19:13

2 Answers 2

5

There are two syntax errors in your ajax call:

$(document).ready(function(){
 function showRoom(){
 $.ajax({
 type:"POST",
 url:"process.php",
 data:{action:"showroom"},
 success:function(data){
 $("#content").html(data);
 }
 });
 }
 showRoom();
});

Keep in mind that jQuery's ajax expects an object as parameter. Inside an object the syntax is

{ key : value }

You had type="POST" which is correct in declarative syntax, but incorrect when defining an object key.

Second, the data property of the aforementioned object should be an object too. So instead of action=showroom it should be

{action:"showroom"}
answered Mar 25, 2014 at 18:53
Sign up to request clarification or add additional context in comments.

Comments

1

you did mistake in your code:

 echo "<li>$row['name']</li>";

This should be:

 echo "<li>".$row['name']."</li>";

try that...

answered Dec 6, 2014 at 23:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.