How to pass javascript return to php variable

Asked

Viewed 155 times

1

I am passing the data of some inputs to a modal, I am doing this with jquery, until ai blza, more accurate the data that were sent and use them in a mysqli query, my doubt as I pass the values sent by jquery to php variables and so useswere at my consultation ?

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>
  • With HTTP requests which in this case can be asynchronous via AJAX.

1 answer

1


You can do this with a request via ajax 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 the get type you should pass the data through the url, if it is post you can pass on body of the requisition.

In this case the variables are separated by the 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 in order for javascript to receive the result of the request you must use a echo what you want to print on the screen with PHP, use return will not work. To exchange data between the two languages use json. PHP example:

$a = 'teste';

echo json_encode($a);

More information about ajax with jquery:

http://api.jquery.com/jquery.ajax/

Browser other questions tagged

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