Change global Jquery variable value and use in PHP

1

Dear, I have a code that displays a dialog with a table, where after selecting the item, it is sent to an input field, via ajax. I need to use this value in a php function on the same page, so that another dialog displays a table based on the input data. I've been analyzing the use of a global jquery variable, however, I'm not getting this variable to get the input value for it to use in the future. Here is the code I use:

Javascript:

var cgc;
$(document).ready(function () {
    $(function () {
        $(".dialog").dialog({
            autoOpen: false,
            height: 400,
            modal: true,
            resizable: false,
            width: 700,                
            open: function (event, ui) { 
                $('.ui-dialog-titlebar').display("none"); 
            }
        });
        $(".opener").click(function () {
            $('input:checkbox').attr('checked', false);
            var nameD = $(this).attr("id");
            var toRemove = 'opener';
            var idDialog = "#dialog" + nameD.replace(toRemove, '');
            $(idDialog).dialog("open");
        });
        $(".botao").click(function () {
            $(".dialog").dialog("close");
        });
    });

    $(".botao").click(function () {
        getValueUsingClass(
            $(this).attr("id")
        );
    });

    function getValueUsingClass (id) {
        var chkArray = [];
        $(".chk:checked").each(function () {
            chkArray.push(
                $(this).val()
            );
        });
        var name = id;
        var toRemove = 'bt';
        var id = name.replace(toRemove, '');
        var val_id = [];

        $("input:checked").each(function () {
            val_id.push(
                $(this).attr("id")
            );
        }); 

        var selected;
        selected = chkArray.join('') + "";
        var val_id2;
        val_id2 = val_id.join('') + "";

        if (selected.length > 1) {
            if (chkArray.length > 1) {
                alert("Selecione um campo");
                $('input[id=' + id + ']').val("");
            } else {
                $('input[id=' + id + ']').val(selected);

                /* Configura a requisição AJAX */
                if(id=='Cliente') {
                    $.ajax({
                        url: 'dados_pedido.php', /* URL que será chamada */ 
                        type : 'POST', /* Tipo da requisição */ 
                        data: 'CODIGO=' + $('#Cliente').val(), /* dado que será enviado via POST */
                        dataType: 'json', /* Tipo de transmissão */
                        success: function (data) {
                            if(data.sucesso == 1) {
                                $('#codigocli').val(data.codigo);
                                $('#nomecli').val(data.nomecli);
                                $('#cnpjcli').val(data.cnpjcli);
                                $('#transpcli').val(data.transpcli);
                                $('#PagCli').val(data.PagCli);
                                cgc = data.cnpjcli;
                            }
                        }
                    });
                };
                return false; 
            }
        } else {
            alert("Nenhuma dado selecionado");
        }
    };
});

HTML and PHP:

<a href="#dialogPed3" id="openerPed3" class="opener">
    <img src="../../images/lupa.png" id="lupa3" width="16" height="16" alt = "Add"/>
</a>
<div id="dialogPed3" class="dialog">
    <?php 
        echo "teste"."<script>document.write(cgc);</script>";
    ?>
</div>

At the beginning of the jquery code there is a declared cgc variable, if I put a value immediately, type var cgc = 123, this is normally displayed in the echo of php, however when its value is changed inside the "$ (document) .ready (function () {"nothing happens, the value remains 123.

I do not know if it helps but I will explain the objective. In my page, in a certain field, there is a button that displays a dialog with a table, but this table is based on the value that is inserted in the input #cnpjcli, I have not found a way to pass this value to php the moment I call the function, what I'm trying to do is use the resource document.write (cgc); to store in a php variable and thus pass to the php function. If you have any other way to get the input value and move to the function, tb will help.

    
asked by anonymous 22.10.2014 / 13:29

2 answers

0

Thiago, this variable is not a "global" variable so to speak, it is defined within the scope of jquery and its script, however when the page is reloaded, or undergoes some change in which the code needs to be reloaded, she is lost.

Consider using cookies On this site or the use of LocalStorage here on this other site so that the value of the variable is not lost by reloading the page.

    
27.10.2014 / 15:35
0

You often declare a input type="hidden" and put id="xxxx" value="";

In the jquery script you set the value for input $('#xxxx').val(300);

in this case in the form that tah the input is like you typed 300 in it.

xxxx can be any other name you designate

    
11.08.2015 / 03:10