Hi All,

I'm trying to call data from a php script using getJSON.

my php script, php/Validate.php:

echo "Hello, World!";

die();

My getJSON bit:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
            <script>
                $(document).ready(function(){
                  $("#ValidateButton").click(function(){
                    $.getJSON("php/Validate.php",function(result){
                      alert(result);
                    });
                  });
                })
            </script>

           <input type="button" value="Validate" id="ValidateButton"/>

I'm not having any luck getting anything output though.

Could someone point me in the right direction as to where I might be going wrong, please?

Thank you for any assistance offered.

I'm not a php developer, but my first thought is that your php script is generating HTML. The script needs to send back data in json format...

A quick search online has led me to the json_encode() method. For example..

 echo json_encode($data);

I'm going to look into this a bit more..

Not the greatest example... but it works as i had described above...

HTML
<!doctype html>
<html>
<body>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

    <input type="button" value="Validate" id="ValidateButton"/>

    <script>
    $(document).ready(function(){
        $("#ValidateButton").click(function(){
            $.getJSON("test.php",function(result){
                alert(result);
            });
        });
    });
    </script>
</body>
</html>
PHP Script
<?php

   $data = "Hello World";
   header('Content-Type: application/json');
   echo json_encode($data);

?>

Brilliant. Thanks. I really need to work on my js stuff. I neglect it too much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.