Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fetching Data from Mysql and filling Inputs in a Form

Status
Not open for further replies.

lexer

Programmer
Jun 13, 2006
432
VE
Hi, I've got the following mysql table (expensemonthly):

table_hrelo9.jpg


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:

Code:
//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):
PHP:
$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:
datareceived_rev9hi.jpg


Please any Ideas? If is there another method to get data from month using php and pass it to Ajax?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top