0

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.

2
Contributors
3
Replies
11
Views
3 Years
Discussion Span
Last Post by Tinnin
0

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..

1

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);

?>
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.