-6

This is the JQuery code loaded over click but the callback function isn't working:

$.getJSON("load_img.php", {"id":"start"} , function(json){
     alert(json);
});

PHP code gives me this output:

[
    {"img_name":"shiva.jpg","img_id":"1"}, 
    {"img_name":"shiva.jpg","img_id":"2"},
    {"img_name":"Recoverd_jpg_file(4).jpg","img_id":"3"}, 
    {"img_name":"Recoverd_jpg_file(542).jpg","img_id":"4"}
]

I cannot load any PHP pages from getJSON() function...i downloaded sample source code from [here][1]

[1]: http://www.sitepoint.com/ajaxjquery-getjson-simple/ and have run it on browser but it doesnt work still... any help would be much appreciated!!!Thank you

Sachin Bahukhandi
  • 1,470
  • 14
  • 24
  • Possible duplicate of [getJSON does not work](http://stackoverflow.com/questions/14786120/getjson-does-not-work) – Kamal Joshi Feb 05 '16 at 18:56

3 Answers3

1

I got my answer today...no <html> tags should be included in the code to be outputted... it should only contain starting and ending php tags i.e.

<?php
//your code or codes
?>
Mr Lister
  • 42,557
  • 14
  • 95
  • 136
Sachin Bahukhandi
  • 1,470
  • 14
  • 24
-2
    <button id="try" type="button">Try</button>

<script  src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    $.getJSON("load_img.php", {"id":"start"}, function(result){
        $.each(result, function(i, field){
             alert(field.img_name);
        });
    });

i is the field name and field is value. Please check and post me back. For further details :-http://www.w3schools.com/jquery/ajax_getjson.asp Page Two:-

<?php

  // ini_set("display_errors", "On");
 //header('content-type:application/json');

$id = $_GET["id"];

if ($id == "start")
{
    $con=new mysqli("localhost", "root", "rootwdp", "try");

    $numbers = array();
    $img_arr = array();
    $res = $con->query("select img_name, img_id from img");
    while ($row = $res->fetch_assoc())

    // mysql_fetch_array() passes parameters from 0 
    { $numbers[]=$row; }

    echo json_encode($numbers);
}

?>
rahul
  • 760
  • 1
  • 5
  • 17
-2
$.getJSON("path to php file", {method: "*", "id":"start"})
                    .done(function (data) {

                            console.log(data)

                    });
//================in php=================================
if($_REQUEST['method'] == "your method" AND $_REQUEST['id'] ) {
//call your php function
}                   
//-------------------
//and put your php code in function 
//.. i hope this help you