Javascript function to return 3 separate values

Asked

Viewed 61 times

0

In the code visualize.php i do select no BD and I award a Table with the result of select and store in a variable $outworking. This variable is read by Javascript and written to .

I would like to change the code so that the Table layout is not in the $result variable, but in the modal body. That way I need Javascript to write the separate values in the modal.

Something like that:

$("#grid-tipo").html(id_tipo);
$("#grid-tipo").html(tipo);
$("#grid-tipo").html(status);

I tried to use a vector but was unsuccessful.

visualize.php

<?php
if(isset($_POST["id_tipo"])){
	include_once "conexao.php";
	$resultado = '';
	$query_user = "SELECT * FROM tipos WHERE id_tipo = '" . $_POST["id_tipo"] . "' LIMIT 1";
	$resultado_user = mysqli_query($conn, $query_user);
	$row_user = mysqli_fetch_assoc($resultado_user);
	$resultado .= '<dl class="row">';
	$resultado .= '<dt class="col-sm-3">Código</dt>';
	$resultado .= '<dd class="col-sm-9">'.$row_user['id_tipo'].'</dd>';
	$resultado .= '<dt class="col-sm-3">Tipo</dt>';
	$resultado .= '<dd class="col-sm-9">'.$row_user['tipo'].'</dd>';
	$resultado .= '<dt class="col-sm-3">Status</dt>';
	$resultado .= '<dd class="col-sm-9">'.$row_user['status'].'</dd>';
	$resultado .= '</dl>';
	echo $resultado;
}
?>

javascrip carrying the values

$(document).ready(function () {
    $(document).on('click', '.view_data', function () {
        var id_tipo = $(this).attr("id");
        if (id_tipo !== '') {
            var dados = {
                id_tipo : id_tipo
            };
            $.post('visualizar.php', dados, function (retorna) {
                $("#grid-tipo").html(retorna);
                $('#viewParamTipos').modal('show');
            });
        }
    });
});

modal code receiving values

<div id="viewParamTipos" class="modal fade"  tabindex="-1" role="dialog" aria-labelledby="viewParamTipos" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title" id="modalLabel">Visualização de Tipos de Veículos</h5>
				<button type="button" class="close" data-dismiss="modal" aria-label="Fechar"><span aria-hidden="true">×</span></button>
			</div>
            <div class="modal-body">
                <span id="grid-tipo"></span>
            </div>
            <div class="modal-footer">
				<button class="btn btn-danger btn-sm" type="button" data-dismiss="modal">Fechar</button>
			</div>
        </div>
    </div>
</div>

2 answers

2


What you can do is this, in your PHP (visualize.php), instead of returning a string with the table, return a string with comma separated values. Example:

$resultado="codigo,tipo,status";
echo $resultado;

On the page where the modal is, create a function to be executed when the modal is opened (show)

var retorno = ['','', ''];
$('#viewParamTipos').on('show.bs.modal', function (event) { 
    var modal = $(this)
    modal.find('#id').html(retorno[0]);
    modal.find('#tipo').html(retorno[1]);
    modal.find('#status').html(retorno[1]);
});

Note that the "return" array was created with an empty content.

To update its contents before displaying the modal, you will need to make the following adjustment when reading the return of your php page (click on the button with the class "'. view_data")

$.post('visualizar.php', dados, function (retorna) {
    retorno = retorna.split(','); //Converte a string em array
    $('#viewParamTipos').modal('show');
}); 

Here is some more information:

https://getbootstrap.com/docs/4.0/components/modal/

https://www.w3schools.com/jsref/jsref_split.asp

1

Bins as it would be if the view.php file had the following structure:

$resultado = array($row_user['id_tipo'], $row_user['tipo'], $row_user['status']);
print_r($resultado);

In the HTML the Java script

$.post('visualizar.php', dados, function (retorna) {
  $("#grid-tipo").html(retorna);
  $('#viewParamTipos').modal('show');
});

would print that content:

Array ( [0] => 1 [1] => Carro [2] => A )

As I would to print the separate elements of the PHP array without passing a string as you listed in the previous answer. Like a Javascript array receiving a PHP array, is that possible? I think it would give more security.

  • 1

    In your PHP file you can use json_encode to convert the array to JSON before sending the response. In your Javascript, just call the array. Here are a few examples: https://www.dyn-web.com/tutorials/php-js/json/array.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.