1

updated

Err i try to see more tutorial and i decided to use $.get() first since its easier and good for starting point..

so this is the script and i think it works correctly except it gives undefined result

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Display Json</title>
<script src="../_js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
    $(document).ready(function()
    {
        $('#jsonButton').click(function()
        {
            var data = ''
            $.get('getJson.php', data, response);
        });//end click
    });//end ready

    function response(data)
    {
        $('#display').prepend('<p>' + data.name + data.phone + '</p>');
    }
</script>
<body>
    <div id="display">

            <input type="button" id="jsonButton" value="getJson!" />

    </div>
</body>
</html>

and this is the getJson.php simple php script that returns simple JSON object :

$data['name'] = 'username';
$data['phone'] = '08989808089';

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

when i click the 'getJson' button, it displays undefined

1
  • did you use a browser debugger like firebug or the chrome dev tools ? Commented May 17, 2013 at 9:24

3 Answers 3

3

that is because your selector is incorrect

$('submit').click(function()
//-^^^^^----here

it should be

 $('input[name="submitButton"]').click(function(){
  ....  
}

OR give an id to your button and use id seletor with #

 <input type="submit" name="submitButton" value="getJson!" id="getjson"/>

  $('#getjson').click(function(){
   ......

OR you can use

$('input:submit').click(function(){ 
  .....
});

updated

and for undefined you can call the callback function .....

$.get('getJson.php', data, function(data){
    $('#display').prepend('<p>' + data.name + data.phone + '</p>');
});
Sign up to request clarification or add additional context in comments.

1 Comment

Pretty much the same answer as my own... posted at same time... yet 3 upticks and an accepted answer. And practically no feedback from the op. Won't bother with his questions again.
0

You need to select the correct button to bind click event.

$('input:submit').click(function(){  });

Comments

0

I'm not 100% sure you've got the selector right. I'd give the button an id and use that.

<script type="text/javascript">
    $(document).ready(function()
    {
        $('#mybutton').click(function()
        {
            $.ajax(
            {
                type: 'GET',
                url: 'getJson.php',
                dataType: 'json',
                success: function(jsonData)
                {
                    $('#display').prepend('<p>' + jsonData.name + '  ' + jsonData.phone + '</p>');
                }
            });//end ajax
            return false;
        });//end click
    });//end ready

</script>
    <body>
        <div id="display">

                <input id="mybutton" type="button" name="submitButton" value="getJson!" />

        </div>
    </body>

Read about proper jquery selectors here

http://www.w3schools.com/jquery/jquery_ref_selectors.asp

There are many different selectors you could use to attach the click event to that button.

Edit: Change the type of the input to button as well... submit is for a form

2 Comments

ermm thx for pointing that out, after change that, it still doesnt work :(
Actually you should change the type of the input to button as well

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.