1

Guys i have a problem actually my php code is correct but i dont know why it display undefined into my client side script. Could somebody help me

here is my api.php this is where i put my php

<?php 
 $dbcon = mysqli_connect("localhost", "root", "", "orms") or die("Server not  available" . mysql_error());
 $data = array();
 $result = mysqli_query($dbcon,"SELECT * FROM cottages") or die(mysqli_error());            //query
//$array = mysqli_fetch_row($result);
$data = array();
while($row = mysqli_fetch_assoc($result))
 {
  $data[] = $row;
 }
 echo json_encode( $data )                          //fetch result    
?>

here is my client.php code

   <?php 
     $dbcon = mysqli_connect("localhost", "root", "", "orms") or die("Server not available" . mysql_error());
 ?>
<html>
 <head>
<script language="javascript" type="text/javascript" src="jquery.js">    </script>
 </head>
 <body>
  <h2> Client example </h2>
 <h3>Output: </h3>
  <div id="output"></div>

  <script id="source" language="javascript" type="text/javascript">

  $(function () 
  {
$.ajax({                                      
url: 'api.php', data: "POST", dataType: 'json',  success: function(rows)        
 {
   for (var i in rows)
   {
  var row = rows[i];          

  var cot_id = row[0];
  var image = row[1];
  $('#output').append("<b>cottage: </b>"+cot_id+"<b> image: </b>"+image)
              .append("<hr />");
      } 
     } 
       });

    }); 
  </script>

       </body>
         </html>  

Thanks for your help in advance..

12
  • The problem is data: "POST" use type: "POST" Commented May 22, 2015 at 6:23
  • Since the script doesn't read parameters, it doesn't matter whether he uses GET or POST. Commented May 22, 2015 at 6:24
  • hello i did it but same error occured.. undefined Commented May 22, 2015 at 6:24
  • mysql_error() wont work with mysqli_connect different drivers. php.net/manual/en/mysqli.error.php Commented May 22, 2015 at 6:24
  • Hi Chris error still occured. with your suggestion to change it to mysqli_error(); Commented May 22, 2015 at 6:25

1 Answer 1

1

You're using mysqli_fetch_assoc, so the row will be an associative array, which turns into a Javascript object. But in the Javascript code you're accessing row as if it's an array, not an object. You need to use the column names:

var cot_id = row.cot_id;
var image = row.image;

(I'm just guessing the column names, because you used SELECT * so I can't see the actual names in your table.)

Sign up to request clarification or add additional context in comments.

1 Comment

Hi barmar! very thanks to your help.. this is now working. but wait theres more ill be trying to include javascript on this one :) wait a minute ill post it in a while.

Your Answer

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.