php - $_POST not picking up value (getJSON)

917

I am trying to use getJSON to pull data from a PHP file however when I pass in "data" it the $_POST call doesn't seem to be retrieving any data.

function runJSON(network,searchType){
        var URL     = returnURL(network,searchType,$('#song_field').val().split(' ').join("+"));

    var val = $('#song_field').val();
    var data = {val: val}

    $.getJSON(URL, val, function(data){
        if(document.getElementById("box") !== "undefined"){
            clearScreen();
            createCols();
        }

        songList = [];
        console.log(data);
        eval(network + "(data);");

        for(i = 0; i < songList.length; i++)
        {   
            displayTrack(i);
        }

        done = true; 

    });
}

php file:

<?php
    $input = $_POST["val"];
    $url   = "http://api.7digital.com/1.2/track/search?q=" . $input . "&oauth_consumer_key=myconsumerkey&country=GB&pagesize=2";

    $data = simplexml_load_file($url);
    $json = json_encode($data);
    echo ($json);
?>
98

Answer

Solution:

your$.getJSON call should pass indata notval:

$.getJSON(URL, val, function(data){

should be

$.getJSON(URL, data, function(data){

And your php should use$_GET not$_POST

$input = $_POST["val"];

should be

$input = $_GET["val"];
854

Answer

Solution:

You are usinggetJSON in wrong way,

var val = $('#song_field').val();

$.getJSON(URL, {val: val}, function(data){ //<- Note how data passed

Also,getJSON usesGET method so in PHP, use$_GET or$_REQUEST, i.e.

$_REQUEST['val'] 

or

$_GET['val']

Documentation: http://api.jquery.com/jquery.getjson/

People are also looking for solutions to the problem: php - I want this to validate, if no errors to mail out the answers.

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.