Fetching Data from Mysql and filling Inputs

Issue

I’ve got the following mysql table (expensemonthly):

enter image description here

I’m trying to fill inputs text in a form with the account_id (and account name fetch from another mysql table called "accounts"), for doing that from ajax code below I call a php file (CountFetchExpensesArrayDos.php) for creating a two dimention array:

//Count Registers and Fill Inputs
    $('#count').click(function(){ 
     
           $.ajax({  
                url:"CountFetchExpensesArrayDos.php", 
                method: 'POST',
                dataType: 'json',               
                data:{monthyear:"112021"},  
                success:function(data){
                    // array Receive
                        alert(data);
                        AddInputs(data.count);
                        alert("Se detectaron: "+data+" registros");
                        FillExpensesInputs(data.count,data.account_id,data.name);                                           
                    
              }  
           });  
        });

Below the CountFetchExpensesArrayDos.php file for getting data from the month (112021):

$Month = $connect->real_escape_string($_POST['monthyear']);
//Count number of month registers
$sqlc = $connect->query("SELECT * FROM expensemonthly WHERE yearmonth = '$Month'");
$count=$sqlc->num_rows;
    
if(! $connect ) {
    die('Could not connect: ' . mysqli_error());
}
         
//Get account_id and name for the month registers
$sql = "SELECT * FROM expensemonthly,accounts WHERE yearmonth = '$Month' AND accounts.account_id=expensemonthly.account_id";
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));
$final_array = array();
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) { 
        $final_array[] = $row['account_id'];           
        $final_array[] = $row['name'];      
    }
    $CountArray[] = $count;
    //Merge Array with number of register for the month, account_id,name
    $Totalarray = array_merge($CountArray,$final_array);
    //Send array to ajax
    exit(json_encode($Totalarray));
} else {
    echo "0 results";
}
mysqli_close($connect);

My problem is that when data is arriving to ajax in an unknown format, If show data "alert(data);" I see it comma separated, but I cannot splitted using javascript "split() Method", I need to split data receive for filling Inputs in the form, this the data as received in ajax:

enter image description here

Please any Ideas? If there another method to get data from month using php and pass it to Ajax?

Solution

Your AJAX code is expecting an associative array with 3 keys: count, account_id, and name. You’re not creating an associative array in the PHP code. You’re just appending each account ID and name as separate array elements.

You can create and push onto separate arrays for the account IDs and names. Then put them in the associative array when creating the JSON at the end.

//Get account_id and name for the month registers
$sql = "SELECT * FROM expensemonthly,accounts WHERE yearmonth = '$Month' AND accounts.account_id=expensemonthly.account_id";
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));
$account_ids = array();
$names = array();
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) { 
        $account_ids[] = $row['account_id'];           
        $names[] = $row['name'];    
    }
    //Merge Array with number of register for the month, account_id,name
    $Totalarray = array('count' => $count, 'account_id' => $account_ids, 'name' => $names);
    //Send array to ajax
    exit(json_encode($Totalarray));
} else {
    echo "0 results";
}
mysqli_close($connect);

Answered By – Barmar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published