2

i am making an ajax call to a php page. the php selects from the database between 2 dates.

this is the php:

if(isset($_POST['fromdate'])){
         
 $fromdate = $_POST['fromdate'];
        
 $todate = $_POST['todate'];
        
 $sql = "SELECT * FROM expenses WHERE date BETWEEN '$fromdate' AND '$todate' ";
            
 $result = mysqli_query($connection, $sql);
        
 while($row = mysqli_fetch_array($result)){
            
   $data['amount'] = $row['amount'];
            
   $data['date'] = $row['date'];
            
   $data['description'] = $row['description'];
            
   }
        
   echo json_encode($data);
        
}

my ajax call looks like this:

$('#new_report').click(function() {
                    
  var fromdate = $('#fromdate').val();
    
  var todate = $('#todate').val();
    
  $.ajax({
                        
    url: 'new_report.php',
                        
    method:'POST',
                        
    dataType: 'JSON',
                        
    data: { 
                            
    'fromdate' : fromdate,
                            
    'todate' : todate
    
    },
                                
  success:function (data){
    
  }
                        
});
                    
                    
});

this works great when selecting by id and only getting one row. i can simply:

success:function (data){

var amount = data.amount;

var date = data.date; //etc..

  }

but since it is a date range, and i get multiple rows, how do i work with the data and display it where i need it? i know i could go to a new page and loop through the php results, but i am forcing myself to work with javascript objects. what is the right way to think about this process?

10
  • I wanted to know how you want to display the data in the front end file Commented Jan 13, 2022 at 6:50
  • 1
    Your code is wide open to SQL injection. Not sure what your question really is here - are you asking how you process the JSON response that has multiple rows? Commented Jan 13, 2022 at 6:59
  • The PHP is overwriting the $data array each time through the loop. It should make a 2-dimensional array with a row for each object. Then you can loop over this array in the JavaScript. Commented Jan 13, 2022 at 6:59
  • 1
    Really? What security was there that would interfere with the presentation of a question? Commented Jan 13, 2022 at 7:03
  • 2
    $result[] = $data; in the loop, then echo json_encode($result); after the loop. Commented Jan 13, 2022 at 7:11

1 Answer 1

1

the question was answered by Barmar but here is the proper php code:

 while($row = mysqli_fetch_array($result)){
        
        $results[] = $row;
        
    }
    
    echo json_encode($results);

then, in javascript i can display it and (hopefully) work with it. something like this:

success:function (data){
                        
    var text = JSON.stringify(data);
                        
    $('#view').html(text);

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

Comments

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.