How to pass javascript return to php variable

1

I'm passing data from some inputs to a modal, I'm doing this with jquery, up to blza, more precise the data that was sent and uses them in a mysqli query, my doubt as I pass the values sent by jquery for php variables and so use them in my query?

Form:

<form class="form-horizontal" action="data/form_cadastra_devolucao.php" method="POST">
    <input type="hidden" id="cliente" name="cliente" value="<?php echo $cliente ; ?>">
    <input type="hidden" id="nome"    name="nome"    value="<?php echo $nome ; ?>">
    <div class="comtainer">
        <div class="row">
            <div class="col-md-6">
                <label for="data">Data:</label>
                <input type="date" class="form-control" id="data" name="data" required>
            </div>

            <div class="col-md-6">
                <label for="loja">Loja:</label>
                <select class="form-control" id="loja" name="loja" required>
                    <option ></option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                </select>

            </div>
        </div>
    </div>
    <br>
    <div class="row">
        <div class="col-md-6">
            <label for="pdv">Pdv:</label>
            <input type="text" class="form-control" id="pdv" name="pdv" required>   
        </div>
        <div class="col-md-5">
            <label for="cupom">Cupom:</label>
            <input type="text" class="form-control" id="cupom" name="cupom" required>
        </div>
        <div class="col-md-1">
            <a data-toggle="modal" href="#modalItensCupom">VALIDAR</a>
        </div>
    </div>
    <br>
    <label for="motivo">Motivo:</label>
    <textarea class="form-control" rows="5" id="motivo" name="motivo"></textarea>
    <br>
    <button type="submit" class="btn btn-primary" style="float: right;"><span class="glyphicon glyphicon-ok"></span> CADASTRAR</button>
</form>

Modal:

<!-- Modal -->
<div class="modal fade" id="modalItensCupom" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">CUPOM <div id="rcupom"></div></h4>
            </div>
            <div class="modal-body">
                <p id="rdata"></p>
                <p id="rloja"></p>
                <p id="rpdv"></p>
                <p id="rcupom"></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
            </div>
        </div>

    </div>
</div>

Jquery:

<script type="text/javascript">
    ;(function($) {
        $('#modalItensCupom').on('show.bs.modal', function() {
            var idata  = $("#data").val();
            var iloja  = $("#loja").val();
            var ipdv   = $("#pdv").val();
            var icupom = $("#cupom").val();

            $( '#rdata' ).text( idata );
            $( '#rloja' ).text( iloja );
            $( '#rpdv' ).text( ipdv );
            $( '#rcupom' ).text( icupom );
        });
    }(jQuery));
</script>
    
asked by anonymous 16.01.2018 / 20:25

1 answer

0

You can do this with an ajax request for PHP.

For example:

$.ajax({
   url : 'caminho-arquivo-php',
   type: 'get' // método http, como : put, post, delete ....
   success: function (dados) {
    // tratamento 
   },
   error: function (error) {
     // tratamento
   }
})

If the request is of type get you must pass the data through the url, if it is post you can pass%% of the request.

In this case the variables are separated by the body character.

GET example:

url : 'arquivo.php?a=1&b=2&c=3'

Post example:

type: 'post',
data {
 iloja : iloja,
 icupom : x,
},

It is important to note that for javascript to receive the result of the request you should use & in what you want to print on the screen with PHP, using echo will not work. To exchange data between the two languages use return . PHP Example:

$a = 'teste';

echo json_encode($a);

More information about ajax with jquery:

link

    
16.01.2018 / 22:19