1

I am running a query to database which fetch a row and i want to return it as array which can accessed as array['min'], array['max'] etc. if i echo it with specific index, it shows the value correctly in ajax but i am unable to pass complete row through it to the ajax file.

PHP file:

<?php 
  // Database logic here
    $calid = reset($_POST);
    require '../incs/connect.php';
    $sql=mysqli_query($con,  "SELECT * FROM calcus_sets WHERE sets_id=$calid ");
    WHILE($row=mysqli_fetch_array($sql))
    {
        echo $row['min'];   break;
    }
    mysqli_close($con);
?>

JS file:

function DB_Fetecher(ops){
    $.ajax({
          type: "POST",
          data: {ops},
          url: "calcus_es1-fetcher.php",
          success: function(res)
          {
            //  alert( res );
            alert(res);
          }
    });
}

This code get right value from mysql but i want to pass full row to ajax and then use each index in ajax itself to put into textboxes.

1 Answer 1

1

You can pass array by:

<?php 
  // Database logic here
    $calid = reset($_POST);
    require '../incs/connect.php';
    $sql=mysqli_query($con,  "SELECT * FROM calcus_sets WHERE sets_id=$calid ");

    $result = array();
    WHILE($row=mysqli_fetch_array($sql))
    {

        $result[] = array( "min" => $row['min'], "max" => $row['max'] );
    }
    mysqli_close($con);

   echo json_encode( $result );
?>

On your js, make sure to specify dataType:"json",

function DB_Fetecher(ops){
    $.ajax({
          type: "POST",
          data: {ops},
          url: "calcus_es1-fetcher.php",
          dataType:"json",
          success: function(res)
          {
            //  alert( res );
            console.log(res);
          }
    });
}

Note: It is advisable to use console.log(res); instead of alert() on debugging.

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

5 Comments

not working, note fetcher function runs on indexchange of select list.
What do you mean by not working? Are you able to see a log?
Did you check on developer tools?
Great! Happy to help :)
Thanks you for the help

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.