My jQuery Ajax doesn't work

Hello,
I added a Jquery ajax to a working code, It dousn’t work.
Here is the code

?><html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">

	
	  
      google.charts.load('current', {'packages':['corechart', 'bar']});
	  google.setOnLoadCallback(drawVisualization);
      google.charts.setOnLoadCallback();
	  
	  function load_coin_data(coin, title)
	  {
		var temp_title = title +' '+ coin;
		    $.ajax({
			url:"ajax_currency_family_balance.php",
			method:"POST",
			data:{coin:coin},
			dataType:"JSON",
				success:function(data){
				   
					drawVisualization(data, temp_title)
				}
					
		});
		
	  };	  
	////////////////////////    Drawing the graph    //////////////////////
      function drawVisualization(chart_data, temp_title){
		
       	var jsondata = chart_data;
	
        var data = new google.visualization.DataTable();
			data.addColumn('string', 'Couple');
			data.addColumn('number', 'Long-Profit');
			data.addColumn('number', 'Long-Loss');
			data.addColumn('number', 'Short-Profit');
			data.addColumn('number', 'Short-Loss');
			
			$.each(jsondata, function(i, jsondata){
				var couple = jsondata.profit; 
				var Long_Profit =  parseFloat($.trim(jsondata.TotalBuyProfit));
				var Long_Loss = parseFloat($.trim(jsondata.TotalBuyLoss));
				var Short_Profit = parseFloat($.trim(jsondata.TotalSellProfit));
				var Short_Loss = parseFloat($.trim(jsondata.TotalSellLoss));

				data.addRow([profit, Long-Profit, Long-Loss, Short-Profit, Short-Loss]);
			});
			
			}//End For Loop
			  
    var options = {
      title : 'temp_title',
      vAxis: {title: 'Balance', gridlines: { count: 10 }},
		     
      hAxis: {title: 'Traded Couples ',
	           hAxis: { ticks: [new Date(17,08), new Date(19,01)] }	  
	  },
      explorer: { actions: ['dragToZoom', 'rightClickToReset'] },      
          
	  colors:['green','red', '#00ff40','#ff4b69'],
       animation: {
          duration: 1000,
          easing: 'out',
          startup: true},
		  bar: { groupWidth: '80%' },
	  barVisibilityThreshold :-1,
	  seriesType: 'bars'};
	  //}
    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  
  </script>

  <script>
  
    $(document).ready(function(){
	
		$(':button').click(function(){
			var coin = $(this).val();
			if(coin != '')
			{
				load_coin_data(coin, 'Profit Loss of currencies traded against');
			}
		});
	});
  </script>
	
  </head>
  <body>
  <div id="header" class=""><h1>WELCOME TO FOREX ANALYZER</h1>  </div>
  <div id="FX_selector" class=""> 
	
		
		<?php
		foreach($_asset as $row)
		{
			echo '<button value="'.$row.'">'.$row.'</button>';
		}
		?>
  </div>
  <div id="chart_area" class="">
	<div id="chart_div"></div>
  </div>
    
	
  </body>
</html>

And Here is the code of the URL in the ajax function

<?php
echo'sucess';
require('website\db_connect.php');

//Take the selected coin from the coins menu
if(isset($_POST['a'])){
	  $coin = $_POST['a'];
	}

/*************************************************************/
/*  get data for chart summing profit/loss for coin family   */
/*************************************************************/


	
	
	try
	{
	$coin = '%'.$coin.'%';
	
	$sql = "SELECT item profit
				,SUM(CASE WHEN profit >= 0 AND type LIKE 'buy%'  THEN profit ELSE 0.0 END) AS TotalBuyProfit
				,SUM(CASE WHEN profit < 0 AND type LIKE 'buy%' THEN profit ELSE 0.0 END) AS TotalBuyLoss
				,SUM(CASE WHEN profit >= 0 AND type LIKE 'sell%'  THEN profit ELSE 0.0 END) AS TotalSellProfit
				,SUM(CASE WHEN profit < 0 AND type LIKE 'sell%' THEN profit ELSE 0.0 END) AS TotalSellLoss
				FROM data  WHERE item LIKE ? GROUP BY item";

				 $stmt = $db->prepare($sql);
				 $stmt->execute([$coin]);
				 $data =  $stmt->fetchAll(PDO::FETCH_ASSOC);
	}
		catch(Exception $e) 
	{
	   return false;        
	}
	
	foreach($data as$row)
	{
	  $output[] = array(
		  
	  'couple' => $row['profit'],
	  'buy-profit' => floatval($row['TotalBuyProfit']),
	  'buy-loss' => floatval($row['TotalBuyLoss']),
	  'sell-profit' => floatval($row['TotalSellProfit']),
	  'sell-loss' => floatval($row['TotalSellLoss'])
	  );
	}
	    echo json_encode($output);
		
		
	

?>

I added an alert command to the function load_coin_data(coin, title)

to write the var temp_title. and when I clicked a button it worked perfectly.

I went to the ajax_currency_family_balance.php file (which is the URL in the ajax function) and I added a line with the coin variable and On execution It wirked and gave desired results.

Yet The whole code is “Broken”

The function doesn’t send anythyng to the URL file, and the file is not executed

What did I miss?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.