1

I'm using PHP and trying to get values from a MySQL database using jQuery/AJAX.

My mysql table has four columns: id, tail, cg and cw

My php code looks like this:

<?php

$inputvalue = $_POST;
$errors = false;
$result = false;

$mysqli = new mysqli('localhost', "root", "", "tp");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
    }

foreach ($inputvalues as $key => $value) {
    if(isset($value) && !empty($value)) {
        $inputvalues[$key] = $mysqli->real_escape_string( $value );
    } else {
        $errors[$key] = 'The field '.$key.' is empty';
    }
 }

if( !$errors ) {
    $addresult = "
        SELECT * 
        FROM `air_cg` 
        WHERE `tail` = '" . $inputvalue['tail'] . "' 
        ORDER BY `id` DESC

    ";

     if( $result = $mysqli->query($addresult) ) {
        // collect results
        while($row = $result->fetch_all())
        {
            $returnResult = $row;
        }
    }
}

mysqli_close($mysqli);
echo json_encode(['result' => $returnResult, 'errors' => $errors]);

exit;
?>

The resulting JSON has this format:

{"result":[["255","Lapdogie","1","2"],["254","Lapdogie","23","234"],["253","Lapdogie","132","454"]],"errors":false}

My javascript code that im using for the ajax function and to parse the resulting JSON looks like this:

function getcgdata(aa){

    $.ajax({
        type: "POST",
        url: "drawchart.php",
        data: {tailnumber:taildata},
        dataType: 'json',
        cache: false,
    })
    .success(function(response) {

        $('input').removeClass('error').next('.errormessage').html('');

        if(!response.errors && response.result) {
            $.each(response.result, function( index, value) {
        var chartdata=(value);
        var cgdata =(cg.value);
        console.log(chartdata);
        console.log(cgdata);
       });

        } else {

            // append the error to the form
            $.each(response.errors, function( index, value) {
                // add error classes
                $('input[name*='+index+']').addClass('error').after('<div class="errormessage">'+value+'</div>')
            });

        }
    });
}

The console log for chartdata shows this: ["256", "Lapdogie", "232", "333"] ["239", "Lapdogie", "23", "12"] ["238", "Lapdogie", "1232", "1232"]

The console log for cgdata only shows one value many times: 232 232 232

I am not sure if the issue is with my PHP code or with the way im trying to parse the JSON.

13
  • @SumanK.C Would I try that in my javascript like this: json_decode(value) ? Commented Jan 21, 2016 at 16:54
  • On the row: var cgdata =(cg.value);, where is cg defined? Commented Jan 21, 2016 at 16:57
  • @MagnusEriksson cg is one of the column names in my database Commented Jan 21, 2016 at 17:00
  • 1
    Change var cgdata =(cg.value); to var cgdata = value[2]; Commented Jan 21, 2016 at 17:05
  • 1
    You don't need to call fetch_all in a while loop. Since it returns all the rows, you just need to call it once. Commented Jan 21, 2016 at 17:47

2 Answers 2

1

i dont see that you define cg.value..

you use

var cgdata =(cg.value);

but cg is where defined?

it should be probably something like chartdata[3] .. ?

Sign up to request clarification or add additional context in comments.

6 Comments

I don't understand. When I use cg.value and use console.log(cg.value) it gives me the value of only one returned row from column "cg" in the database.
@bruno, that's impossible if your json output looks like the one you posted here.
you have not defined variable cg in this function, so it is probably derived from somewhere else... and it was set last time to 233, so thats why it shows always one value.. you need to use the function( index, value) { variables, so it is in the value variable and you should access it from there..
@MagnusEriksson Well, it somehow does work, i'm not sure how.
The reason is probably what @LudovitScholtz suggested.
|
0

Rajdeep Paul's suggestion below worked for me for this particular situation:

Change

var cgdata =(cg.value); 

to

var cgdata = value[2];

I'm aware that there are many issues with my code as pointed out by Ludovit Scholtz and Magnus Eriksson and I shall refine it at a later stage.

Thank you all!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.