How to incorporate values from the database into a js?

0

Good morning, it's my first time working with ajax and I would like to know how I can make a variable in JS take values from the database

This is my code in PHP to make the call of the data of the database

<?php 
  $host = "localhost";
  $user = "root";
  $pass = "pass";

  $databaseName = "bd";


$link=mysqli_connect($host, $user, $pass, $databaseName);

if (mysqli_connect_errno())
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
$results=array();

    $query="SELECT * FROM tasas";
    $show=mysqli_query($link,$query) or die ("Error");
    while($row=mysqli_fetch_array($show)){
       $results[] = $row['valor'];
       
    }   
    return $results[];
?>

Now I want the stored data to be able to replace the values that are here so that they take the values of the database

	if (pickup == 0){
		tasaP = 10;
		descP = "Casa u oficina";
	}
	if (pickup == 1){
		tasaP = 0;
		descP = "Centro de acopio";
	}
	else if (pickup == 2){
		tasaP = 5;
		descP = "Aliado 1";
	}
	else if (pickup == 3){
		tasaP = 6;
		descP = "Aliado 2";
	}
	
	if (delivery == 0){
		tasaD = 5;
		descD = "Casa u oficina";
	}
	else if (delivery == 1){
		tasaD = 10;
		descD = "Centro de acopio";
	}
	else{
	tasaD = 0;
	}

	if (insurance == 0){
		tasaI = 10;
		descI = "Básico";
	}
	else if (insurance == 1){
		tasaI = 25
		descI = "Premium";
	}
	else{
		tasaI = 0;
	}

	if (type == 0){
		tasaC = 0;
		descC = "Paquete";
	}
	else{
		tasaC = 10;
		descC = "Documento";
	}

I have read that it is done with Ajax but I can not understand how to grasp the values and replace the static values that I have in my js

If it is possible for someone to explain me in detail I would appreciate it

    
asked by Chicky Ng 30.01.2018 в 16:22
source

1 answer

0

Well, you can effectively use ajax to do what you want. First you have to know that the php has to return a json that AJAX can understand, so I leave the part of the code to pass the PHP fix to a JSON (I did not understand much your code, this goes after making the query. I know how to use Mysqli, but it's more or less the same idea):

$result = $statement->fetch();
    header('Content-Type: application/json');
        $rows = array(
            'pickup ' => $result['pickup'],
            'delivery' => $result['delivery'],
            'insurance' => $result['insurance'],
            'type' => $result['type']
        );
echo (json_encode($rows, JSON_FORCE_OBJECT)); //Esto equivale al return que tú pusiste

Now in the jquery, suppose you want to make the call when the page load ends, it would be this way (assuming that the values of pickup, delivery, insurance and type do not use them again, if you want you can save it in variables global if you need them again later):

$(document).ready(function() {
    $.ajax({
      url: 'ruta/nombre.php', //tu archivo php
      method: 'POST',
      success : function(data){
      updateVariables(data['pickup'], data['delivery'], data['insurance'], 
      data['type']); //Los mismos nombres que pusimos en PHP
      }
    });

    function updateVariables(pickup, delivery, insurance, type){
        if (pickup == 0){
          tasaP = 10;
          descP = "Casa u oficina";
        }else if (pickup == 1){
          tasaP = 0;
          descP = "Centro de acopio";
        }else{
              if (pickup == 2){
                tasaP = 5;
                descP = "Aliado 1";
              }
              else if(pickup == 3){
                tasaP = 6;
                descP = "Aliado 2";
              }
              else{
                tasaP = ?;
                descP = ?;
              }
        }

        if (delivery == 0){
          tasaD = 5;
          descD = "Casa u oficina";
        }else if (delivery == 1){
          tasaD = 10;
          descD = "Centro de acopio";
        }
        else{
        tasaD = 0;
        }

        if (insurance == 0){
          tasaI = 10;
          descI = "Básico";
        }else if (insurance == 1){
          tasaI = 25
          descI = "Premium";
        }else{
          tasaI = 0;
        }

        if (type == 0){
          tasaC = 0;
          descC = "Paquete";
        }else{
          tasaC = 10;
          descC = "Documento";
        }
    }
}
    
answered by 31.01.2018 в 17:45